【问题标题】:Azure Durable Function - counter in orchestrationAzure 持久功能 - 编排中的计数器
【发布时间】:2019-09-12 18:04:58
【问题描述】:

我正在基于monitor pattern 构建一个持久函数。我有下面的代码,我的问题是关于我用于简单指数重试回退的计数器变量。

[FunctionName("RequestOrchestrator")]
public static async Task RequestOrchestrator(
    [OrchestrationTrigger] DurableOrchestrationContext monitorContext, ILogger log)
{
    DateTime endTime = monitorContext.CurrentUtcDateTime.AddHours(1);
    int counter = 0;

    while (monitorContext.CurrentUtcDateTime < endTime)
    {
        var result = await monitorContext.CallActivityAsync<bool>("GetStatusExternal", "test");

        if (result)
        {
            // all ok
            break;
        }
        else
        {
            counter++;
            // Wait for the next checkpoint with exponential backoff
            var nextCheckpoint = monitorContext.CurrentUtcDateTime.AddSeconds(5 * counter);
            if (!monitorContext.IsReplaying)
            {
                log.LogInformation($"Next check at {nextCheckpoint}.");
            }

            await monitorContext.CreateTimer(nextCheckpoint, CancellationToken.None);
        }
    }
}

计数器的使用是这样还是counter++需要进入

if (!monitorContext.IsReplaying)
  counter++;

replay-safe

【问题讨论】:

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


    【解决方案1】:

    没有。你不需要monitorContext.IsReplaying 检查counter++
    您只需要对只希望运行一次的语句进行此检查,例如日志记录(如在您的代码中)、对外部系统的状态更新等。

    为了重放安全,您基本上只需要您的代码是确定性的。因此,任何不能在Pure Function 中构成的代码都必须移到它们自己的活动函数中。其他都行。

    如文档所述,任何随时间变化(重放时间)的代码,如基于时间的生成器、来自外部 API 的远程数据等,都必须在活动函数中。

    【讨论】:

    • 感谢您的解释!所以我使用计数器进行指数回退的方式确实可以?!
    • 是的。这是正确的。您可能希望根据用例设置等待时间的上限。
    猜你喜欢
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多