Azure Function shutting down during execution

6/10/2021

I am running a Queue Triggered Azure Function in Kubernetes cluster. Sometimes, the Azure Function container stops the execution in while processing a message. The logs just show "Application is shutting down...".

My aim is to let the current execution finish before the function shuts down. Is there any way to achieve this?

-- Chayan Bansal
azure
azure-functions
azure-functions-runtime
keda
kubernetes

1 Answer

6/10/2021

First of all, step one is to get notified that the function is about to be aborted. You can use the CancellationToken for that:

A function can accept a CancellationToken parameter, which enables the operating system to notify your code when the function is about to be terminated. You can use this notification to make sure the function doesn't terminate unexpectedly in a way that leaves data in an inconsistent state.

Now you know that the function is about to be aborted you can react on that. However, you have just 10 seconds before the function is aborted. So that gives you limited time to complete the message processing.

One option is to cancel further processing and rollback any changes, if that is faster than completing the message, so the message get visible again for other instances that are alive to process.

-- Peter Bons
Source: StackOverflow