Azure Function retry policy does nothing

Gijs Romme 50 Reputation points
2023-05-03T07:29:27.17+00:00

Hi,

I have an Azure Function app running that queries data from an API and stores it in a database every night using a Timer trigger. The API unfortunately does not have a bulk querying option, so I have to make over 200 requests consecutively to retrieve all the required data. Usually this works fine but every now and then, the function does not completely finish because of an error somewhere in the querying process. For this purpose, I tried to implement a retry policy in the 'function.json' file as follows:

{  
	"scriptFile": "__init__.py",  
	"bindings": [    
		{      
			"name": "mytimer",      
			"type": "timerTrigger",      
			"direction": "in",      
			"schedule": "0 59 21,22 * * *"    
		}  
	],  
	"retry": {      
		"strategy": "exponentialBackoff",      
		"maxRetryCount": 5,      
		"minimumInterval": "00:00:10",      
		"maximumInterval": "00:15:00"  
	}
}

This does nothing, however. I don't see any retries happening ever. Am I missing something or is this functionality just not working correctly at the moment?

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

Accepted answer
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-05-03T20:37:36.1+00:00

    the retry policy you defined will only be applied when there is an unhandled exception within your function code. If the error is being caught and handled within your code, the Azure Functions runtime will not consider it as a failure, and the retry policy won't be triggered.

    To ensure that the retry policy is triggered when there is an error in the querying process, you should let the function throw an exception when an error occurs

    for example

    def main(mytimer: func.TimerRequest) -> None:
        # Your logic to query the API and store data in the database
        try:
            # Your code to query the API and store data in the database
        except Exception as e:
            logging.error(f"An error occurred while querying the API: {e}")
            raise
    
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2023-05-08T11:42:58.7433333+00:00

    @Gijs Romme Thanks for reaching out.

    Azure Functions provides built-in retry behaviors for individual trigger extensions and retry policies provided by the Functions runtime. Starting with version 3.x of the Azure Functions runtime, you can define retry policies for Timer triggers that are enforced by the Functions runtime. The retry policy you have defined in the function.json file is correct. However, the retry policy is only enforced when the function fails due to a transient error. If the function fails due to a non-transient error, such as a syntax error in your code, the retry policy will not be enforced. You can use the retry_context object to determine whether the function is being retried and to access the current retry count.

    Hope the above clears things up. Feel free to get back to me if you have any queries or concerns.


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.