Which Azure Function Support In-built retry policy

Satyam Chauhan 467 Reputation points
2024-02-08T06:56:46.06+00:00

Hi, I am using cosmos DB trigger, timer trigger and Event HUb trigger azure functions. I want to implement retry mechanism in the function. After going through the article - https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages?tabs=exponential-backoff%2Cin-process&pivots=programming-language-csharp#retries, it is stated that not all functions supports retry policy by default and it has to be implemented. Please let me know for which functions we need to implement retry on our own.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,018 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Dronec 161 Reputation points
    2024-02-08T10:03:06.94+00:00

    Sure, here it is:

    [FunctionName("your-function")]
    public static async Task Run.....
     var retryCount = 0; // Retries counter
     while (true)
     {
         try
         {
    ... whatever you do in your function
    }
     catch (Exception ex)
     {
         System.Threading.Thread.Sleep(10000);
         retryCount++;
         if (retryCount >= 5)
         {
             log.LogError($"Error {ex.message} happened, {retryCount} attempt.");
             throw;
         }
    else
    log.LogInformation($"Error {ex.message} happened, {
     }
    
    0 comments No comments

  2. Mike Urnun 9,236 Reputation points Microsoft Employee
    2024-02-08T17:57:44.2233333+00:00

    Hello @Satyam Chauhan - Thanks for posting on the MS Q&A & engaging the community here. AFAIK, among the CosmosDB, Timer, and Event Hubs triggers that you named, TimerTrigger is the only one where the Retry policy behavior opts to follows:User's image

    When it runs again next time per schedule, the timer object passed into the function will have its isPastDue set to true:

    {
        "Schedule":{
            "AdjustForDST": true
        },
        "ScheduleStatus": {
            "Last":"2016-10-04T10:15:00+00:00",
            "LastUpdated":"2016-10-04T10:16:00+00:00",
            "Next":"2016-10-04T10:20:00+00:00"
        },
        "IsPastDue":true
    }
    

    For CosmosDB and Event Hubs triggers, you should be able to implement the built-in FixedDelay and ExponentialBackOff policies accordingly. Hope this helps. Feel free to tag me in the comments below if you have any follow-up questions.


    Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.

    0 comments No comments