如何删除Hangfire中的作业?

时间:2016-03-16 14:44:43

标签: c# hangfire

如果您尝试删除一个不存在的作业,例如如果jobId不在Hangfire.Job

,则会挂起Hangfire
BackgroundJob.Delete(jobId);

在尝试删除作业之前,有没有办法检查作业是否存在?

2 个答案:

答案 0 :(得分:4)

尝试使用监控API(JobStorage.Current.GetMonitoringApi()),可以获取工作详细信息或工作列表。

完整的代码示例:

var monitoringApi = JobStorage.Current.GetMonitoringApi();

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

如果您想获得排队的项目:

// 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);

答案 1 :(得分:2)

不再需要这样做,因为导致Hangfire挂起的错误已经修复。

相关问题