Home > OS >  Does a timer-triggered Azure function execute if an instance of the function is already executing?
Does a timer-triggered Azure function execute if an instance of the function is already executing?

Time:08-31

I have an Azure Function App with the [TimerTrigger(...)] attribute, which is scheduled to execute every morning.

Suppose you manually execute the function via the Function Menu Blade -> Code Test -> Test/Run, as shown at the bottom of this message. What happens if this manual execution of the function is still running when the time specified in the TimerTrigger attribute arrives?

  1. Will the manual execution be interrupted by the timer-triggered execution?
  2. Will the manual execution prevent the timer from triggering a new execution of the function?
  3. Or will a new instance of the function be kicked off when the timer triggers it, running in parallel with the existing manual execution?

enter image description here

CodePudding user response:

You can run the same azure function at the same time. You can add a bool if you want to prevent the execution of the azure function if it is currently running:

public class AzureFunction()
{
   private static bool _isRunning = false;

   [FunctionName("Function1")]
   public async Task Run([TimerTrigger("0 30 9 * * *")] 
      if (_isRunning)
      {
         //an instance is already running, lets not continue
         return;
      }
      _isRunning = true;

   //work is being done

   //after work is done
   _isRunning = false;
}

Say you have a Timer that goes off every minute, but your code takes 2 minutes to run. The timer will kick off every minute regardless of if the previous run is done or not.

If you set your TimerTrigger to run at a daily time, a manual run will not interrupt or cause an issue to your daily run but you can still do a manual run during your timed trigger. Example of the one above will run every day at 9:30am.

  • Related