Search code examples
azure-webjobsazure-webjobssdk

TimerTrigger Schedules and code execution time


What happens if a function gets invoked by a TimerTigger every 5 minutes and for some reasons the code takes more than 5 minutes to complete?

  • Does this result in my function running twice at the same time?
  • Or does the interval start when the triggered code execution is completed?

I could not find an answer myself in the docs. I have to ensure that my function is running always as singleton.

Thanks, Alex


Solution

  • If your function execution takes longer than the timer interval, another execution won't be triggered until after the current invocation completes. The next execution is scheduled after the execution completes. You can see this in the code here. You can prove this to yourself by trying a simple local example - create a function that runs every 5 seconds, and put a sleep in there for a minute. You won't see another function start until the first finishes.

    As far as running singleton, the above shows that only a single function invocation runs at a given time on the same instance (VM). The SDK further ensures that no other functions are running across scaled out instances. You can read more about that here. To see this in action, you can simulate by starting two instances of your console app locally - one will run the schedule the other will not. However, if you kill the one running the schedule, the other one will pick it up after a short time (within a minute).