Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all hangfire recurring jobs on startup?

I am looking at using Hangfire as a job scheduler for recurring jobs. So configuring them is simple with AddOrUpdate, but then how do i delete it? I don't want to pollute my code with RecurringJob.RemoveIfExists() when that job has been deleted and then have to remember to delete it later.

Is there a way to get a list of all recurring jobs and delete them when the server starts and so my code will re add them in every time? If not, if there a better way?

e.g.

Application version 1: Added new Hangfire recurring job Do something 1

Application version 2: Added new Hangfire recurring jobs Do something 2 and Do Something 3

Application version 3: Removed Hangfire recurring job Do something 2

Problem: the job will still exist on the server with error "Could not load type..." and needs to be deleted.

like image 649
lahsrah Avatar asked Oct 27 '16 06:10

lahsrah


People also ask

How do you delete a hangfire recurring job?

You can remove an existing recurring job by calling the RemoveIfExists method. It does not throw an exception when there is no such recurring job. RecurringJob. RemoveIfExists("some-id");

What is a recurring job?

Recurring jobs are used for: Jobs that have multiple visits with a repeating schedule. Eg. You visit the client every Tuesday. Jobs where you invoice your client periodically Eg.


1 Answers

A bit late on this one but hopefully it will help someone else. I got stuck in the same situation. In the end the answer on HangFire recurring task data helped me out.

I use the JobStorage to loop through all recurring jobs and remove each in turn as below:

using (var connection = JobStorage.Current.GetConnection()) {     foreach (var recurringJob in connection.GetRecurringJobs())     {         RecurringJob.RemoveIfExists(recurringJob.Id);     } } 

I am sure there is a nicer way out there but I couldn't find it

like image 131
paul Avatar answered Sep 23 '22 14:09

paul