Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable requeue jobs on Hangfire dashboard

Tags:

c#

hangfire

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.

like image 844
agascon Avatar asked Apr 11 '18 11:04

agascon


People also ask

How do I stop my Hangfire recurring job?

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.

How do you stop a Hangfire job?

If anyone here is familiar with hangfire how can I stop the process through task manager? Then call <server>. WaitForShutdown() to wait until it's finished. This should allow any tasks currently processing to finish.

How do I delete an Enqueued job on Hangfire?

I've landed here with more or less the same question. We enqueue long-running jobs in Hangfire. There is no “Cancel” button for jobs in the built-in dashboard, only a “Delete” button. Using that Delete button will remove the Job from the running jobs in the built-in dashboard (near) instantly.

Where does Hangfire store recurring jobs?

Where does HangFire store recurring jobs? Persistent. Background jobs are created in a persistent storage – SQL Server and Redis supported officially, and a lot of other community-driven storages. You can safely restart your application and use Hangfire with ASP.NET without worrying about application pool recycles.


2 Answers

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

like image 88
TheDude Avatar answered Nov 08 '22 14:11

TheDude


How about deleting the job?

RecurringJob.RemoveIfExists("myJobID");

like image 34
soccer7 Avatar answered Nov 08 '22 15:11

soccer7