Search code examples
azureazure-webjobsazure-servicebus-queues

Azure Scheduled WebJob Never Finished Status


I have a scheduled web job that runs a function every minute:

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

Sometimes it hangs and has a "Never Finish" status for a few days. This prevents new schedules for the job to be triggered. The Azure log also didn't record any entries - log was empty for that run.

I wonder if there is a way to tell Azure scheduler to continue with the scheduling if the job has a "Never Finish" status / state? Does setting "UseMonitor = true" do this?


Solution

  • As far as I know, if the scheduled web job processing is taking a long time periodically (or not finishing at all), it must be that every now and then the operations in your job function take a long time or fail. All depends on what your job is actually doing internally. If it is going async in places, the SDK will continue to wait for it to return.

    According to this, I suggest you could try to use webjob's TimeoutAttribute. It easy for functions to be cancelled based on timeout if they're hung.It will show a exception.

    If you find the error is too much and you want to alter, I suggest you could use ErrorTrigger, you could refer to this article.

    More details, you could refer to below codes.

    I used the queue to test it, the result is as same as 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");
            }