How to ensure that the same job will not run twice on the same time

Hi, I’m planning to use hangfire on a web site which is load balanced on 3 servers, my main concern that I want to ensure that even I got a request to enque the same job from the 3 server, that job will run only once in the same time.

on the other hand, I want other jobs to run on the same time without depending on each other (mutli-threaded)

can this be done??

thanks in advance…

I’m not quite sure I understand what you are asking. If you’re asking if Hangfire is load balancer safe, the answer is yes. When a job is picked up it gets locked so other servers can’t pick it up. If you’re asking if a job gets queued 3 different times, how many times will it execute, then the answer is 3 since it got queued 3 times. You could include logic in your job that checks to see if the task has been already executed (similar to the way you would to ensure the job is re-entrant).

First of all, thank you very much for your reply …

please let me rephrase my question …

  1. Hang fire Background Job Server will be installed on 3 web servers with load balancer.
  2. As many users will use the site, 2 or more users may ask to execute the same job at the same time.
  3. Now, the queue will have 2 or more requests for the same job (function).
  4. I want to prevent all the 3 background job servers to execute the same job in the same time (they can execute any other function, but not the same function at the same time)

so, I want some way to check if the same function is running now by any other background job servers and to wait until that job is done or rolled back.

can this be done??

thanks in advance

I’ve never used this attribute, but there does exist a job filter attribute you can throw on your function that only allows one instance to run at a time DisableConcurrentExecutionAttribute. I don’t believe this attribute will prevent the other instances from executing, but it will prevent them from running the same function in parallel across even across servers since it asks the datastore if there are any instances currently running.