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