How to remove all hangfire temporary jobs at startup?

I consider using Hangfire as a job scheduler for recurring jobs. So setting them up is simple with AddOrUpdate , but how can I remove it? I don't want to pollute my code with RecurringJob.RemoveIfExists() when this job was deleted, and then I need to remember it later.

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

eg.

Application Version 1: Added new repetitive work Hangfire Do something 1

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

Application Version 3: Fixed re-run of Hangfire Do something 2

Problem: the task will still exist on the server with the error "Unable to load type ..." and it must be deleted.

+20
source share
4 answers

A little late, but hopefully this helps someone else. I am stuck in the same situation. In the end, the answer to the duplicate HangFire task data helped me.

I use JobStorage to cycle JobStorage all repeating jobs and delete them one at a time:

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

I am sure there is a better way, but I could not find it

+46
source

Paul was helpful, but the API API seems to have changed. Using Hangfire 1.6.20 I needed to get duplicate jobs from StorageConnectionExtensions

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

You can use this code:

 var recurringJobs = Hangfire.JobStorage.Current.GetConnection().GetRecurringJobs(); foreach (var item in recurringJobs) { RecurringJob.RemoveIfExists(item.Id); } 
0
source

And is there a way to delete scheduled tasks, just like that?

0
source

Source: https://habr.com/ru/post/1258870/


All Articles