c# - Hangfire 使用取消令牌
问题描述
我有一个周期性工作,根据情况/事件,我必须停止并删除一个特定的周期性工作及其正在运行的实例(它可能正在运行或不运行)。
删除重复作业很容易。要停止正在运行的作业,据我所知,我必须使用取消令牌。问题是:如何保留取消令牌?有没有办法将其存储在数据库中并稍后使用?
解决方案
我过去遇到过同样的问题,我找到了解决方法。我在下面为您编译了一些伪代码,希望对您有所帮助。
public void DeleteRunningHangfireJob(string methodName)
{
if (!string.IsNullOrEmpty(methodName)) {
var monitor = JobStorage.Current.GetMonitoringApi();
var processingJobs = monitor.ProcessingJobs(0, int.MaxValue);
var enqueuedJobs = monitor.EnqueuedJobs(0, int.MaxValue);
processingJobs.Where(o => o.Value.Job.Method.Name == methodName).ToList().ForEach(x => { BackgroundJob.Delete(x.Key); });
enqueuedJobs.Where(o => o.Value.Job.Method.Name == methodName).ToList().ForEach(x => { BackgroundJob.Delete(x.Key); });
}
}
推荐阅读
- java - java - 如何将数组列表中的多个自定义对象存储到java中的json文件中?
- crystal-lang - Granite ORM + Fiber + mysql = 意外的 EOF(异常)?
- javascript - React+Redux:只有在代码被缩小时才会在渲染上卸载组件的错误
- rx-java2 - 重复自身直到找到期望值的 RxObservable
- java - “builder.replace”是什么类型的复杂性?
- enterprise-architect - 在 Gitbook 中使用 Enterprise Architect API 获取文档
- python - 如何在 Django 中删除不可为空的字段?
- javascript - 将 HyperApp Web 应用程序上传到 github,我要上传哪些文件?
- c++ - constexpr(即常量初始化)模板变量的初始化顺序是否有保证?
- firefox - Imacro 脚本在 Firefox 中不起作用