Search code examples
c#azureazure-functionsazureservicebus

Azure functions Queue binding retry via clientRetryOptions and host.json not working


I'm using isolated process as per https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus?tabs=isolated-process%2Cextensionv5%2Cextensionv3&pivots=programming-language-csharp#hostjson-settings

I need to implement some delay between retries ideally via the visibility delay of messages on the queue. Thread.Sleep after an exception does allow a delay but blocks the processing of the other messages.

I would prefer to avoid durable functions

When setting clientRetryOptions via the host.json file, which I believe should apply here, I do not see any effect for example with fewer max retries or larger delay.

Edit: Is this setting only for the connection to Azure service bus? Perhaps I need some interaction with initialvisibilitydelay?

    {
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "clientRetryOptions": {
        "mode": "fixed",
        "tryTimeout": "00:03:00",
        "delay": "00:00:05.00",
        "maxDelay": "00:01:00",
        "maxRetries": 2
      }
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

packages

        [Function("QueueTest")]
    public void Test([ServiceBusTrigger("test", Connection = "ServiceBusConnection")] string item,
     FunctionContext context)
    {
        StringBuilder log = new StringBuilder();
        log.AppendLine("Created at " + DateTime.Now);
        MessagingContext.ProcessedMessages.Add(new ProcessedMessage
        {
            Interface = "QueueTest",
            CreatedDate = DateTime.Now,
            Status = "Error",
            MessageId = "Test",
            QueueMessageId = "Test",
            Details = log.ToString()
        });
        MessagingContext.SaveChanges();
        throw new InvalidOperationException("Pretend failure");
    }

Solution

  • Azure Functions does not provide a retry policy for Service Bus trigger and relies on the native Service Bus retries. The native tries with Service Bus does not allow retry with a delay.

    There's a feature request to support abandoning messages with a custom delay, but it's not out yet. An alternative would be to receive a message, clone it, and send it with a delay while completing the original message but that would require the ability to invoke service operations (complete a message, send a message), which is not possible yet either. Here's a tracking issue as the feature is work in progerss.