Azure Durable Functions provides a rich set of Error Handling APIs. This post shows how Activities or Sub-Orchestrations can be re-run with the different retry options.
Activities in a workflow can call an API or run a code flow which might fail due to connection problems, network timeouts or other similar problems. If it was run a second time, it might succeed, and the flow could then complete successfully. For this, a retry Error Handling can be implemented. The HttpClient in .NET Core can implement this by using Polly. Azure Durable functions supports this directly without requiring extra Nuget packages. This can also be used to retry whole sub-orchestrations and not just single Http API calls.
Code: https://github.com/damienbod/AzureDurableFunctions
Posts in this series
- Using External Inputs in Azure Durable functions
- Azure Functions Configuration and Secrets Management
- Using Key Vault and Managed Identities with Azure Functions
- Waiting for Azure Durable Functions to complete
- Azure Durable Functions Monitoring and Diagnostics
- Retry Error Handling for Activities and Orchestrations in Azure Durable Functions
Retrying Azure Durable Function Activities.
The IDurableOrchestrationContext interface provides the Retry Error Handling APIs in Azure Durable Functions. This interface is added as a parameter in the Azure Function using the OrchestrationTrigger.
[FunctionName(Constants.MyOrchestration)] public async Task<MyOrchestrationDto> RunOrchestrator( [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log) {
The RetryOptions class provides the configuration for the different Retry calls. This can be used to set max timeouts, a fall off for retrying, or whatever configuration you might require.
The CallActivityWithRetryAsync method can then be called and if the Activity being called fails, the retry options will be used to re-run the activity until the max has be reached or it succeeds. If the activity continues to throw exceptions, the Durable function will complete with a Failed status, unless otherwise set.
var retryOptions = new RetryOptions( firstRetryInterval: TimeSpan.FromSeconds(3), maxNumberOfAttempts: 5) { BackoffCoefficient = 1.5 }; var myActivityThreeResult = await context.CallActivityWithRetryAsync<string> ( Constants.MyActivityThree, retryOptions, context.GetInput<string>() );
Retrying Azure Durable Function Sub-Orchestrations.
Polly provides the same functionality when calling APIs. What’s nice about retry in Azure Durable Functions is that the Retry Error Handling can be applied to complete Orchestrations. If any part of the sub-orchestration fails, the whole thing can be re-run using this error handling. This can be implemented using the CallSubOrchestratorWithRetryAsync method.
var mySubOrchestrationDto = await context.CallSubOrchestratorWithRetryAsync<MySubOrchestrationDto> ( Constants.MySecondOrchestration, retryOptions, myActivityOne );
If we add an Exception one on the activities, we can see the retry APIs running and view the status of the orchestration running. (You need to set the showhistory=true)
Links
https://github.com/scale-tone/DurableFunctionsMonitor
https://www.npmjs.com/package/azure-functions-core-tools
https://damienbod.com/2018/12/23/using-azure-key-vault-with-asp-net-core-and-azure-app-services/
https://docs.microsoft.com/en-us/azure/azure-functions/durable/
https://github.com/Azure/azure-functions-durable-extension
https://damienbod.com/2019/03/14/running-local-azure-functions-in-visual-studio-with-https/
Microsoft Azure Storage Explorer
Microsoft Azure Storage Emulator
Install the Azure Functions Core Tools
[…] Retry Error Handling for Activities and Orchestrations in Azure Durable Functions (Damien Bowden) […]
[…] Retry Error Handling for Activities and Orchestrations in Azure Durable Functions – Damien Bowden […]
Hi, I have been trying to find some documentation on what happens with a Durable function execution if the request that triggered it times out, but there are no errors in the function itself? For example, a Power Automate Flow sending an HTTP request to a durable function, and that request action times out due to hitting the configured timeout limit, does the Durable function keep going, or is it cancelled? In this scenario, I can’t find any trace of this function execution in Application Insights, and can’t tell if the function did its job or was cancelled.
Hi JD this will be persisted in the storage, you can check this using azure functions monitor, or just check the storage yourself. With the monitor you can then re-run cancel or whatever.
Greetings Damien