Monday 22 April 2019

Timer trigger in web job Azure

In this post we are going to see how to create the timer trigger in azure and what is the purpose of it.

TimerTrigger is the attribute which does the trigger for execute a function at certain intervals in webjob.

Code:
class Program
  {
    static void Main(string[] args)
    {
      JobHostConfiguration config = new JobHostConfiguration();
      config.NameResolver = new TriggerResolver();
      config.UseTimers();

      JobHost host = new JobHost(config);
      host.RunAndBlock();
    }

    private class TriggerResolver: INameResolver
    {
      public string Resolve(string name)
      {
        string value = ConfigurationManager.AppSettings[name];              
      }
    }
  }
  
  public class Startup
  {
    public static void ScheduleTrigger([TimerTrigger("%Schedule%")] TimerInfo timer)
    {
      
    }
  }
}



From the above code you can see the schedule information is loaded from configuration file using INameResolver interface

Configuration value:
<add key=”Schedule” value=”24:00:00″/>


in another way we can schedule the timer using the typeof a class or cron expression

public static void LoggingFunction([TimerTrigger("0 0 6 * * *", RunOnStartup = false)] 
TimerInfo timerInfo, TextWriter log) 
{

  //Do stuff every day at 6AM

}


in the above cron expression

*    *    *    *    *    *  command to be executed
{second} {minute} {hour} {day} {month} {day-of-week}


From this post you can learn Timer Trigger in Azure webjobs

2 comments:

  1. Nice articel, This article help me very well. Thank you. Also please check my article on my site about Web Developer.

    ReplyDelete