Azure worker role - background tasks schedule

David Dury picture David Dury · Jan 22, 2013 · Viewed 9.2k times · Source

I have one azure storage table where I have a bunch of tasks to be completed by a worker role at a certain time. Example:

       Task 1: -> run every 5 min
       Task 2: -> run every 1 min
       Task 3: -> run every 10 min
       Task 4: -> run every 1 min
       Task 5: -> run every 5 min
       ...........................
       Task 1000: -> run every 1 min

Is this approach correct: Each task has a column DateTime called "LastRun". There is another column called "RunEvery" that stores the time when the task has to be executed. The worker role iterates through all tasks continuously and for each task checks the column "LastRun" with the following approach:

      DateTime currentTime = DateTime.Now;
      if (currentTime >= (myTask.LastRun + myTask.RunEvery))
      {
           myTask.Execute()
      }
      else
      {
           Check.Next.Task.InTable();
      }

What about consuming resources if the worker role runs continuously? How we can spear resources? Or can I implement this in a better way? What is your advice?

Answer

Gaurav Mantri picture Gaurav Mantri · Jan 23, 2013

Adding to @Simon Munro's answer: Yet another way to implement task scheduling without external scheduler dependencies is by making use of Quartz library (http://quartznet.sourceforge.net/) in your worker role. I've used it in one of the project and it works extremely well. It gives you a lot of flexibility as far as scheduling tasks are concerned. You would still need to make of blob leasing and Windows Azure Queues to take care of concurrency issue among multiple instances of your worker role.

UPDATE: Inspired by this, I wrote a blog post regarding the same which you can read here: http://gauravmantri.com/2013/01/23/building-a-simple-task-scheduler-in-windows-azure/.