Technical Blog of a Programming Enthusiast

Using timer triggers with Azure web jobs

If you are writing web jobs and you need to have these triggered on a scheduled basis you can use a timer trigger.

Timer trigger lets you specify a CRON expression or timespan value to decide when to trigger a webjob.

Usually how you do such a stuff for a webjob is to create a settings.json file and specify something like below

{
"schedule": "0 0 1 * * *",
"is_singleton": true
}

Alternatively, you can do the same stuff using a TimerTrigger which is built on top of Azure webjobs SDK. This is the same technology on top of which azure functions are built.

If you are new to webjobs then please go through the msdn documentation for the same.

How it works

Let’s create an azure webjob in visual studio. You would notice it creates a Functions.cs file. This is where all your triggered functions reside. There are many different kinds of triggers available viz. ServiceBusTrigger, QueueTrigger ,TimerTrigger etc.

There are three different ways I can specify the trigger timings (next time when the webjob should start) using this trigger.

Be careful that Cron expressions used and accepted by different systems can vary. If you are using one of the online tools to generate a cron expression, there is no guarantee it would work in .Net framework. There is a cheat sheet published here specifically for Azure which seems to work.

TimeSpan Expression

A TimeSpan structure represents an ‘interval of time’ in the .NET framework and we can parse a time span expression to schedule our timer trigger as shown below.

As I mentioned earlier you can also do this scheduling by adding settings.json file and specifying the expression there but the major difference lies with the fact that settings.json is a trigger for the whole webjob i.e. your ‘main’ method whereas with TimerTrigger and Functions you can specify multiple triggers for doing different things within the same webjob.

So if you are converting some existing console app to webjob then you can go for settings.json file else its preferable to go with trigger approach.

If you are interested in knowing more about webjobs, here are a couple of pluralsight courses which covers from basic to advanced of azure webjobs and app services.