Search code examples
azureazure-storageazure-functionsazure-queues

Azure Function Queue trigger: how to set time delay for dequeue message


I have an Azure Function which listens to azure queue and, for example, something wrong. It re-adds a message to the queue again. But after 5 times message will be moved to poison queue.

I want to re-add a message to queue with a delay. For example, retry thru 1 hour. Because my Azure Function works with external resource, which can be unavailable for now. I don't want to do retry 5 times during 10 seconds at all, I want to retry after 1 hour. Of course, I write my own implementation of it, but probably this feature already exists.


Solution

  • @4c74356b41 has pointed out the right way. The host.json settings for queue is what you are looking for.

    visibilityTimeout is The time interval between retries when processing of a message fails maxDequeueCount is The number of times to try processing a message before moving it to the poison queue.

    {
        "version": "2.0",
        "extensions": {
            "queues": {
                "visibilityTimeout" : "01:00:00",
                "maxDequeueCount": 2
            }
        }
    }
    

    If your function is v1, similarly

    {
        "queues": {
          "visibilityTimeout" : "01:00:00",
          "maxDequeueCount": 2
        }
    }
    

    Update

    Since the problem is mainly about changing visibilityTimeout according to specific situation, setting the delay of CloudQueue.AddMessageAsync accordingly is the only way. Actually the visibilityTimeout does exactly the same thing but on the function app level(all queue), so we don't need to insist on it in this case.