Search code examples
c#azureazure-webjobs.net-core-3.1timer-trigger

azure Webjob TimerTrigger, force scheduling


I have an azure webjob TimerTrigger.

We may be led to stop the webjob, in a way or another (for instance changing an appSetting, aborts the webjob).

The webjob is defined as TimerTrigger in Azure. There are storage accounts for AzureWebJobsDashboard and AzureWebJobsStorage in the appSettings of the webjob. The webjob is executed at a certain time of the day. The execution of the webjob may last 5 or 6 hours. there are massive writings in a storage account (around 13000) and in storage table.

The question is :

When restarting the webjob, there is, often, an UnscheduledInvocationReason: IsPastDue, OriginalSchedule. I would like to avoid that, that the next execution of the webjob will be done accordingly to the cron expression of the timerTrigger webjob.

Is it possible, and if so how to do that ?

Any idea ?

Regards.


Solution

  • If I understand the problem correctly, you could use RunOnStartup = false (default) and UseMonitor = false

    Timer trigger for Azure Functions

    Parameter Description
    RunOnStartup If true, the function is invoked when the runtime starts. For example, the runtime starts when the function app wakes up after going idle due to inactivity. when the function app restarts due to function changes, and when the function app scales out. So runOnStartup should rarely if ever be set to true, especially in production.
    UseMonitor Set to true or false to indicate whether the schedule should be monitored. Schedule monitoring persists schedule occurrences to aid in ensuring the schedule is maintained correctly even when function app instances restart. If not set explicitly, the default is true for schedules that have a recurrence interval greater than or equal to 1 minute. For schedules that trigger more than once per minute, the default is false.

    There are a few ways to set these values, however it can be set from the attribute it self

    [FunctionName("MyLovelyHorseFunction")]
    public static void Run(
      [TimerTrigger(
         "0 */5 * * * *",
         RunOnStartup = false,
         UseMonitor = false)]
      TimerInfo myTimer,
      ILogger log)
    {
       ...
    }
    

    Also note, you can explicitly check for past due (which may or may not help you out depending on your needs)

    if (myTimer.IsPastDue)
    {    
        ...
    }