Azure Durable Function - 最终重试逻辑
Azure Durable Function - final retry logic
我正在构建一个持久函数,如果 activity 在 maxNumberOfAttempts
之后失败,我需要创建某种警报
调试时,我看到 DurableOrchestrationContext class 有一个名为 History
的 属性,但我无法访问它,因为它是内部的。
此时我能想到的唯一选择是使用 Azure Monitor group/summarize 通过 ExecutionId 记录日志。我不喜欢这种方法,因为 Azure Monitor 不应该知道或关心函数中配置的 maxNumberOfAttempts
。
是否还有其他我缺少的方法可以做到这一点?
我能够根据@peter-bons 提供的信息解决这个问题:
When retrying did not result in any successful call to the function a
FunctionFailedException
is thrown.
下面是代码示例:
[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);
}
}
}
我正在构建一个持久函数,如果 activity 在 maxNumberOfAttempts
调试时,我看到 DurableOrchestrationContext class 有一个名为 History
的 属性,但我无法访问它,因为它是内部的。
此时我能想到的唯一选择是使用 Azure Monitor group/summarize 通过 ExecutionId 记录日志。我不喜欢这种方法,因为 Azure Monitor 不应该知道或关心函数中配置的 maxNumberOfAttempts
。
是否还有其他我缺少的方法可以做到这一点?
我能够根据@peter-bons 提供的信息解决这个问题:
When retrying did not result in any successful call to the function a
FunctionFailedException
is thrown.
下面是代码示例:
[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);
}
}
}