Lets say I want to have a TimerTrigger function app that executes every 10 seconds and prints an increasing count(1...2...3...), how can I achieve this WITHOUT using environment variable?
What is the alternative to global variables in Azure Function Apps?
604 views Asked by Sasha Chernin AtThere are 2 answers
I suggest you look into Durable Functions. This is an extension for Azure Functions that allow state in your (orchestrator) functions.
In your case, you can have a single HTTP triggered starter function that starts a long running orchestrator function. The HTTP function passes the initial count
value to the orchestrator function. You can use the Timer functionality of Durable Functions to have the orchestrator wait for the specified amount of time before continuing/restarting. After the timer expires, the count
value is incremented and you can restart the orchestrator function with this new count
value by calling the ContinueAsNew
method.
This periodic work example is almost what you need I think. You still need to add the initial count
to be read as the input, and increment it before the ContinueAsNew
method is called.
If you need more details about Durable Functions, I have quite some videos that explain the concepts.
You're already using an Azure Storage account for your function. Create a table within that storage account, and increment the counter there. This has the added benefit of persisting across function restarts.
Since you're using a TimerTrigger, it's implicit that there will only ever be one instance of the function running. If this were not the case, you could end up in a race condition with two or more instances interleaving to incorrectly increment your counter.