简体   繁体   English

计时器 function 块 HttpTrigger Azure function ap

[英]Timer function blocks HttpTrigger Azure function ap

I have 1 function app project, with a Timer trigger and a http trigger.我有 1 个 function 应用程序项目,带有一个定时器触发器和一个 http 触发器。 (using DI or not, does not seem to matter) (是否使用 DI,似乎并不重要)

The timertrigger:定时器触发器:

public static class Timer
{
    [FunctionName("Timer")]
    public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup =true)]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        Thread.Sleep(10000);//this is just for demonstrating the behavior!!

        log.LogInformation($"C# Timer trigger function executed done");
    }
}

the http trigger: http 触发器:

public static class Function1
{
    [FunctionName("test2")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
        ILogger log)
    {

        return new OkObjectResult("Done");
    }
}

The issue is, that when the timer runs, all requests are blocked.问题是,当计时器运行时,所有请求都被阻止。 Resulting in "Error: connect ECONNREFUSED 127.0.0.1:7071"导致“错误:连接 ECONNREFUSED 127.0.0.1:7071”

I read here and there (mostly in comments) that multiple triggers are (were) not supported, but that doesn't make sense to me as everything seems prepared for it anyway (i mean: i could not find any conclusive documentation about it, VS doesnt throw ANY resistance to add multiple, and even the debug interface and azure interface nicely sums up all the functions it could find...)我在这里和那里(主要是在评论中)读到不支持多个触发器,但这对我来说没有意义,因为无论如何一切似乎都已准备就绪(我的意思是:我找不到关于它的任何结论性文档, VS 不会抛出任何阻力来添加多个,甚至调试界面和 azure 界面也很好地总结了它能找到的所有功能......)

Is this behaviour expected, or is there some setting i can change to make it run in parallel or so??这种行为是预期的,还是我可以更改某些设置以使其并行运行?

This behavior is expected, since you're completely halting the Azure Functions process by running a Thread.Sleep() .此行为是预期的,因为您通过运行Thread.Sleep()完全停止了 Azure Functions 进程。 As long as your Function App hasn't scaled to multiple instances, there's only one process running your Functions.只要您的 Function 应用程序没有扩展到多个实例,就只有一个进程在运行您的函数。 This is where working asynchronously comes in.这就是异步工作的用武之地。

If you change your timer triggered function to this, it should work fine.如果您将计时器触发 function 更改为此,它应该可以正常工作。

[FunctionName("Timer")]
    public static async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup=true)] TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        await Task.Delay(10000);

        log.LogInformation($"C# Timer trigger function executed done");
    }

Next to this, please be advised:接下来,请注意:

  • It is not recommended to have your timer triggered Function RunOnStartup set to true建议触发计时器 Function RunOnStartup设置为true
  • It is not advised to explicitly build in delays in Functions建议在函数中显式构建延迟
  • Multiple triggers for one Function are not supported, having multiple Functions each with their own trigger within one Function App is supported.不支持一个 Function 的多个触发器,支持在一个 Function 应用程序中具有多个功能,每个功能都有自己的触发器。
  • A Function App is a collection of one or more Functions Function App是一个或多个Functions的集合

Triggers are what cause a function to run.触发器是导致 function 运行的原因。 A trigger defines how a function is invoked and a function must have exactly one trigger.触发器定义如何调用 function,并且 function 必须只有一个触发器。 Triggers have associated data, which is often provided as the payload of the function.触发器具有关联数据,通常作为 function 的有效负载提供。

Source: Azure Functions triggers and bindings concepts来源: Azure 函数触发器和绑定概念

EDIT:编辑:
Tested it, and this also occurs when the Function is async , and also when it's running on Azure. This means the initial run of the Function with runAtStartup=true always blocks any other incoming requests, and with runOnStartup enabled the trigger is invoked whenever your function app is scaled.对其进行了测试,当 Function 为async时以及它在 Azure 上运行时也会发生这种情况。这意味着 Function 的初始运行与runAtStartup=true始终阻止任何其他传入请求,并且启用runOnStartup时调用触发器function 应用程序已缩放。

This turns out to be because of RunOnStartup .事实证明这是因为RunOnStartup Any scheduled runs after the initial one do not have this issue.在初始运行之后的任何计划运行都没有此问题。

However, if RunOnStartup = false , the trigger still might be called on startup and then it does have the issue.但是,如果RunOnStartup = false ,触发器仍然可能在启动时被调用,然后它确实有问题。

As stated in the comments, async does not change anything (I actually use async and DI in the real scenario, but simplified the example here.)如评论中所述, async不会改变任何东西(我在实际场景中实际上使用了async和 DI,但在这里简化了示例。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM