Judy007
Judy007

Reputation: 5870

Do azure functions gracefully shutdown existing function invocations when deploying new function app?

Id like to understand the implications of deploying a new release of a azure function app as it relates to existing function invocations/threads. I understand that with frameworks such as asp.net, there were options such as adding app_office.htm to the root.

How do we ensure that we dont terminate active threads and allow the application to gracefully shutdown before deployment?

Upvotes: 1

Views: 5394

Answers (2)

Murray Foxcroft
Murray Foxcroft

Reputation: 13755

Option 1: Graceful shutdown is achieved using cancellation tokens. One thing I can't seem to determine is the grace period you have once the cancellation token is issued :(

public static class AsyncExample
{
    [FunctionName("BlobCopy")]
    public static async Task RunAsync(
        [BlobTrigger("sample-images/{blobName}")] Stream blobInput,
        [Blob("sample-images-copies/{blobName}", FileAccess.Write)] Stream blobOutput,
        CancellationToken token,
        ILogger log)
    {
        log.LogInformation($"BlobCopy function processed.");
        await blobInput.CopyToAsync(blobOutput, 4096, token);
    }
}

Option 2 is to use the durable functions extensions. These extensions provide reliable execution by maintaining state and using checkpoints to allow your functions to restart mid execution after a shutdown or failure. In short, the durable functions checkpoint the state of your function at every await statement and enable replay from the last successful await statement.

Option 3 is to use use deployment slots and have a Prod1 and Prod2 slot and alternate deployment. This will allow you built a drain-stop approach to preserve running instances to completion. You will still need to cater for failed instances and unknown exceptions with durable functions or a cancellation token.

Option 4 is to use two functions app instances and use a load balancer to manage the traffic across them.

Upvotes: 2

Vova Bilyachat
Vova Bilyachat

Reputation: 19494

Yes if you use cancellation token.

So by default if your AF will go to shutdown it will notify threads about shutdown, if you don't use cancellation token then it will wait some time(I can't find how long) and kill your threads even job is not complete.

Upvotes: 2

Related Questions