What happens when a scheduled WebJob runs for a long time

久未见 提交于 2019-12-30 19:58:20

问题


What happens if an Azure WebJob is scheduled to run but the previous running instance hasn't finished yet? Will it run the WebJob again so that two are running at once? Will it not run the WebJob and start the clock over? I haven't been able to find this behavior documented anywhere. I have a job I want to run hourly but it's possible that sometimes it might take longer than an hour to complete, but I never want two of them running at once.


回答1:


As i understand it scheduled webjobs is just triggered webjobs that is run using Azure Scheduler, if you open Azure Scheduler in management portal you can see the webjobs and even configure them in more detail. (You can see the log too which would give you the simple answer to your question).

If you like to look at whats going on your scheduled webjob is run as a Triggered webjob by Kudu, if you look in the Kudu source you will see that a lockfile is created when a job is started, and if you try to start another job a ConflictException is thrown if there is already a lock file

The Azure scheduler calls your job using a webhook that catches the ConflictException and gives you the "Error_WebJobAlreadyRunning" warning which will tell you: "Cannot start a new run since job is already running."




回答2:


The best way I have found to throttle WebJob execution is to use the scheduled WebJob to queue the work to be done using the built-in queue and then create a separate WebJob that runs continuously which executes whenever a new queue message appears but set the BatchSize = 1. This effectively prevents any job from executing concurrently.

Here is some sample code in the WebJob that queues up a message.

class Program
{
  static void Main()
  {
    var host = new JobHost();
    host.Call(typeof(Functions).GetMethod("QueueSomething"));
  }
}
public class Functions
{
  [NoAutomaticTrigger]
  public static void QueueSomething([Queue("myqueue")] ICollector<string> message, TextWriter log)
  {
    //do some work here to create a message to pass to the other WebJob so it can execute your task
    string x = "serialize some data or instructions to pass";
    
    //if I want to pass it a couple of things to do use ICollector above and add this to the queue too
    string x2 = "some additional task to do";
    

  //Add the json to the WebJobs queue
  message.Add(x);
  message.Add(x2);
}

Here is the code in the continuously running WebJob. This monitors "myqueue" and then calls DoSomething() when a new message appears. The key thing is the BatchSize property that prevents this WebJob from reading and processing another message until the first one is complete.

class Program
{
    static void Main()
    {
        JobHostConfiguration config = new JobHostConfiguration();

        //Read and process one message at a time
        config.Queues.BatchSize = 1;
        var host = new JobHost(config);
        host.RunAndBlock();
    }
}

public class Functions
{
    public static void DoSomething([QueueTrigger("myqueue")] string message, TextWriter log)
    {
     //Read and deserialize your message and then do some work with it
     string d = JsonConvert.DeserializeObject(message);
    }
}

Hope that helps.



来源:https://stackoverflow.com/questions/29542705/what-happens-when-a-scheduled-webjob-runs-for-a-long-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!