如果 Azure 计时器函数的执行持续时间长于触发周期,会发生什么情况?

What happens when an Azure Timer function has an execution duration that's longer shorter than the trigger period?

如果 Azure 函数执行了 10 分钟,但有 5 分钟的周期,触发器是否仍会在 5 分钟标记处触发?

回答这个问题最简单的方法是自己测试。

我创建了一个小函数,执行后只等待 90 秒。

计时器设置为每分钟 运行,因此,测试是查看函数是否仍然每分钟执行一次,或者是否延迟 30 秒。

日志显示您问题的答案是...

你可以看到它在排队等待另一个执行,因为一旦它完成,它就会再次开始。

您还会看到每次调用的开始时间都会延迟 30 秒,函数到 运行。

这是函数...

using System;

public static async Task Run(TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

    await Task.Delay(90000);
}

监控日志

调用

我尝试过的解决方法之一是:

host.json中,给定函数超时明确地在本地测试:

{
  "version": "2.0",

  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
    "functionTimeout": "00:05:00"
}

在 Azure Functions 中,每 10 分钟安排一次计时器触发器:

  public void Run([TimerTrigger("0 */10 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function1 executed at: {DateTime.Now}");

即使 运行 在 Azure Cloud(消费模式)中,也没有发生超时,因为计时器仍然是 运行每 10 分钟 ning 一次:

MS Doc and an Azure blog article 中所述,Azure 函数请求超时(这意味着您的业务逻辑应在函数超时发生之前完成,因此在每个计划中您都可以 运行 相同使用定时器触发器的逻辑)。