Azure Durable Function - 编排中的计数器
Azure Durable Function - counter in orchestration
我正在基于 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?
没有。您不需要 monitorContext.IsReplaying
检查 counter++
.
您只需要对您想要的语句进行此检查 运行 就像日志记录(如在您的代码中)、对外部系统的状态更新等
为了确保重播安全,您基本上只需要您的代码是确定性的。因此,任何无法在 Pure Function 中构成的代码都必须移至它们自己的 activity 函数中。其他都行。
如文档所述,任何随时间(重播时间)变化的代码,如基于时间的生成器、来自外部 API 的远程数据等,都必须在 activity 函数中。
我正在基于 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?
没有。您不需要 monitorContext.IsReplaying
检查 counter++
.
您只需要对您想要的语句进行此检查 运行 就像日志记录(如在您的代码中)、对外部系统的状态更新等
为了确保重播安全,您基本上只需要您的代码是确定性的。因此,任何无法在 Pure Function 中构成的代码都必须移至它们自己的 activity 函数中。其他都行。
如文档所述,任何随时间(重播时间)变化的代码,如基于时间的生成器、来自外部 API 的远程数据等,都必须在 activity 函数中。