Scheduled Jobs in Azure Mobile Services

Sometimes, what seems to be a simple job can turn out into a not-so-simple job. It’s at that moment, some help is needed. Well, I struggled at adding a scheduled job (where it is? How do I have to add it?) and found out there are some little things to tell about it.

So you have created an Azure Mobile Service and found out it is possible to add scheduled jobs. Good for you! So you went to the Scheduler tab page in the portal?

sj01

And there was no job available yet. But you can create a new job. The free edition of Azure Mobile Services let’s you add one (1) jobs…

sj02

And the scheduler is pretty good, it is simple but quit flexible in the way the job can be scheduled. But after creation, if you try to run it once by hand, an error occurs:

sj03

Hmm, so there is no actual job available? Let’s create one. Go to the tab page ‘in the shape of a cloud’ and see it’s possible to get the actual source code (the C# solution which can be run in VS201x). Just follow the dialog and hit the Download button:

sj04

When the zip is downloaded and extracted, start the solution and just check if you can compile it completely (or get the Azure SDK first and let VS201x pick the Nuget packages while compiling).

The solution is packed with four projects: there are two clients, some shared code and the actual AMS service. We are only interested in the last one. This service project contains a folder called ScheduledJobs.

sj05

This folder already contains a SampleJob but this is not the job we expected. So let us add a new job called NewTestJob. There is no template available in Visual Studio so I just took a copy of the existing job and renamed it. The most important steps are to override the ScheduledJob base class and to take the exact same name as in the portal but with the trailing ‘Job’.

sj06

Just for clarification, this job logs some text in the portal log. You can do whatever you want to program in this job but the log is a nice tool to check if everything works.

After that, start deploying (Right click on the service project in the Solution Explorer and hit Deploy). If needed, attach the code to the AMS declaration (which you have created in the cloud at the beginning).

sj07

If publishing succeeds, you get that smiling blue man group (a smurf taking a selfie using it’s short arms while laying on it’s side?)

sj08

Ok, we are ready for some action. Due to the ‘Convention of Configuration’ the portal now can discover the job. So try to run it once again. This time it should not fail!

sj09

Let’s look at the outcome in the logging. Go to the Logs tab and see a row is added containing the text which is logged by the job (right after the failed attempt).

sj10

So in the end, it turns out it’s very easy to add a scheduled job. Just inherit from the right base class and follow the ‘convention over configuration’ rule.

Real die-hards can check the poral:

sj11

After adding the Master key of the AMS in the password field of the Basic authentication dialog (leave the name field just empty) the job is executed.

sj12

A second line is added to the log. Mission completed.

Advertenties