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

Dan*_*ary 2 c# hangfire

如果您尝试删除不存在的作业(即,如果jobId不在Hangfire中),则Hang​​fire会挂起。

BackgroundJob.Delete(jobId);
Run Code Online (Sandbox Code Playgroud)

有什么方法可以在尝试删除作业之前检查它是否存在?

小智 5

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

完整的代码示例:

var monitoringApi = JobStorage.Current.GetMonitoringApi();

var deletedJobs = monitoringApi.DeletedJobs(0, 10);
Run Code Online (Sandbox Code Playgroud)

如果要排队的物品:

// 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);
Run Code Online (Sandbox Code Playgroud)


Dan*_*ary 3

无需再执行此操作,因为导致 Hangfire 挂起的错误已修复。