Azure Web Sites - scheduled tasks

If you're comfortable writing code in node.js, do take a look at Windows Azure Mobile Services. It has the capability to define and execute scheduled tasks. More about this can be found here: http://www.windowsazure.com/en-us/develop/mobile/tutorials/schedule-backend-tasks/.

Another alternative could be to use Aditi's Scheduler Service: http://www.aditicloud.com/.

Yet another alternative would be to write your own scheduler and hosting that solution in Azure Websites. I would recommend using Quartz.net scheduling library. It's free, open source and used by many folks.

I still think going Worker Role route for job processing is a viable solution. What you could do is host the front-end infrastructure in a Windows Azure Website and have it communicate to the worker role via Windows Azure queues. Assuming you host 2 instances of worker roles in Extra Small VM size, it's going to cost you about $30.00 per month ($0.02 x 2 x 750 hours). I wrote a blog post on building your own task scheduler and hosting it in a worker role not too long ago. You can read that post here: http://gauravmantri.com/2013/01/23/building-a-simple-task-scheduler-in-windows-azure/

Also, can I connect the site to my domain and use ssl for free using Azure Web Sites?

I don't think so. SSL is not free with Windows Azure Websites. Take a look at SSL pricing here: http://www.windowsazure.com/en-us/pricing/details/web-sites/.


It looks like Azure finally has proper scheduling built in - see the Azure Scheduler documentation

Whilst it's in preview and you'll most likely have to use the REST API for now, I'm sure it won't be long before the functionality is available in the management portal. I've been waiting ages for this!


You can use the Timer class to run the scheduler inside your Azure Web Site project. This way, you can have everything encapsulated inside your ASP.NET MVC project.

Info about Timer class: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

I tested this in a MVC project on Azure Web Site and can confirm that it works. To get this set up, start the timer inside Application_Start() of Global.asax.cs:

private Timer _timer;
protected void Application_Start()
{
    _timer = new Timer(1 * 60 * 1000); // 1 minute
    _timer.Elapsed += (sender, e) => ScheduledTask.Process();
    _timer.Enabled = true;

    // other code goes here
}

In my example, the timer calls ScheduledTask.Process() every minute. Here is what my ScheduledTask class looks like:

public class ScheduledTask {

    public int Id { get; set; }
    public DateTime Time { get; set; }

    public static void Process() {

        using (var db = new ApplicationDbContext()) {
            db.ScheduledTasks.Add(new ScheduledTask {
                Time = DateTime.Now
            });
            db.SaveChanges();
        }

    }

}

In Process(), you can do whatever you want, but I'm just creating an entry in the database to test whether this works. I'll keep this task working for about a day so and you can see the results here:

http://contactmanager1.azurewebsites.net/ScheduledTask

Update

It turns out that this isn't the best way to set up scheduled tasks, because this timer dies when the application pool times out (due to low activity on the site). The timer automatically starts up again when application pool starts up again, but for low volume sites, this isn't a reliable solution.