Last active
May 30, 2019 18:16
-
-
Save schourode/7639291 to your computer and use it in GitHub Desktop.
Provides a base class for NCron jobs which are not allowed to be executed in parallel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace NCron | |
{ | |
public abstract class SynchronizedCronJob<TJob> : CronJob where TJob : SynchronizedCronJob<TJob> | |
{ | |
private static readonly object SyncRoot = new object(); | |
private static bool IsRunning = false; | |
public override void Execute() | |
{ | |
var runNow = false; | |
lock (SyncRoot) | |
{ | |
if (!IsRunning) | |
{ | |
IsRunning = true; | |
runNow = true; | |
} | |
} | |
if (runNow) | |
{ | |
try | |
{ | |
ExecuteSynchronized(); | |
} | |
finally | |
{ | |
IsRunning = false; | |
} | |
} | |
else | |
{ | |
Log.Warn(() => string.Format("Job of type {0} is already running.", typeof(TJob).Name)); | |
} | |
} | |
/// <summary> | |
/// This method is invoked whenever the job is due for execution, after job initialization. | |
/// </summary> | |
public abstract void ExecuteSynchronized(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't line 14 be if(!IsRunning)?