UPDATE: This content is now fairly outdated. Instead of the solution below, I recommend checking out the Azure Scheduler and Azure WebJobs services.

A common question I get related to Windows Azure Web Sites is how you handle scheduled tasks. When uptime is critical or you are running large processes I generally recommend using a Worker Role. It is pretty easy to setup a single worker role to run scheduled tasks. Additionally, you could easily deploy a Linux VM and setup your own cron service. However, if you are looking for something simple and free you can easily use a free hosted cron service to trigger a url hosted in Windows Azure Web Sites that performs a task on demand. There are limitations to this method such as how long the task can run, but it should work for simple jobs.

For this demo I am using SetCronJob.com. There are a bunch of free and paid hosted cron services available and I am not recommending any one over the other as I haven’t used any except for this simple demo. I just picked this one at random so be sure to research and test the service you use.

The first thing to do is create an action that runs at a set url in your web app. For this demo I will use node.js, but you could very easily do this with any language. Below is a simple action in an express app that responds to /crontask and sends an email using SendGrid.

app.get('/crontask', function(req, res) {
  var sender = new SendGrid.SendGrid('user','key');
  sender.send({
    to: 'john@contoso.com',
    from: 'anna@contoso.com',
    subject: 'test mail',
    text: 'This is a sample email message.'
  });
});

Now to trigger this task to run you will need to setup a cron scheduler. First, you will need to create an account on setcronjob.com. They have a free plan that allows you to schedule jobs at 10 minute intervals. After you have an account you just need to create a new cron job. Enter the url of your Windows Azure Web Site and the path where your cron job is located.

Now that you have the cron job setup your email task will run as scheduled. There are certainly more sophisticated tasks you could run such as reading from a queue for messages that need to be sent, but you can see how easy this is to setup.