Azure Durable Functions - 连接多个 CreateTimer 任务

Azure Durable Functions- concatenate multiple CreateTimer Tasks

我有这个功能:

[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;
        }

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

非常感谢。

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

看起来让您感到困惑的是,Orchestrator 每次在 IDurableOrchestrationContext API 方法之一上调用 await 时都会运行 replay functions。这就是 Durable Functions 如何实现其名称背后的 "durability"。

这对于新开发人员来说可能非常混乱,尤其是在日志记录方面。我建议使用我们的回放安全记录器,它不会记录之前回放中已经记录的消息。您可以通过将以下内容添加到编排函数的顶部来实现。

log = context.CreateReplaySafeLogger(log);

您可以通过使用 extended sessions 来部分抑制这种重放行为,这在功能上允许编排从它停止的地方恢复,假设它在 return 上调用 await 的消息及时时尚。