<p>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 <code>&quot;Application is shutting down...&quot;</code>.</p>
<p><strong>My aim is to let the current execution finish before the function shuts down. Is there any way to achieve this?</strong></p>

Chayan Bansal
<p>First of all, step one is to get notified that the function is about to be aborted. You can use <a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library?tabs=v2%2Ccmd#cancellation-tokens" rel="nofollow noreferrer">the CancellationToken</a> for that:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>Now you know that the function is about to be aborted you can react on that. However, <a href="https://github.com/Azure/azure-functions-host/issues/7103" rel="nofollow noreferrer">you have just 10 seconds</a> before the function is aborted. So that gives you limited time to complete the message processing.</p>
<p>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.</p>
