Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry mechanism for NET Isolated Durable Azure Functions

I am migrating a NET6-based in-process function app to a NET7-based isolated function app. It uses Durable Functions, which have changed quite a bit in its API. I need to call an activity function that needs to be retried if the error message contains a keyword.

Previously, with 5 retries, 5 mins apart, this was:

var retryOptionsOld = new RetryOptions(TimeSpan.FromMinutes(5), 5)
{
    Handle = ex => ex.Message.Contains("KEYWORD")
};
await ctx.CallActivityWithRetryAsync("activityName", null, retryOptionsOld)

Now, we need to pass TaskOptions to the activity function. However, I don't seem to be able to pass an error message condition AND a time limit condition at the same time. It seems that I can only do one or the other.

var retryOptionsNew1 = TaskOptions.FromRetryHandler(ctx => ctx.LastFailure.ErrorMessage.Contains("KEYWORD"));
var retryOptionsNew2 = TaskOptions.FromRetryPolicy(new RetryPolicy(5, TimeSpan.FromMinutes(5)));

await ctx.CallActivityAsync("activityName", null, retryOptionsNew1); //or...New2

How can I build something like before, combining time parameters and a handler condition?

like image 799
baouss Avatar asked Sep 15 '25 18:09

baouss


1 Answers

You can use the constructor of TaskOptions that takes the RetryPolicy.

var retryPolicy= new RetryPolicy(
    maxNumberOfAttempts: 3, 
    firstRetryInterval: TimeSpan.FromSeconds(5))
{
    HandleAsync = c => Task.FromResult(c.Message.StartsWith("err"))
};
var taskOptions = new TaskOptions(retryPolicy);
like image 81
404 Avatar answered Sep 17 '25 19:09

404