RunOrchestrator([Orc-6ren">
gpt4 book ai didi

Azure Durable Functions - 连接多个 CreateTimer 任务

转载 作者:行者123 更新时间:2023-12-03 02:39:38 25 4
gpt4 key购买 nike

我有这个功能:

[FunctionName("MultipleTimersOrchestrator")]
public async Task<List<string>> RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
{
var outputs = new List<string>();

log.LogInformation($"Starting process {DateTime.Now}");

DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromSeconds(5));
await context.CreateTimer(deadline, CancellationToken.None);

log.LogInformation($"Starting GetUser Tokyo {DateTime.Now}");
outputs.Add(await context.CallActivityAsync<string>("GetUser", "Tokyo"));
log.LogInformation($"Ended GetUser Tokyo {DateTime.Now}");

DateTime deadline2 = context.CurrentUtcDateTime.Add(TimeSpan.FromSeconds(10));
await context.CreateTimer(deadline2, CancellationToken.None);

log.LogInformation($"Starting GetUser Seattle {DateTime.Now} ");
await context.CallActivityAsync<string>("GetUser", "Seattle");
log.LogInformation($"Ended GetUser Seattle {DateTime.Now}");


return outputs;
}

我的问题是它表现得很奇怪...是否有可能在一个 Orchestrator 函数中等待多个计时器?从日志来看,整个函数似乎在 5 秒后执行,然后在 10 秒后执行。我期望它像 Task.Delay() 一样工作这是正确的吗?有人可以解释一下吗?

非常感谢。

最佳答案

需要澄清的是,使用多个创建计时器调用是一种非常有效的模式,它的功能与 Task.Delay() 非常相似,尽管是在多个函数执行的过程中。

看来让您感到困惑的是 Orchestrator 的功能 replay functions每次他们对 IDurableOrchestrationContext API 方法之一调用 await 时。这就是 Durable Functions 实现其名称背后的“耐用性”的方式。

这对于新开发人员来说可能非常令人困惑,尤其是日志记录方面。我建议使用我们的重播安全记录器,它不会记录之前重播中已记录的消息。您可以通过将以下内容添加到编排函数的顶部来做到这一点。

log = context.CreateReplaySafeLogger(log);

您可以使用extended sessions部分抑制这种重播行为。 ,它在功能上允许编排从中断处恢复,假设它调用 await 的消息及时返回。

关于Azure Durable Functions - 连接多个 CreateTimer 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61791640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com