gpt4 book ai didi

c# - 通过 TimerTrigger 在连续作业上使用 Azure WebJob 超时

转载 作者:行者123 更新时间:2023-12-03 01:47:46 24 4
gpt4 key购买 nike

我有一个连续的 WebJob,其函数使用 TimerTrigger 每 30 秒运行一个进程。函数中的特定调用偶尔会看似随机挂起,导致 Web 作业无限期等待。当前的解决方案是通知服务已停止,然后登录到 Azure 仪表板并手动中止它。

请注意,我知道正确的行动方针是找出根本原因并解决它。相信我,我们正在努力。同时,我想治疗症状,并且需要帮助。

我正在尝试让 WebJob 使用超时装饰器检测状态,如 Azure WebJobs SDK 上的这篇文章中所述:https://github.com/Azure/azure-webjobs-sdk/issues/590 。实现该建议后,我可以看到,当有问题的调用挂起时,会检测到超时,但 WebJob 仍然不会终止。我在这里做错了什么,不会杀死该函数以允许后续调用?

程序.cs

static void Main()
{
var config = new JobHostConfiguration();
config.UseTimers();
config.FunctionTimeout = new TimeSpan(0, 15, 0);
var host = new JobHost(config);

Functions.Initialize();
host.RunAndBlock();
}

函数.cs

[Singleton]
[Timeout("00:05:00")]
public async static Task PeriodicProcess([TimerTrigger("00:00:30", RunOnStartup = true)] TimerInfo timer, CancellationToken cancelToken, TextWriter log)
{
log.WriteLine("-- Processing Begin --");

List<Emails> cases = GetEmailsAndWhatNot();
foreach (Email e in Emails)
{
try
{
ProblematicFunction_SendEmail(e, log);
}
catch(Exception e)
{
// do stuff
}
}
log.WriteLine("-- Processing End -- ");
}


public static void ProblematicFunction_SendEmail(Email e, TextWriter log)
{
// send email
}

问题期间的 WebJob 输出

-- Processing Begin --

Timeout value of 00:05:00 exceeded by function 'Functions.PeriodicProcess' (Id: '0f7438bd-baad-451f-95a6-9461f35bfb2d'). Initiating cancellation.

尽管网络作业发起取消,但该函数并没有终止。我需要监控 CancellationToken 吗?我需要将异步调用传播到多远?我在这里缺少什么实际上会中止该过程?

最佳答案

TimerTrigger关于 TimerTrigger 的说明:

单例锁

TimerTrigger uses the Singleton feature of the WebJobs SDK to ensure that only a single instance of your triggered function is running at any given time.

日程安排

If your function execution takes longer than the timer interval, another execution won't be triggered until after the current invocation completes. The next execution is scheduled after the current execution completes.

这是我针对这个场景的测试,你可以引用一下:

  • 使用CancellationToken.None并且从不传播取消 token

    enter image description here

    注意:函数PeriodicProcess会在30秒后超时,但耗时作业仍在运行,并且在长时间运行的作业完成后,将打印处理结束日志。

  • 传播取消 token

    enter image description here

    注意:如果我们传播取消 token ,耗时作业将立即被取消。

代码片段

[Timeout("00:00:30")]
[Singleton]
public async static Task PeriodicProcess([TimerTrigger("00:00:10", RunOnStartup = true)] TimerInfo timer, CancellationToken cancelToken, TextWriter log)
{
log.WriteLine($"-- [{DateTime.Now.ToString()}] Processing Begin --");

try
{
await longRunningJob(log, cancelToken);
}
catch (Exception e)
{
// do stuff
}
log.WriteLine($"-- [{DateTime.Now.ToString()}] Processing End -- ");
}


private async static Task longRunningJob(TextWriter log, CancellationToken cancelToken)
{
log.WriteLine($"-- [{DateTime.Now.ToString()}] Begin Time-consuming jobs --");
await Task.Delay(TimeSpan.FromMinutes(1), cancelToken);
log.WriteLine($"-- [{DateTime.Now.ToString()}] Complete Time-consuming jobs --");
}

关于c# - 通过 TimerTrigger 在连续作业上使用 Azure WebJob 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44205309/

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