What happens when a scheduled webjob runs for a long time

What happens if Azure WebJob is scheduled to start but the previous running instance is not finished yet? Will it launch WebJob again so that the two start at the same time? Will WebJob launch and run the clock? I could not find this behavior anywhere. I have work that I want to do hourly, but it is possible that sometimes it can take more than an hour, but I never want two of them to work right away.

+6
source share
2 answers

As I understand it, scheduled webjobs are simply running webjobs that run using the Azure Scheduler, if you open Azure Scheduler in the management portal, you can see the webjobs and even configure them in more detail. (You can also see a magazine that will give you a simple answer to your question).

If you like to watch what happens, your scheduled website starts as a Kudu Triggered webjob, if you look at the Kudu source, you will see that the lock file is created when the task starts, and if you try to run another task a ConflictException is raised if already have a lock file

The Azure Scheduler calls your work with a webhook that catches a ConflictException and throws an error message, "Error_WebJobAlreadyRunning" , which tells you: "Unable to start a new start because the job is already running."

+7
source

The best way I found to run WebJob is to use a scheduled WebJob for the job queue, which must be done using the built-in queue, and then create a separate WebJob that runs continuously, which runs every time a new queue message appears, set BatchSize = 1. This effectively prevents the execution of any task at the same time.

Here is an example of code in WebJob that queues 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 a constantly running WebJob. This controls "myqueue" and then calls DoSomething () when a new message appears. The main BatchSize property that prevents this WebJob from reading and processing another message until the first 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 this helps.

-1
source

Source: https://habr.com/ru/post/984903/


All Articles