计时器函数块 Http 触发器 Azure 函数应用程序
Timer function blocks HttpTrigger Azure function ap
我有 1 个函数应用程序项目,带有一个定时器触发器和一个 http 触发器。 (用不用DI,好像无所谓)
定时器触发器:
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");
}
}
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");
}
}
问题是,当计时器 运行s 时,所有请求都被阻止。导致“错误:连接 ECONNREFUSED 127.0.0.1:7071”
我在这里和那里(主要是在评论中)读到不支持多个触发器,但这对我来说没有意义,因为无论如何一切似乎都已准备就绪(我的意思是:我找不到任何关于它的结论性文档,VS 不会抛出任何阻力来添加多个,甚至调试界面和 azure 界面也很好地总结了它能找到的所有功能...)
这种行为是预期的,还是我可以更改某些设置以使其 运行 左右并行??
此行为是预料之中的,因为您通过 运行ning Thread.Sleep()
完全停止了 Azure Functions 进程。只要您的 Function App 没有扩展到多个实例,就只有一个进程 运行ning 您的 Functions。这就是异步工作的用武之地。
如果您将定时器触发功能更改为此,它应该可以正常工作。
[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");
}
接下来请注意:
- 不建议您将定时器触发功能
RunOnStartup
设置为 true
- 不建议在函数中明确构建延迟
- 不支持一个函数的多个触发器,在一个函数应用程序中有多个函数,每个函数都有自己的触发器支持。
- 功能应用是一个或多个功能的集合
Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.
来源:Azure Functions triggers and bindings concepts
编辑:
对其进行了测试,当 Function 为 async
时也会发生这种情况,当它在 Azure 上为 运行ning 时也会发生这种情况。这意味着带有 runAtStartup=true
的函数的初始 运行 总是 阻止任何其他传入请求,并且启用 runOnStartup
时,只要您的函数调用触发器应用已缩放。
原来是因为RunOnStartup
。在初始运行之后的任何计划运行都没有此问题。
但是,如果 RunOnStartup = false
,触发器仍可能在启动时被调用,然后 确实 有问题。
正如评论中所述,async
不会改变任何东西(我在实际场景中实际上使用了async
和DI,但在这里简化了示例。)
我有 1 个函数应用程序项目,带有一个定时器触发器和一个 http 触发器。 (用不用DI,好像无所谓)
定时器触发器:
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");
}
}
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");
}
}
问题是,当计时器 运行s 时,所有请求都被阻止。导致“错误:连接 ECONNREFUSED 127.0.0.1:7071”
我在这里和那里(主要是在评论中)读到不支持多个触发器,但这对我来说没有意义,因为无论如何一切似乎都已准备就绪(我的意思是:我找不到任何关于它的结论性文档,VS 不会抛出任何阻力来添加多个,甚至调试界面和 azure 界面也很好地总结了它能找到的所有功能...)
这种行为是预期的,还是我可以更改某些设置以使其 运行 左右并行??
此行为是预料之中的,因为您通过 运行ning Thread.Sleep()
完全停止了 Azure Functions 进程。只要您的 Function App 没有扩展到多个实例,就只有一个进程 运行ning 您的 Functions。这就是异步工作的用武之地。
如果您将定时器触发功能更改为此,它应该可以正常工作。
[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");
}
接下来请注意:
- 不建议您将定时器触发功能
RunOnStartup
设置为true
- 不建议在函数中明确构建延迟
- 不支持一个函数的多个触发器,在一个函数应用程序中有多个函数,每个函数都有自己的触发器支持。
- 功能应用是一个或多个功能的集合
Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.
来源:Azure Functions triggers and bindings concepts
编辑:
对其进行了测试,当 Function 为 async
时也会发生这种情况,当它在 Azure 上为 运行ning 时也会发生这种情况。这意味着带有 runAtStartup=true
的函数的初始 运行 总是 阻止任何其他传入请求,并且启用 runOnStartup
时,只要您的函数调用触发器应用已缩放。
原来是因为RunOnStartup
。在初始运行之后的任何计划运行都没有此问题。
但是,如果 RunOnStartup = false
,触发器仍可能在启动时被调用,然后 确实 有问题。
正如评论中所述,async
不会改变任何东西(我在实际场景中实际上使用了async
和DI,但在这里简化了示例。)