Recurring jobs triggered from web server with load balancing

Hi

What happens if I schedule a recurring job from a web application that runs on 2 servers behind loadbalancing? Will both servers trigger the job? They are using the same database.

when you register the recurring job, do not forget to use unique id RecurringJob.AddOrUpdate("some-id", () => Console.WriteLine(), Cron.Hourly); - by this you make sure that both servers, when starting, will not initialize 2 separate recurring jobs, but one job specified by some-id.

for the execution you should annotate your jobs with DisableConcurrentExecution attribute to avoid parallel execution from both servers http://odinserj.net/2014/05/21/hangfire-0.8.2-released/#toc_2

Hi

@Lukas_Kolafa i have the same question of @Jeppe_Svendsen but in case of enqueued BackgroundJob.
Sometimes my job is executed 2 times.

Thanks.

Hi @kikkovinke

hard to say like that. that can be caused by many reasons starting from incorrectly used attributes on your side to infrastructure problems with your database / redis (or whatever you use to avoid concurrency). I would start debugging the issue when the job is being executed - put a breakpoint in the job and check if your database keeps the concurrency lock open. If you check the the code of Concurrency attribute in Hangfire, you will see how it works - it uses some SQL locking feature for this purpose. There were as well some issues with certain SQL databases that the lock would be released after 30 minutes. As said… this can have 100+1 reasons :slight_smile: