Schedule recurring jobs in Mobile Services
This topic shows you how to use the job scheduler functionality in the Management Portal to define server script code that is executed based on a schedule that you define. In this case, the script periodically check with a remote service, in this case Twitter, and stores the results in a new table. Some other periodic tasks that can be scheduled include:
- Archiving old or duplicate data records.
- Requesting and storing external data, such as tweets, RSS entries, and location information.
- Processing or resizing stored images.
This tutorial walks you through the basic steps of how to use the job scheduler to create a scheduled job that requests tweet data from Twitter and stores the tweets in a new Updates table. You can watch a video version of this tutorial by clicking the clip to the right.
Create the new Updates table
-
Log on to the Windows Azure Management Portal, click Mobile Services, and then click your mobile service.
-
Click the Data tab, then click +Create.

This displays the Create new table dialog.
-
In Table name type Updates, then click the check button.

This creates a new storage table Updates.
Now that you have somewhere to store Twitter data, you can create the scheduled job.
Create a new scheduled job
-
Click the Scheduler tab, then click +Create.

Note
When you run your mobile service infreemode, you are only able to run one scheduled job at a time. Inreservedmode, you can run up to ten scheduled jobs at a time.
-
In the scheduler dialog, enter getUpdatesfor the Job Name, set the schedule interval and units, then click the check button.

This creates a new job named getUpdates.
-
Click the new job you just created, then click the Script tab.

-
Replace the placeholder function getUpdates with the following code:
var updatesTable = tables.getTable('Updates');
var request = require('request');
function getUpdates() {
// Check what is the last tweet we stored when the job last ran
// and ask Twitter to only give us more recent tweets
appendLastTweetId(
'http://search.twitter.com/search.json?q=%23mobileservices&result_type=recent',
function twitterUrlReady(url){
request(url, function tweetsLoaded (error, response, body) {
if (!error && response.statusCode == 200) {
var results = JSON.parse(body).results;
if(results){
console.log('Fetched new results from Twitter');
results.forEach(function visitResult(tweet){
if(!filterOutTweet(tweet)){
var update = {
twitterId: tweet.id,
text: tweet.text,
author: tweet.from_user,
date: tweet.created_at
};
updatesTable.insert(update);
}
});
}
} else {
console.error('Could not contact Twitter');
}
});
});
}
// Find the largest (most recent) tweet ID we have already stored
// (if we have stored any) and ask Twitter to only return more
// recent ones
function appendLastTweetId(url, callback){
updatesTable
.orderByDescending('twitterId')
.read({success: function readUpdates(updates){
if(updates.length){
callback(url + '&since_id=' + (updates[0].twitterId + 1));
} else {
callback(url);
}
}});
}
function filterOutTweet(tweet){
// Remove retweets and replies
return (tweet.text.indexOf('RT') === 0 || tweet.to_user_id);
} This script calls the Twitter query API to request recent tweets that contain the hashtag #mobileservices. Duplicate tweets and replies are removed from the results before they are stored in the table.
-
Click Run Once to test the script.

This saves and executes the job while it remains disabled in the scheduler.
-
Click the back button, click Data, click the Updates table, click Browse, and verify that Twitter data has been inserted into the table.

-
Click the back button, click Scheduler, select getUpdates, then click Enable.

This enables the job to run on the specified schedule, in this case every hour.
Congratulations, you have successfully created a new scheduled job in your mobile service. This job will be executed as scheduled until you disable or modify it.
Next Steps