How can we let recurring job only trigger on one instance in case of scale out

882 Views Asked by At

I use Hangfire to read files from FTP server and I have multiple server instances to read from FTP. I need the recurring job only trigger on one instance to prevent the same job to read from same file.

var jobId = BackgroundJob.Enqueue<FtpImageJob>(j => j.ExecuteAsync(null, device.NumericId, device.DeviceId, device.VehicleId, device.TenantId));
                BackgroundJob.ContinueJobWith<FtpDeleteJob>(jobId, j => j.ExecuteAsync(null, numericId), JobContinuationOptions.OnAnyFinishedState);
2

There are 2 best solutions below

0
On

If you are looking to restrict execution of job to only one of the many Hangfire servers, use a separate queue that only exists on one server. This way your recurring job will only execute on that hangfire server that's serving that queue.

0
On

You may either use paid Hangfire.Pro package with Mutex feature or decorate your job function with [DisableConcurrentExecution] attribute.