【问题标题】:Azure Durable Function - final retry logicAzure 持久函数 - 最终重试逻辑
【发布时间】:2021-06-04 12:41:04
【问题描述】:

我正在构建一个持久函数,如果活动在maxNumberOfAttempts 之后失败,我需要创建某种警报

调试时,我看到 DurableOrchestrationContext 类有一个名为 History 的属性,但我无法访问它,因为它是内部的。

此时我能想到的唯一选择是使用 Azure Monitor 按 ExecutionId 对日志进行分组/汇总。我不喜欢这种方法,因为 Azure Monitor 不应该知道或关心函数中配置的 maxNumberOfAttempts

还有其他我想念的方法吗?

【问题讨论】:

  • 当重试没有成功调用函数时,会抛出FunctionFailedException。应该记录那个,你可以根据它创建一个警报。

标签: c# azure-functions azure-durable-functions


【解决方案1】:

根据@peter-bons 提供的信息,我能够弄清楚这一点:

当重试没有成功调用函数时 FunctionFailedException 被抛出。

下面是代码示例:

[FunctionName("MyDurableFunctionOrchestrator")]
public async Task RunOrchestrator(
    [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
{
    var retryOptions = new RetryOptions(TimeSpan.FromMinutes(1), 2)
    {
        BackoffCoefficient = 2
    };

    var request = context.GetInput<UserRequest>();         

    var tasks = new List<Task>
    {
        context.CallActivityWithRetryAsync(nameof(Example1Activity), retryOptions, request),
        context.CallActivityWithRetryAsync(nameof(Example2Activity), retryOptions, request)
    };

    try
    {
        await Task.WhenAll(tasks);
    }            
    catch(FunctionFailedException e)            
    {
        // Azure Monitor will alert on this
        log.LogError(e, "Critical Error: {identity}", request.Identity);
        
        // this will tell us all the tasks that failed
        foreach(var task in tasks.FindAll(t => t.IsFaulted))
        {                    
            log.LogError(task.Exception, "Failed activity for '{identity}'", request.Identity);    
        }                
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2022-10-20
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2016-11-08
    • 2021-09-24
    相关资源
    最近更新 更多