Reputation: 75
I have set up a recurring job that simply processes some orders:
{
RecurringJob.AddOrUpdate(
hangFireJob.JobId,
() => hangFireJob.Execute(),
hangFireJob.Schedule);
}
The issue I'm running into, is that we have multiple "backup" servers all running this same code. They all are reaching out to the same HangFire database.
I'm seeing the same job ran multiple times, which obviously gives us errors since the orders are already processed.
I want the backup servers to recognize that the recurring job has already been queued and not queue it again. I figured that it would go off the jobname to do this (the first parameter above). What am I missing here?
I included the hangfire server setup below:
private IEnumerable<IDisposable> GetHangfireServers()
{
var configSettings = new Settings();
GlobalConfiguration.Configuration
.UseNinjectActivator(_kernel)
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(configSettings.HangfireConnectionString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = false,
SchemaName = "LuxAdapter",
PrepareSchemaIfNecessary = false
});
yield return new BackgroundJobServer();
}```
Upvotes: 1
Views: 3192
Reputation: 69
Since the defaults values provide uniqueness only on a process level, you should handle it manually if you want to run different server instances inside the same process:
var options = new BackgroundJobServerOptions
{
ServerName = String.Format(
"{0}.{1}",
Environment.MachineName,
Guid.NewGuid().ToString())
};
var server = new BackgroundJobServer(options);
// or
app.UseHangfireServer(options);
Upvotes: 0
Reputation: 11364
Not sure if you have tried this already but look into using the DisableConcurrentExecution
attribute on the job that will prevent multiple executions of the same job.
Upvotes: 2