Hangfire Discussion

How to handle recurring jobs running each one on multiple servers

Tags: #<Tag:0x00007f8507105368> #<Tag:0x00007f8507105200>


I have two different recurring jobs running on two different machines using the same database. I would like to make sure that each job executes on each machine and for that I have enqueued these jobs using the Instance name from Azure as a Queue name.

So I have followed the suggestion posted here: https://stackoverflow.com/questions/42201809/hangfire-recurring-job-on-every-server
And a similar solution here: One queue for the whole farm, and one queue by server

So my setup is basically this:

var hangfireQueueName = ConfigurationManager.AppSettings["WEBSITE_INSTANCE_ID"];

var options = new BackgroundJobServerOptions() {
	ServerName = serverName,
	Queues = new[] { "default", hangfireQueueName }

jobServer = new BackgroundJobServer(options);

RecurringJob.AddOrUpdate("Job1", () => Job1.RunAsync(JobCancellationToken.Null), Cron.MinuteInterval(Job1.Schedule), queue: hangfireQueueName);
RecurringJob.AddOrUpdate("Job2", () => Job2.RunAsync(JobCancellationToken.Null), Cron.MinuteInterval(Job2.Schedule), queue: hangfireQueueName);

So the problem I have now is when the machine instance name changes for any reason, I end up with recurring jobs enqueueing jobs for queues/servers that no longer exist.

How can I set my jobs in order to avoid this behaviour or should I try to dispose the old recurring jobs?