Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions Timer Trigger thread safety

I was wondering if anybody knows what happens if you have a Cron setting on an Azure Function to run every 5 minutes if its task takes more than 5 minutes to execute. Does it back up? Or should I implement a locking feature that would prevent something, say in a loop, from handling data already being processed by a prior call?

like image 926
devlord Avatar asked Mar 14 '18 15:03

devlord


People also ask

Is Azure function thread safe?

HTTPClient is thread safe, as many others are, so you dont need to worry about it, but please check the documentation thoroughly for other classes.

What is timer trigger in Azure function?

The timer trigger uses a storage lock to ensure that there is only one timer instance when a function app scales out to multiple instances. If two function apps share the same identifying configuration and each uses a timer trigger, only one timer runs.

How do I manually trigger a timer in Azure?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.

Can Azure function have two triggers?

A trigger defines how a function is invoked and a function must have exactly one trigger.


2 Answers

Azure function with timer trigger will only run one job at a time. If a job takes longer then next one is delayed.

Quoting from Wiki

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 current execution completes.

That is true even if you scale out.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer#scale-out

You may want to ensure that your function does not time out on you. See https://buildazure.com/2017/08/17/azure-functions-extend-execution-timeout-past-5-minutes/ on how to configure function timeout.

like image 179
Shahid Syed Avatar answered Sep 22 '22 00:09

Shahid Syed


If a timer trigger occurs again before the previous run has completed, it will start a second parallel execution.

Ref: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference#parallel-execution

When multiple triggering events occur faster than a single-threaded function runtime can process them, the runtime may invoke the function multiple times in parallel.

Something else to note is that even using the new WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT settings, you cannot prevent multiple executions on the same instance.

Each instance of the function app, whether the app runs on the Consumption hosting plan or a regular App Service hosting plan, might process concurrent function invocations in parallel using multiple threads.

The best advice is to either reduce the duration it takes to execute, through optimisation or changing the approach to the problem. Perhaps splitting the task down and triggering it in a different way may help.

like image 41
GodUK Avatar answered Sep 18 '22 00:09

GodUK