Ski weather app with the new Mobile Services scheduler
21 December 2012
Update: Corrected the server script code snippet.
Update 2:The example below will unfortunately no longer work due to Twitter API changes… I need to update it. In the mean time, our official documentation shows a very similar scenario, so check that out.
As Scott announced earlier today, Mobile Services is celebrating the holiday season by bringing you some cool new features, including the ability to execute scheduled jobs (or “cron jobs”).
"Windows Azure Mobile Services now supports the ability to easily schedule background jobs (aka CRON jobs) that execute on a pre-set timer interval, and which can run independently of a device accessing the service (ensuring that you don’t block or slow-down any requests from your users). This job scheduler functionality allows you to perform a variety of useful scenarios without having to create or manage a separate VM."
Some of the scenarios you can enable with it include:
- Periodically purging old/duplicate data from your tables.
- Periodically querying and aggregating data from external web service (tweets, RSS entries, location information) and cache it in your tables for subsequent use.
- Periodically processing/resizing images submitted by users of your service.
- Scheduling the sending of push notifications or SMS messages to your customers to ensure they arrive at the right time of the day.
Here I want to give a more detailed demonstration of the feature and celebrate another winter tradition: skiing. Let’s walk through a simple scenario where we will use a scheduled job to aggregate and analyze tweets related to ski conditions near Seattle. The source code is available here.
Let’s assume we already have a mobile service called skibuddy. We can navigate to the new Scheduler tab in the Windows Azure portal to create a scheduled job.
Let’s go ahead and create a job called getUpdates. We will use the default interval of 15 minutes that is already selected in the dialog.
Once the job is created we will now see it in the summary view. You will note that the job starts off as disabled, so let’s click the job name to add our script.
On the job details page, we see information about our job and also we also have an opportunity to change the schedule. For now let’s leave everything as-is and navigate over to the Script tab.
Here we see the script editor window that has been pre-populated with a default empty script.
Let’s replace the default script with the following one.
~~~ javascript var updatesTable = tables.getTable(‘updates’); var request = require(‘request’);