Using Azure Mobile Services to warm-up a website 1

Recently I’ve been working on a website deployed to a shared Website instance on Windows Azure, and some of my testers have said that the site is sometimes really slow to load – in order that the resources are kept to a minimum you’ll find that these websites shutdown after a few minutes inactivity.  A classic case where some warm-up scripts are needed, but where’s the best place to deploy these for minimal complexity, keeping the whole solution in Azure. You could configure a virtual machine and run some task management engine on that, but that feels like a lot of overhead just to keep a site alive. Best thing is that we can use this functionality on a free instance of WAMS, therefore incurring no additional cost.

Windows Azure Mobile Services has a scheduling engine which can be used for many tasks, we can deploy a job in to the scheduling engine on a free instance of WAMS (you can only have one scheduled task in the free mode, but we can do a whole load of things in that one task). We don’t really have any opportunity to deploy a complex schedule model, but that’s fine as we can simply keep our site alive every 15 minutes (that’s the minimum granularity WAMS task scheduling allows us). The following code shows how we can call our website from WAMS and make sure it’s alive:

function KeepAlive() 
{     
    KeepSiteAlive("http://www.yousayhello.co.uk");     
}
 
function KeepSiteAlive (siteurl) 
{
     console.info("Warming up "+siteurl);
     var httpRequest = require('request');
     httpRequest(siteurl, function(err, response, body)         
     {             
        if (err)
        {
                 console.warn("Couldn't retrieve site");                             
        }
        else if (response.statusCode !== 200)
        {
              console.warn("Bad response");             
        }
        else             
        {              
            console.info("Site Warmed Up!");             
        }                         
      });
}

So how do we go about setting this, and monitoring its output?

Creating the Service

Firstly – you need to create an Azure Mobile Service. From the Windows Azure Portal, select Add, You’ll get the following window up – create a Mobile Service:.

Create Mobile Service

Create a Mobile Service

You’ll then get the opportunity to name your service, and select which region you wish your service to run in. If you’re already running some websites that you want to warm-up, you should keep your mobile services in the same region.

 

Create A Mobile Service

Create a Mobile Service

 

You’ll see that the service creation wants to use a database; for a simple warm-up scheduler you’ll not need to actually consume any database resource, if you already have a SQL database then simply link to that – you’ll need to provide the credentials for your database.

Database Settings

Database Settings

 

When you’ve selected finish, your service will be created, and will become available in your Windows Azure Portal. If you click on the Scheduler tab when you’re looking at this service you get the opportunity to create a schedule.

 

Create A Scheduled Job

Create A Scheduled Job

You can then name your job, and set the schedule. The schedule definition isn’t complex, and the minimum granularity is 15 minutes, but this provides us with all we need to run our simple warm-up.

You can only add one scheduled service for a free instance of a Mobile Service, but you can warm-up many sites in one script.
 
Create Job Detail

Create Job Detail

Once the schedule has been created, you can edit the script that’s run each time the scheduler is called.

Azure Script Editor

Azure Script Editor

Here you can edit the script that you’ll run when the scheduler is called. You’ll notice that there’s an enable button on the bottom of this page. Don’t forget to enable your script before you finish!

Monitoring your schedule’s progress

You’ll notice in the code snippet at the top of this post that there are calls to console.info() and console.warn() – where does the output of this logging go? If you click on the Logs tab at the top of the page you’ll see all the output of the logging we have collected.

View Logs

View Logs

 

What next?

In my next post I’m going to show you how to use the Push notifications service in WAMS to send an alert to your mobile device if your site fails to respond to the warm-up properly, providing you with a very easy and convenient way of monitoring your site’s availability, and also how to warm-up other components in your infrastructure.

 

One comment on “Using Azure Mobile Services to warm-up a website

  1. Pingback: What’s the Stack? | Oveeo Engineering

Leave a Reply