如何在删除作业之前检查作业是否存在于 Hangfire 中?

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

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

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

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

尝试使用监控 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);

Hangfire 挂起的问题已经修复,不需要再这样做了。