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

问题描述 投票:2回答:2

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

BackgroundJob.Delete(jobId);

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

c# hangfire
2个回答
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);

2
投票

没有必要再这样做,因为导致Hangfire挂起的错误已得到修复。

© www.soinside.com 2019 - 2024. All rights reserved.