How to have a single project to hold multiple scheduled Azure Webjobs<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to have a single project to hold multiple scheduled Azure Webjobs

Posted on: April 25, 2016

They are many ways to run scheduled jobs on Windows Azure. You can have a console application uploaded and configured directly on the portal. This first solution isn't perfect because the schedule is done directly on the website, would be better to have it on code repository.

You can use the Azure Scheduler to setup trigger to manual job, but this is more expense and still require configurations that are not in a great format (as Cronjob).

You can create a file per Azure WebJob named settings.job and setup a Cronjob schedule. This is good but still need one project per Azure Webjob. The latest solution works fine. In the Properties/webjobs-list.json of your website, you define a single entry for each webjobs. When you publish your website, the Webjobs get updated. Here is an example of the webjobs-list.json.

2 "$schema": "",
3 "WebJobs": [
4 { "filePath": "../Jobs/webjob1.csproj" },
5 { "filePath": "../Jobs/webjob2.csproj" },
6 { "filePath": "../Jobs/webjob3.csproj" },
7 ... ... ] }

This is an example of one settings.job (in the previous example, we would have 1 per webjob, thus 3).

2 "schedule": "0 0 */1 * * 1-5"

You will also need a third file, also 1 per Webjobs called webjob-publish-settings.json. This one is next to the Properties/webjobs-list.json. This one define the Webjobs properties. It needs to be set to run OnDemand. Here is an example:

2 "$schema": "",
3 "webJobName": "ScriptBotBuyContest",
4 "startTime": null,
5 "endTime": null,
6 "jobRecurrenceFrequency": null,
7 "interval": null,
8 "runMode": "OnDemand"

Still, the problem is that we need to have a lot of work to do on each of you WebJobs since you need to have 2 .json file per WebJobs, a project per WebJobs and defined all of them in the website.

Microsoft Azure WebJobs Sdk Extensions provides an answer to that problem : 1 project that can run multiple scheduled WebJobs with the feature named Timer Trigger.

The first step, is to create a new Console Project and few Nuget package. The default one for WebJobs "Microsoft.Azure.WebJobs" is needed as well as "Microsoft.Web.WebJobs.Publish" that will let you public the WebJobs project with you website. Th third library needed is the Extensions on named "Microsoft.Azure.WebJobs.Extensions". This one depend on the package called "ncrontab". You will also needs "WindowsAzure.Storage" because some information needs to be stored on Azure Storage.

The second step, is to add 2 entries in Connection Strings.

1<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=...;AccountKey=..." />
2 <add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=...;AccountKey=..." />

You can create an Azure Storage for Dev and for Production if you do not want to mix up your local test. I haven't mentioned it but this solution will let you run it locally just by running from Visual Studio, with the normal "F5" debug this console.

The third step is to create a file named webjob-publish-settings.json under the Properties folder of your console solution. The important detail is to set the runMode to Continuous. The content should look like the following:

2 "$schema": "",
3 "webJobName": "ScriptJobs",
4 "startTime": null,
5 "endTime": null,
6 "jobRecurrenceFrequency": null,
7 "interval": null,
8 "runMode": "Continuous"

Forth and finally, you need to have your startup class having a specific initialization and 1 static method per job you have. If you have 50 jobs, than this class would have 50 static methods.

1static void Main() {
2 var config = new JobHostConfiguration();
3 config.UseTimers(); //Allow to use scheduler
4 JobHost host = new JobHost(config); host.RunAndBlock();

Here is an example of a signature of a job.

1public static void WebJob1(
2 [TimerTrigger("0 */12 * * * 1-5")]
3 TimerInfo timerInfo, TextWriter log) {
4 //Your job here
5 }

This solution doesn't need to have settings.job because the Cronjob is directly in the signature, with the attribute TimerTrigger. However, this method still need with your website to have the webjobs-list.json to link to the single project that hold all your jobs.

2 "$schema": "",
3 "WebJobs": [ { "filePath": "../Job/webjobprojectthatcontainsallyourjobs.csproj" } ]

This method is very powerful. It reduces the amount of project, whence create a overall build solution speed faster. Having a single project remove the amount of configuration files required with a single one for the job, and a single one for the website (webjobs-list.json to specify that single WebJob). It is also very powerful because you can see read the log from Azure portal, you do not loss feature. You can find some documentation on Microsoft Azure Extensible blog and on GitHub.