0

I have a function that is in a continuous Azure WebJob and that function is required to be called once every 15 minutes.

Code in Program.cs

    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var host = new JobHost(config);
        host.Call(typeof(Functions).GetMethod("StartJob"));
        host.RunAndBlock();
    }

Code in Function.cs

    [NoAutomaticTrigger]
    public static void StartJob()
    {
        checkAgain:
        if (DateTime.Now.Minute % 15 == 0 && DateTime.Now.Second == 0)
        {
            Console.WriteLine("Execution Started on : " + DateTime.Now);
            //Execute some tasks
            goto checkAgain;
        }
        else
        {
            goto checkAgain;
        }
    }

Is my approach correct? As this is an infinite loop, will this code block incur any type of performance issue to the AppService under which this webjob is hosted.?

AJIN
  • 65
  • 1
  • 1
  • 10

1 Answers1

-1

There are timer triggers for webjobs: function.json

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

c#

public static void Run(TimerInfo myTimer, ILogger log)
{
    if (myTimer.IsPastDue)
    {
        log.LogInformation("Timer is running late!");
    }
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}" );  
}

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

terrencep
  • 675
  • 5
  • 16
  • you caan also pass in a timespan expression for 15 minutes: ([TimerTrigger("0.15:00", RunOnStartup = true)]TimerInfo timerInfo) – terrencep Aug 20 '19 at 16:11
  • What you are talking about is azure functions. As per my code where should i place this Run function? Or can i make the function in my code **StartJob()** to behave like **Run()** in your suggestion? – AJIN Aug 21 '19 at 05:42
  • what you are looking for is the namespace Microsoft.Azure.WebJobs.Extensions.Timers. timerinfo will be the first param of startjob. – terrencep Aug 21 '19 at 13:58
  • @AJIN does this answer your question – terrencep Sep 15 '19 at 17:54