Custom retry - Azure Service Bus trigger for Azure Functions

Sanja Stojkova 16 Reputation points
2022-12-13T12:43:27.71+00:00

We have Azure service bus trigger for azure function, however if the msg fails we want to retry(5 times with some delay) and lock the msg for certain time(one minute) so it cant be processed;
We were using smth like this

 "retry": {  
        "strategy": "exponentialBackoff",  
        "maxRetryCount": 5,  
        "minimumInterval": "00:00:10",  
        "maximumInterval": "00:15:00"  
    }  
}  

However the Retry policies are no longer supported for Service bus
(Retry policies going forward will only be supported for Timer and Event Hubs triggers. We've updated the docs for the retry policy GA here: https://learn.microsoft.com/azure/azure-functions/functions-bindings-error-pages#retries)
I am trying to implement simple retry logic within the function

  let triesCounter = 0;  
    while(triesCounter < 5){  
        try{  
            await mapper.process(message);  
        }  
        catch(ex){  
            logger.info(ex)  
            if (++triesCounter == 5) throw ex;  
        }  
        await sleep(50000);  
        triesCounter++;  
    }  

However I am not able to find any useful instructions on how to lock the msg for certain amount of time; How can i achieve that?
Any help would be much appreciated;

P.S already tried the following options, without any luck:

  1. "clientRetryOptions": {
    "mode": "exponential",
    "tryTimeout": "00:01:00",
    "delay": "00:00:00.80",
    "maxDelay": "00:01:00",
    "maxRetries": 3
    },
  2. const serviceBusClient = new ServiceBusClient(process.env.SERVICE_BUS_CONNECTION, {
    retryOptions: {
    maxRetries: 3,
    retryDelayInMs: 1000,
    timeoutInMs: 1000,
    mode: RetryMode.Fixed,
    maxRetryDelayInMs: 1000,
    },
    });
Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Arun Siripuram 911 Reputation points
    2022-12-22T17:53:13.443+00:00

    @Sanja Stojkova

    To implement a custom retry strategy for an Azure Service Bus trigger for an Azure Function, you can use the RetryOptions property of the ServiceBusTriggerAttribute. The RetryOptions property allows you to specify the number of retries, the delay between retries, and the maximum lock duration for messages that fail to process.

    Here is an example of how you can use the RetryOptions property to implement a custom retry strategy for an Azure Service Bus trigger:

    [FunctionName("MyFunction")]
    public static void Run([ServiceBusTrigger("myqueue", Connection = "ServiceBusConnectionString")] Message message, ILogger log, [ServiceBus("mydeadletterqueue", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] out Message deadLetterMessage, ExecutionContext context)
    {
    // Retry the message up to 5 times, with a delay of 30 seconds between retries, and a maximum lock duration of one minute.
    var retryOptions = new RetryOptions(TimeSpan.FromSeconds(30), 5, TimeSpan.FromMinutes(1));
    message.SystemProperties.RetryCount = retryOptions.MaxRetryCount;
    message.SystemProperties.LockedUntilUtc = DateTime.UtcNow.Add(retryOptions.MaxLockDuration);
    // Process the message here...
    }

    In this example, the function will retry the message up to 5 times, with a delay of 30 seconds between retries, and a maximum lock duration of one minute. If the message still fails to process after all retries have been attempted, it will be moved to the dead letter queue.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    3 people found this answer helpful.
    0 comments No comments

  2. Dan 0 Reputation points
    2023-01-26T12:49:45.7533333+00:00

    How can this be done using Python?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.