Azure Scheduled WebJob 从未完成状态

Azure Scheduled WebJob Never Finished Status

我有一个计划的 Web 作业,每分钟 运行 一个函数:

[TimerTrigger("00:01:00", RunOnStartup = true)]

有时它会挂起并有几天 "Never Finish" 状态。这可以防止触发作业的新计划。 Azure 日志也没有记录任何条目 - 运行.

的日志为空

我想知道如果作业具有 "Never Finish" 状态,是否有办法告诉 Azure 调度程序继续调度?设置 "UseMonitor = true" 会这样做吗?

据我所知,如果计划的 Web 作业处理周期性地花费很长时间(或根本没有完成),那一定是您的作业函数中的操作时不时地花费很长时间或失败。一切都取决于你的工作实际上在内部做什么。如果它在某些地方异步,SDK 将继续等待它到 return。

据此,我建议您可以尝试使用webjob的TimeoutAttribute。 很容易根据超时取消功能,如果它们 hung.It 将显示异常。

如果你发现错误太多,想修改,我建议你可以使用ErrorTrigger,你可以参考这个article

更多细节,您可以参考下面的代码。

我用队列测试了一下,结果和TimerTrigger webjob一样。

  //Change the function timeout value
        [Timeout("00:00:03")]
        public static void  TimeoutJob(
        [QueueTrigger("queue")] string message,
         CancellationToken token,
        TextWriter log)
        {

            Console.WriteLine("From function: Received a message: " + message);

            Task task = new Task(new Action(() =>  /*here is your code*/ Thread.Sleep(5000)), token);
            // this will cancel the task is the token is CancellationRequested and show the exception the task is cancel
            task.Wait();
             Console.WriteLine("From function: Cancelled: Yes");
        }