在Azure Functions编排中主动进行的活动重试

时间:2020-07-19 18:27:41

标签: azure-functions orchestration azure-durable-functions

我有一个长期运行(几分钟)的工作,并且将HTTP触发器实现为Azure Functions业务流程:

[FunctionName("ManualImportOrders")]
public async Task<HttpResponseMessage> HttpStart(
    [HttpTrigger(AuthorizationLevel.Function, "POST")] HttpRequestMessage req,
    [DurableClient] IDurableOrchestrationClient starter,
    ILogger log)
{
    var dtoInResult = await ReadDtoInAsync(req);
    if (!dtoInResult.IsSuccess)
    {
        return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(dtoInResult.Error) };
    }

    string instanceId = await starter.StartNewAsync("ManualImportOrders_Orchestrator", dtoInResult.Value);

    log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

    return starter.CreateCheckStatusResponse(req, instanceId);
}

[FunctionName("ManualImportOrders_Orchestrator")]
public async Task<object> RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var dtoIn = context.GetInput<ManualImportOrdersDtoIn>();

    var result = await context.CallActivityAsync<object>("ManualImportOrders_RunJob", dtoIn);

    return result;
}

[FunctionName("ManualImportOrders_RunJob")]
public async Task<object> RunManualAsync([ActivityTrigger] IDurableActivityContext context, ILogger logger, CancellationToken cancellationToken)
{
    var dtoIn = context.GetInput<ManualImportOrdersDtoIn>();
    logger.LogInformation("Invoking manual order import");
    var result = ... call to business logic ...
    logger.LogInformation("Manual order import completed");
    return result.Output;
}

上面有三个功能:

  • ManualImportOrders调用业务流程。
  • ManualImportOrders_Orchestrator调用单个活动。
  • ManualImportOrders_RunJob是一项活动,它会进行长期运行的处理。

ManualImportOrders_RunJob活动功能由于未处理的异常而失败时,我希望整个编排失败。当确实使用AF Tools在本地运行时,会发生这种情况。但是,在Azure中运行时,似乎有某种重试机制,因为活动功能是使用相同的实例ID再次运行的。

例如,第一次绿色跑步成功。在第二次红色运行中,活动功能失败并一遍又一遍地被召回:

Functions log excerpt

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我试图找到durable function是否有默认的重试机制,但我没有找到它,对此现象我也很困惑。

但是我认为您可以指定自动重试策略,这可以减少对活动功能的调用次数。设置重试策略的语法是:

[FunctionName("TimerOrchestratorWithRetry")]
public static async Task Run([OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var retryOptions = new RetryOptions(
        firstRetryInterval: TimeSpan.FromSeconds(5),
        maxNumberOfAttempts: 3);

    await context.CallActivityWithRetryAsync("FlakyFunction", retryOptions, null);

    
}

您可以将maxNumberOfAttempts的值设置为最小值。希望对您有帮助。

有关更多详细信息,请参阅此官方document