Search code examples
c#azureazure-webjobsazure-storage-queues

Do Webjobs automatically renew leases on Azure Queue messages?


When Webjobs get a message from a queue on Azure Storage via QueueTrigger, it leases the message (makes it invisible). If the triggering function (of webjob) takes a long time to process the message, is this lease automatically extended? Or should I handle that in the function?

On this link Windows Azure Queues: Improved Leases, Progress Tracking, and Scheduling of Future Work, the author states that "A lease on the message can be extended by the worker that did the original dequeue so that it can continue processing the message"

Note: I've tried a webjob (with a QueueTrigger) which waits for 20 minutes.

//Write Log
Thread.Sleep(1200000);
//Write Log

It is completed successfully. And during this time no other webjob instance try to attempt for the same queue item (It did not become visible). Therefore it seems that an auto-renew mechanism for leases exists. Anyhow I am waiting for an answer from a Microsoft employee or with an official link (msdn, azure, ...).


Solution

  • Yes, your lease is automatically extended. It is 10 minutes each time.

    See this answer here [1] by a Microsoft employee referring to the docs and comments on azure.microsoft.com [2].

    EDIT (long answer)

    In addition, an examination of the source code, starting with the QueueListener class at https://github.com/Azure/azure-webjobs-sdk/blob/cfc875a7f00e595410c0603e6ca65537025490a9/src/Microsoft.Azure.WebJobs.Host/Queues/Listeners/QueueListener.cs indicates the same.

    The code in QueueListener has the relevant parts on line 138, where the 10 minute visibilityTimeout variable is defined:

    TimeSpan visibilityTimeout = TimeSpan.FromMinutes(10); // long enough to process the job
    

    That variable is then passed along to ProcessMessageAsync, which starts a timer defined in the method CreateUpdateMessageVisibilityTimer with that same value. The 10 minute value is used to determine when the first and next update the visibility timeout also (by halving it and creating an instance of the LinearSpeedupStrategy class).

    Eventually, in the class UpdateQueueMessageVisibilityCommand [3], you will find that the UpdateMessageAsync method on the queue is called with that same 10 minute renewal.

    The LinearSpeedupStrategy will renew again after 5 minutes, unless the renewal failed in which case it will try again after 1 minute (as defined in QueueListener).

    [1] Azure Storage Queue and multiple WebJobs instances: will QueueTrigger set the message lease time on triggered?

    [2] https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-get-started/

    [3] https://github.com/Azure/azure-webjobs-sdk/blob/cfc875a7f00e595410c0603e6ca65537025490a9/src/Microsoft.Azure.WebJobs.Host/Queues/Listeners/UpdateQueueMessageVisibilityCommand.cs