READ THIS FIRSTUpdate: 3-4-2016: If you're finding this blog and looking for information related to Azure Mobile Services, I'd strongly recommend checking out Azure Mobile Apps. Azure Mobile Apps is a new version (consider it a v2) of Azure's mobile backend support. All of the same features of Azure Mobile Services are there, with a lot of other very cool features to go along. You can read more about Azure Mobile Apps, and how to transition from Azure Mobile Services, here.
Today I’m happy to announce the availability of scheduled scripts inside of Windows Azure Mobile Services. Scheduled scripts, also known as CRON jobs, allow you to define a server side script (just like you can for table CRUD operations) that you can then make run on a schedule or on demand if you want. This opens up a wide array of scenarios to do background processing, on the fly push notifications to users, and many other things. Today I’m going to walk you through creating a very simple script which will compliment the quick start application that is available with Mobile Services. I’ll walk through creating a new service quickly but if you want, you can skip that step and go down to the Scheduler part beneath it.
Creating our Mobile Service
Start by logging into the Windows Azure portal and going to Mobile Services. Click the create button in the bottom left of the portal and choose a new Mobile Service:
In the next screen, pick a URL for your service, choose whether you want to use an existing SQL database or create a new one, and select the region you want your service hosted in. Proceed to the second screen and complete the database settings. When you’re done with that click the checkmark in the bottom right and your Mobile Service should start being created. After it switches from Creating to Ready, you can click into your new Mobile Service. The first screen you’ll get to is the quick start. I’m going to use the iOS platform today and follow the Create a new iOS app instructions. After creating the TodoItem table and downloading my project, I can run my application and get a simplified Todo list app when I’ve used to add a few items:
Now we’ll look at scheduling a script.
For the next steps, I’m going to talk about and add a scheduled script to my app. For this example, I’m assuming that you’re using the quick start app and that you’ve already created the TodoItem table and inserted some items. Let’s return to the portal and at the top of the screen for your Mobile Service, you should now see a Scheduler tab:
Go ahead and click on that tab. The next screen should inform you that there aren’t any scheduled jobs for your Mobile Service and should have a link to Create a Scheduled Job. Go ahead and click the create link. The next window should look like this:
Here we can specify a name for our job and then choose what schedule to run it on or to run it on demand. I’m going to name my job AddTodoItem and schedule it to run on demand. After creating it, you’ll see a grid of all your (currently only one) scheduled jobs:
Here we can see that our job is disabled, that it hasn’t been run before and isn’t set to run at any time in the future, and that it’s frequency of running is On demand. Tapping anywhere within the right four columns of your service will show you a Enable and Delete button at the bottom. The Enable button is only necessary if we are running the job on a schedule and not on demand. That way you could have multiple jobs and turn them on and off without needing to change their frequency. Click on the job’s name and you’ll be taken to the configuration screen:
Here we get a lot of the same information as we did on the grid in addition to being able to change the Schedule near the bottom. At the bottom of this screen, you’ll see again see an Enable and Delete button. Additionally, you’ll see the very important Run Once button:
We’ll use this button shortly to run our script. For now click the Script link near the top under the job name. This screen is where we can actually define our script as well as run it:
I’m going to change that script to handle inserting a new Todo item:
Here we’re just getting a reference to the TodoItem table and then calling it’s insert method. We pass into that a dynamically created object that has the same properties we’re sending in the application, a text field and a complete field. Notice that we’re putting the current date into the text property so we can visualize the items getting added more easily. Now save your script and after it’s saved, click the Run Once button. After it starts, you should see a message which says The following job was successfully started: ‘AddTodoItem’. Notice that this just means the job has started, not that it’s finished. We can check on the success by running our app and seeing if a todo was added:
As you can see, the script added my todo and it’s appearing without any issues. If for some reason it didn’t show up, we could return to the portal and go to the Mobile Service’s Logging tab to see if our script recorded any errors.
There is one important limitations to scheduled jobs and that is the number that you can have. When you’re running in the free preview for Mobile Services, you are limited to one scheduled job per mobile service (not per subscription). You can increase this limit by moving up to a paid reserved instance if you want. If you do try to create another scheduled job, you’ll see a notification appear at the bottom of the portal which says Cannot create scheduler job. The maximum number of scheduler jobs in free mode is 1. To create more jobs, please use the Scale tab to switch your service to reserved mode.
Today we released Scheduled scripts for Windows Azure Mobile Services. This allows you to run both on the fly and scheduled scripts. Today we just inserted another item into our table but there are tons of things you can do inside of these jobs. If you wanted to trigger an on the fly push notification, you could do it here. If you wanted to send a daily email (using the Send Grid module) you could set it up as a job here. These are just a couple things you might want to do. Additionally, as the capabilities of the server side scripts grow, you’ll be able to make use of new capabilities from both the CRUD scripts and your scheduled job scripts. There is more information available on Scheduled Scripts at Scott Gu’s blog here. You can sign up for a free Windows Azure trial and get access to Mobile Services here.