.NET Zone is brought to you in partnership with:

Sasha Goldshtein is a Senior Consultant for Sela Group, an Israeli company specializing in training, consulting and outsourcing to local and international customers.Sasha's work is divided across these three primary disciplines. He consults for clients on architecture, development, debugging and performance issues; he actively develops code using the latest bits of technology from Microsoft; and he conducts training classes on a variety of topics, from Windows Internals to .NET Performance. You can read more about Sasha's work and his latest ventures at his blog: http://blogs.microsoft.co.il/blogs/sasha. Sasha writes from Herzliya, Israel. Sasha is a DZone MVB and is not an employee of DZone and has posted 204 posts at DZone. You can read more from them at their website. View Full User Profile

Windows Azure Mobile Services - Scheduler Scripts

12.27.2012
| 2805 views |
  • submit to reddit

Scheduler scripts are a very recent addition to Windows Azure Mobile Services. This is a very nice feature that supports periodic background processing in your mobile service. As a reminder, in the original release of Windows Azure Mobile Services, the only opportunity to run any server-side work (in scripts) is when the client performs an operation on a table (select, insert, update, or delete). For any kind of periodic processing, you'd have to use an external solution or have a client ping one of your tables -- a very ugly solution, as opposed to a scheduled script.

When would you use scheduled scripts?

  • To perform background processing on data uploaded by your users, or data that you retrieve from some external web services. For example, if you allow free text uploads, you might want to index the text for faster searching, or mine the text to detect interesting patterns.
  • To prune unnecessary data from your tables, such as dead push channels (channels that represent devices that are no longer available, or devices where you app is no longer installed).
  • To send push notifications at a certain time of day, or when an interesting event occurs at some point in the future. This is helpful when you have users all over the world, and you don't want to send a push notification to European users at 8PM Pacific time...
  • But another very useful thing you can do with scheduled scripts is test your server-side code. As you write your server-side scripts, the only way to test them previously was to hit one of your tables with a CRUD operation. With scheduled scripts, you can run your script by clicking a button in the Windows Azure Management Portal.

    Image borrowed from Scott Guthrie's blog.

    When I was working on push notification support in my unofficial Android SDK for Windows Azure Mobile Services, it was very helpful to have a script that sends push notifications to all registered devices, and invoke it whenever I want without hitting a table with a CRUD operation:

function TestPush() {
    var reqModule = require('request');
    var channelTable = tables.getTable('pushChannel');
    channelTable.read({
        success: function(channels) {
            channels.forEach(function(channel) {
                reqModule({
                    url: 'https://android.googleapis.com/gcm/send',
                    method: 'POST',
                    headers: {
                        'Authorization': 'key=MY_AUTHORIZATION_KEY'
                    },
                    json: {
                        registration_ids: [channel.regId],
                        data: {
                            "__builtInType": "notification",
                            "contentTitle": "Test title",
                            "contentText": "Test content",
                            "tickerText": "Test ticker",
                            "number": "42"
                        }
                    }
                }, function (err, resp, body) {
                    if (err || resp.statusCode !== 200) {
                        console.error('Error sending GCM push: ', err);
                    }
                });
            });
        }
    });
}

    With the most recent command-line tool release to manage your Windows Azure Mobile Services, you can list, download, and upload your scheduler scripts from the command-line. Unfortunately, there's no way at this point to invoke the scheduler script from the command-line tool, but you can easily test any script from the portal, as we've seen above.

    I am posting short links and updates on Twitter as well as on this blog. You can follow me: @goldshtn
Published at DZone with permission of Sasha Goldshtein, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)