Reputation: 785
Currently the Hangfire dashboard offers an option to requeue jobs (either succeed or failed) and in my case running twice a job can cause problems.
I have tried to add AutomaticRetry attribute...
[AutomaticRetry(Attempts = 0)]
Which solves the problem when jobs fails, jobs are not requeued automatically, but the button is still on the dashboard and they can be manually requeued.
Upvotes: 8
Views: 10507
Reputation: 873
You can set date for that task from hangfire database.
UPDATE [HangFire].[Hash]
SET [Value] = '2050-01-01T00:00:00.0000000Z'
where [Key]='Job_Name' and [Field]='NextExecution'
Upvotes: 3
Reputation: 267
Currently there is no way to make Hangfire stop running the jobs. For example when you have issue with one of services that are used in jobs, and you want to stop hangfire until issue resolution.
The idea is to have your job raise an exception. It will then go into the failed state and depending on your AutomaticRetry setting attempt to rerun the job if needed automatically (up to the defined number of retry attempts) or stay there so that once the problem is solved you can manually requeue the job from the dashboard.
Having the job sit there waiting for a service to come back online does not sound advisable (speaking in general, I obviously don’t know your specific scenario that well).
On the whole I find I am even extremely careful of even doing automatic retries. I only even consider doing those if I have a guarantee that whatever the job does is idempotent (i.e. running the same actions multiple times does not cause issues).
Imagine a job that adds 100 $ to the salary of every employee in a company (i.e. set salary = salary+100). You run the job updating the DB but halfway through the DB server connection drops. Half the employees have had the salary increase, the other half did not get it yet. Running the same job again should not apply the 100$ increase a second time to those employees done in the first run.
Stopping the whole server also seems a bit drastic. I believe the advised mechanism is to just delete the job if you don’t want the job (if it is a recurring one and not a fire and forget) to enqueue new runs for a while. Then when the issue is solved you just reschedule it. I do agree that a pause feature would be a nice to have. You could extend hangfire yourself to do this using the jobfilters and IElectStateFilters. Just have a boolean (i.e. IsHangfirePaused=true) somewhere that you can check in the OnStateElection event and prevent the job from transitioning to the EnqueuedState when it is set to true.
this is according to https://discuss.hangfire.io/t/ability-to-stop-running-jobs/4215/2
Upvotes: 2
Reputation: 157
As odinserj
suggests on Hangfire Discussion.
You can simply use a condition inside a recurring job and store it in your database to prevent job running:
public void MyMethod()
{
if (someCondition) { return; }
/* ... */
}
Upvotes: 1
Reputation: 3995
How about deleting the job?
RecurringJob.RemoveIfExists("myJobID");
Upvotes: 1