6

It looks like out of the box Sitecore is able to prevent the same scheduled task from running simultaneously if a previous run hasn't finished yet (time interval between runs is less than time required to finish a task)

I've found this info in this blog: https://briancaos.wordpress.com/2017/08/18/sitecore-scheduled-task-schedule-time-format-and-other-quirks/ According to this blog, changing Async option allows manipulating this behavior, so in theory, we could run the same task simultaneously.

I've created a simple task, that is triggered every one minute and executes the code below. So, the task itself takes five minutes to finish.

public void Synchronize()
{
    _logger.Log.Info("Synchronization started.");
    Thread.Sleep(new TimeSpan(0, 5, 0));
    _logger.Log.Info("Synchronization finished.");
}

When Async is not checked, it works as expected and in logs I never see anything like two or more tasks running in parallel:

Synchronization started
Synchronization started
Synchronization started
....

According to Brian's blog checking Async should change this and tasks should start running simultaneously. However, it doesn't happen and Sitecore still prevents the task from triggering a second time if the previous run hasn't finished yet. Strange thing that I see Last Run field being updated every minute when Async is checked, so at least it tries to run it every minute, but it's not reflected in logs!

Roughly speaking in both cases it works as needed, but since I can't force two tasks to run in parallel, not sure if I understand a concept, so have a concern that code won't blow up on a different environment. Is there any setting that I've missed, do I need to write something like follows, etc?

public void Synchronize()
{
    lock (SynchronizeLock)
    {
        _logger.Log.Info("Synchronization started.");
        Thread.Sleep(new TimeSpan(0, 5, 0));
        _logger.Log.Info("Synchronization finished.");
    }
}

I have Sitecore 7.5 rev. 141003 installed, plus "SITECORE SHELL WAX" plugin to simplify work with schedules.

Thanks, Andrii

2

async in task execution means that the agent which runs the tasks will execute task asynchronously instead of synchronously.

So if you have TaskA and TaskB, if async is not selected, agent will run first TaskA and after it's finished, it will run TaskB.

If async is selected, Agent will start TaskA and immediately after will start TaskB (without waiting for TaskA to finish).

Still, if there is one TaskA already running, Job Manager will not start second TaskA, even if async is selected.

There is a really nice blog post with more information: https://sitecorebasics.wordpress.com/2014/09/16/do-you-know-why-you-check-async-checkbox-while-configuring-scheduled-task/

  • Thanks. So, do I understand it correctly that Sitecore automatically does what is needed and I shouldn't worry that my method will be launched in parallel a few times by a scheduler? – and85 Feb 21 '18 at 13:56
  • Assuming that you have only 1 server which runs scheduled task, yes. If you have multiple servers with agents executing tasks, from what I understand same task can be executed in parallel on all of them and it doesn't matter if it's sync or async. E.g. if it takes 1 hours to finish the task and schedule says every 15 minutes, every server can pickup same task before it's completed. I haven't tested this though. – Marek Musielak Feb 21 '18 at 13:59
  • I shouldn't worry that my method will be launched in parallel a few times - Yes, you should worry. Optimise your code and/or run it less frequently. The scheduler may not run Jobs with the same name, but it will queue them up and the as soon as the previous one finishes, the next one start. If this happens frequently, the task would essentially be continuously running. – jammykam Feb 22 '18 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.