Increase the recurring job polling interval for hangfire and enabling/disabling the recurring job process

1.2k Views Asked by At

I am trying to create a background processor windows service using hangfire. I would like to increase the recurring job polling interval to more than 1 minute(hard-coded by default). The reason for doing the same is that recurring polling can affect the performance of the database.
Is there a possibility to enable/disable the hangfire recurring Job feature. This is required in case there are multiple instances of the service installed.

1

There are 1 best solutions below

4
On

When you create a recurring job in Hangfire, even if you have multiple Hangfire servers, the job will not be run on two servers at the same time.

You can use Cron expression to define the frequency at which to run your job, as described in Hangfire docs:

RecurringJob.AddOrUpdate(() => YourJob(), "0 12 * */2");

However, your need may be to avoid triggering a job when the previous instance is still running. For this situation, I would recommend setting a flag (in the DB for example) when your job starts and removing it when it ends. Then check if the flag is present before actually starting your process.

Update

As you stated you want to prevent the RecurringJobScheduler from running on some servers, I have looked into the code and it seems there is no option to do this.

You can check the file BackgroundJobServer.cs where the scheduler is added to the process list and the RecurringJobScheduler.cs where the DB is queried. The value of 1 minute is hardcoded, as specified in the comments.

I think your only option is the pull request you have already made :(