Skip to content

Instantly share code, notes, and snippets.

@schourode
Last active May 30, 2019 18:16
Show Gist options
  • Save schourode/7639291 to your computer and use it in GitHub Desktop.
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.
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();
}
}
@slide
Copy link

slide commented Jul 22, 2014

Shouldn't line 14 be if(!IsRunning)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment