Azure Durable Function 错误处理:有没有办法确定您正在进行的重试?

Azure Durable Function error handling: Is there a way to identify which retry you are on?

Durable Functions documentation 指定以下模式以设置在 activity 函数中引发异常时自动处理重试:

public static async Task Run(DurableOrchestrationContext context)
{
    var retryOptions = new RetryOptions(
        firstRetryInterval: TimeSpan.FromSeconds(5),
        maxNumberOfAttempts: 3);

    await ctx.CallActivityWithRetryAsync("FlakyFunction", retryOptions, "ABC");

    // ...
}

但是我看不到在 activity 函数中检查您要重试的方法:

[FunctionName("FlakyFunction")]
public static string[] MyFlakyFunction(
    [ActivityTrigger] string id, 
    ILogger log)
{
    // Is there a built-in way to tell what retry attempt I'm up to here?
    var retry = ??

    DoFlakyStuffThatMayCauseException();
}

编辑:我知道可以通过将某种计数混入RetryOptions.Handle 委托来处理它,但这是一个糟糕的解决方案。它可以通过每次执行时维护外部状态来手动处理,但考虑到有一个内部重试计数,我只是想知道是否有任何方法可以访问它。主要用途是调试和日志记录,但我可以想到许多其他用途。

似乎没有办法识别重试。 Activity 函数不知道状态和重试。当 CallActivityWithRetryAsync 调用时,DurableOrchestrationContext 调用 ScheduleWithRetry method of the OrchestrationContext class inside the DurableTask framework:

public virtual Task<T> ScheduleWithRetry<T>(string name, string version, RetryOptions retryOptions, params object[] parameters)
{
    Task<T> RetryCall() => ScheduleTask<T>(name, version, parameters);
    var retryInterceptor = new RetryInterceptor<T>(this, retryOptions, RetryCall);
    return retryInterceptor.Invoke();
}

RetryInterceptor class 上的 Invoke 方法被调用,并在最大重试次数上执行 foreach 循环。此 class 不公开获取重试次数的属性或方法。

另一个有助于调试的解决方法是在 activity 函数中记录语句。当你在本地 运行 它时,你可以在那里放置一个断点,看看它在那里停止的频率。请注意,已经有一个 feature request 来处理更好的重试日志记录。如果您觉得更合适,您可以在那里添加您的反馈或提出新问题。

老实说,我认为 activity 不知道状态并重试是件好事。这应该是协调者的责任。但是,如果您可以获得一些关于重试的统计数据以查看性能是否有下降趋势,这将很有用。