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

标签 c# azure azure-webjobs azure-webjobssdk

我有一个连续的 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/

相关文章:

r - 如何使用 Azure 通知中心和 R 向客户手机发送短信?

azure - Microsoft Azure 网站创建 : 'Cannot modify this site because another operation is in progress'

azure - Login-AzureRmAccount 和 Connect-AzureRmAccount 之间有什么区别?

.net-core - .NET Core 中的 Azure Web 作业

c# - 使用 xsd.exe (VS2010) 从 xsd 生成 C# 代码时出现 StackOverFlowException

c# - 使用 LinqToSql 生成返回数据库记录?

c# - 如何在 C# 中将未包含在 datagridview 中的其他数据库值显示到另一个表单中?

c# - 使用 MailMessage 和 SmtpClient 或 MessageQueue 延迟发送电子邮件

powershell - 如何通过 scm.azurewebsites.net REST API 更新 settings.job?

azure - 在 Kudu 中找不到 Azure webjob 的 App.config