Menu

Keep Your Azure Apps Active on Shared Servers

December 29, 2014 by Christopher Sherman

Websites and services on free and shared tiers of Microsoft Azure (and most other cloud providers) will idle your application after 20 minutes of inactivity to save resources. Unfortunately a “cold” application will take some time to spin up, resulting in a sub par experience for your visitors.

To keep a site “hot,” I’ll show you how to simulate requests to your application using Azure Mobile Services. The best part: this service will be free as long as you do not exceed the Azure pricing thresholds. Let’s get started.

Create an Azure Mobile Service

  1. Open the Azure management portal and click on the MOBILE SERVICES menu item.
  2. Click CREATE A NEW MOBILE SERVICE on the screen that appears.
  3. Under URL give your application a URL and make note of it. Select to create a free SQL database or, if you’ve already used your free database, create a new SQL instance. Under REGION, select the option closest to the majority of your hosted services. Finally, under BACKEND, choose JavaScript and click the arrow button to move to the next screen.
  4. Leave the database name as the default and either select an existing server or create one. Click the check button to generate the service once you complete the database server input fields.
  5. Once Azure finishes creating the service you will have a status of Ready. Click on the name of your service and, on the next screen, select the DATA tab. On the screen that appears, click ADD A TABLE.
  6. Name the table URL and, for the purpose of this tutorial, set the INSERT PERMISSION field to Everyone. You can change this later if your purposes require it. Click the check button to generate the table.
  7. Once the table finishes, click on the table name. On the next screen click on the COLUMNS tab and then ADD COLUMN.
  8. Leave the first field as String and, in the COLUMN NAME field, enter url.
  9. Click on the newly created column and select the SET INDEX menu item to optimize reads on our URL column.
  10. Now click on the SCRIPT tab, be sure the operation is set to INSERT, and paste the code below. These functions do a bit of validation and ensure that you don’t insert duplicate URLs.
function insert(item, user, request) {
var sitesTable = tables.getTable('url');
  
 if (item.url.length > 100) {
request.respond(statusCodes.BAD_REQUEST, 'Url length must be under 100.');
} else {
sitesTable.where({
url: item.url
}).read({
success: function (results) {insertIfUnique(results, request); }
});  
 }
}

function insertIfUnique (existingItems, request) {
if (existingItems.length === 0) {
request.execute();
} else {
request.respond(statusCodes.BAD_REQUEST, 'Url already added.');
}
}

Set an Azure Mobile Service Scheduler

Now that the service is configured, we need to schedule a task that will request saved URLs in our table to keep websites and services in memory.

  1. Return to the home view for your mobile service and click on the SCHEDULER tab.
  2. Click CREATE and provide a descriptive job name. You can leave the default schedule of 15 minutes unless your purposes require otherwise. Click the check button to generate the scheduler.
  3. Click ENABLE to start the scheduler.
  4. Click on the SCRIPT tab and replace the JavaScript with the code below. The function queries the table of URLs and uses Node to generate the HTTP request that will keep your website or service from going cold.
function ping() {
var sitesTable = tables.getTable('url');
var req = require('request');
  
 sitesTable.select('url')
.read({ success: function(results) {
results.forEach(function (siteObject) {
req.get({ url: siteObject.url});  
 });
}});  
}

Add a URL to the Service

The only thing left to do is add the URL of any website or service you wish to keep in memory. Open your favorite web debugging proxy and create a POST request to the URL of the mobile service you created in the first section. Below is a sample request using the Fiddler proxy tool.

POST http://your-mobile-service.azure-mobile.net/tables/url HTTP/1.1
User-Agent Fiddler
Content-type: application/json
Host: your-mobile-service.azure-mobile.net
Content-Length: 35

{"url":"https://your-app-name.com"}

Microsoft Azure