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.

Cron Expressions

You can simply use a Cron expression as shown below.

[code language="csharp"] 

    public static void ProcessTimerAction([TimerTrigger("0 0 0 * * *", RunOnStartup = true)]TimerInfo timerInfo) 

        { 

            Console.WriteLine("Write your custom logic here ...."); 

        }

This trigger would run every day at 12 AM.

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.

public static void ProcessTimerAction([TimerTrigger("1.00:00", RunOnStartup = true)]TimerInfo timerInfo)
        {
            Console.WriteLine("Write your custom logic here ....");
        }

This would run every day i.e. after every 24 hrs.

Custom TimerSchedule Implementation

For advanced cases where you want to specify custom logic in determining when should be your next trigger for the webjob, you could write your own custom implementation as shown below.

public class JobSchedule : TimerSchedule
    {
        public override DateTime GetNextOccurrence(DateTime now)
        {
            return now.AddSeconds(86400); //Write your logic own logic to return the next trigger timing
        }
    }
 public static void ProcessTimerAction([TimerTrigger(typeof(JobSchedule), RunOnStartup = true)]TimerInfo timerInfo)
        {
            Console.WriteLine("Write your custom logic here ....");
        }

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.

 

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.