How can I make sure a scheduled task does not execute a new script run in case a previous one is still busy?
I have a scheduled task that runs every X minutes.
In rare / exceptional cases it is possible that the execution of the script takes longer than this time.
In those cases I rather have the next scheduled task trigger to skip the execution and wait until the running script has finished.
This to prevent an increasing load / queue of scripts running.
I also don't want to decrease the frequency of the scheduled task as in normal situations it would then not update fast enough.
I just have to cover the exceptional case.
May I assume Scheduled task = configured on 1 specific agent. So it will always run automation in the same SLAutomation Process.
If that is the case, could I perhaps use the following:
private static int _isRunning = 0; // Script is launched from Scheduld task, make sure itpublic void Run(IEngine engine) { // No wait: immediate try-lock if (System.Threading.Interlocked.CompareExchange(ref _isRunning, 1, 0) != 0) { engine.Log("[WARNING] Script already running. Skipping this cycle."); return; // skip, do NOT wait } try { engine.SetFlag(RunTimeFlags.NoInformationEvents); RunSafe(engine); }finally { System.Threading.Volatile.Write(ref _isRunning, 0); } }