I have an asp.net core MVC 8 web app with hangfire (v1.8.7) configured that executes several jobs. Occasionally, a job may exceed the specified time and trigger another instance. My goal is to prevent Hangfire from launching multiple instances of the same job simultaneously.
I have only one server running
I tried to use the DisableConcurrentExecution attribute but it didn't work
[DisableConcurrentExecution(timeoutInSeconds: (3600))]
public ActionResult SetScheduledJob_SyncModifiedItemsFromAffiliateBox()
{
RecurringJob.AddOrUpdate("SyncModifiedItemsFromAffiliateBox_OffHours", () =>
SyncModifiedItemsFromAffiliateBoxJob(), Cron.Minutely());
return RedirectToAction("index");
}
public void SyncModifiedItemsFromAffiliateBoxJob()
{
Thread.Sleep(2 * 60 * 1000);
}
this is my program.cs code:
// Add Hangfire services.
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(appSettings.GetConnectionString("HangfireConnection")));
// Add the processing server as IHostedService
builder.Services.AddHangfireServer();
app.UseHangfireDashboard();
app.MapHangfireDashboard();
Here is the solution that works for me:
first, you need to create this class that gets registered jobs from the database and compares it with the ones running.
create class PreventConcurrentExecutionJobFilter:
public class PreventConcurrentExecutionJobFilter : JobFilterAttribute, IClientFilter, IServerFilter { }
implements these methods on class:
second you need to call this class as attributes for the job method: