How to disable requeue jobs on Hangfire dashboard
Asked Answered
R

4

8

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.

Remodel answered 11/4, 2018 at 11:23 Comment(2)
Do you have a database connected to this project? If so you could use ajax calls and keep the settings in a database entry.Rockabilly
Don't think is a good idea to modify the DB without using the actual Hangfire api.Remodel
S
3

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'
Sufferable answered 9/6, 2022 at 21:48 Comment(0)
N
2

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

Nonessential answered 29/8, 2018 at 7:14 Comment(1)
This has nothing to do with the question. Problem is that a failed job can be requeued in the dashboard and I want to disable that.Remodel
C
1

How about deleting the job?

RecurringJob.RemoveIfExists("myJobID");

Coda answered 27/8, 2018 at 15:28 Comment(1)
This won't help, as we are talking about one-time fire and forget jobs. There are some option in AutomaticRetry attribute... OnAttemptsExceeded = AttemptsExceededAction.Delete, but won't help either. The deleted job can be requeued.Remodel
O
1

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; }

    /* ... */
}
Oversoul answered 29/8, 2018 at 6:23 Comment(1)
There are some checks already in place, and job won't run if it shouldn't, but the button is there anyway. I feel that some app operator can be tempted to use it and well it's confusing in any case.Remodel

© 2022 - 2024 — McMap. All rights reserved.