Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a job exists in Hangfire before I delete it?

Tags:

c#

hangfire

Hangfire hangs if you try and delete a job that doesn't exist, i.e if jobId isn't in Hangfire.Job

BackgroundJob.Delete(jobId);

Is there any way of checking if a job exists before trying to delete it?

like image 743
Dan O'Leary Avatar asked Dec 25 '22 07:12

Dan O'Leary


2 Answers

Try using the monitoring API (JobStorage.Current.GetMonitoringApi()), there is a possibility to get job details or list of jobs.

Complete code example:

var monitoringApi = JobStorage.Current.GetMonitoringApi();

var deletedJobs = monitoringApi.DeletedJobs(0, 10);

If you want to get queued items:

// If no queue name was defined somewhere, probably this will be "default".
// If no items have been queued, then the queue won't exist, and it will error here.
var queue = monitoringApi.Queues().First().Name;

var enqueud jobs = monitoringApi.EnqueuedJobs(queue, 0, 10);
like image 197
stepandohnal Avatar answered Feb 06 '23 09:02

stepandohnal


There is no need to do this anymore, as the bug causing Hangfire to hang has been fixed.

like image 27
Dan O'Leary Avatar answered Feb 06 '23 07:02

Dan O'Leary