9

I have a simple Azure Worker role running that performs a task every day at 12 PM. Below is the code that accomplishes this.

public override void Run()
{
    try
    {
        while (true)
        {
              int time = Convert.ToInt32(DateTime.Now.TimeOfDay);
              if (time == 12)
              {
                   DoSomethingElse();
              }
        }

    }
    catch (Exception ex)
    {
        Log.Add(ex, true);
    }            
}

Here DoSomethingElse() is a method to send an email at every day 12 PM, and also fires once and only once per day.

How can I implement a scheduler that fire when the time is 12PM and execute DoSomethingElse().

My question is: Is this (above code) is the best method or use any 3rd party tool.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Hope
  • 1,252
  • 4
  • 17
  • 35
  • possible duplicate of [Azure - how do I run a job that calls a function in the webservice every hour?](http://stackoverflow.com/questions/8548159/azure-how-do-i-run-a-job-that-calls-a-function-in-the-webservice-every-hour) – David Makogon Jun 05 '12 at 12:52
  • On a side-note, your code above will cause a tight loop, running the CPU constantly at 100% and may cause Azure to restart your worker role. You need to at the very least put a sleep statement in there. Or use a timer to wait. But, in any case, due to multi-instance you need to look for other answers to the scheduling like what Makogon is talking about. – flytzen Jun 05 '12 at 20:49

4 Answers4

5

There are several other questions here that deal with this (and I've marked one above). Having said that, and at the risk of repeating what other answers already state:

In your case, a simple message on a Windows Azure Queue, time-delayed to not show up until noon, would work. This also helps deal with multi-instance scenarios: If you're running two instances of your role, you don't want the same scheduled task running twice, so you need a way to have only one of those instances execute this code. This is easily handled via queue message, or you could run scheduler code on a single instance by using something like a blob lease (which may only have one write-lock against it) as a mutex. This is covered in @smarx's blog post, here.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • But i have a doubt that if i implement it via queue message same problem still exixt becase multiple instance will create multiple que message at same time..? – Hope Jun 05 '12 at 13:03
  • 1
    True, it's a startup issue. Consider a mutex via blob lock. @smarx covered this in his blog post [here](http://blog.smarx.com/posts/building-a-task-scheduler-in-windows-azure). – David Makogon Jun 05 '12 at 13:05
  • Azure now has a simple scheduler service that calls an endpoint based on a highly configurable schedule. The problem is that it doesn't work for long running processes. The timeout is 30s and is not configurable. – AntonK Oct 19 '14 at 23:19
  • "This is easily handled via queue message" But how do you ensure only a single queue message gets added each day? – Mike Asdf Sep 01 '18 at 20:06
1

You could also use Quartz.Net http://quartznet.sourceforge.net/ using the blob lease mentioned ab ove is a great way to make sure only one of your instances is hosting tasks.

Using Quartz.Net to Schedule Jobs in Windows Azure Worker Roles

Alexandre Brisebois
  • 6,599
  • 12
  • 50
  • 69
0

Cloud Scheduler specifically deals with task scheduling in the cloud.

I just came across this so I Haven't tried it.

http://getcloudscheduler.com/

Update: Forget cloud scheduler! I ran my daily schedule 600 times consecutively resulting on 600 email being sent to my clients. Don't use!!!

AntonK
  • 2,303
  • 1
  • 20
  • 26
0

Use the Azure Task Scheduler. Nice tutorial by Scott Gu here.

Specifically, I would look at the Storage Queue action type - just register for queue events in your Worker Role.

(Note that this service may cost money if you want to schedule tasks more frequently than every hour.)

Dunc
  • 18,404
  • 6
  • 86
  • 103
  • I agree. This is the simpelest way. Just use a webhook to trigger the event. The Azure Task Scheduler is ridiculously expensive however for what it does. – Peter de Bruijn Dec 15 '16 at 11:11