【发布时间】:2019-06-06 02:01:11
【问题描述】:
我正在使用Hangfire BackgroundJob 使用以下代码在 C# 中创建后台作业。
var options = new BackgroundJobServerOptions
{
ServerName = "Test Server",
SchedulePollingInterval = TimeSpan.FromSeconds(30),
Queues = new[] { "critical", "default", "low" },
Activator = new AutofacJobActivator(container),
};
var jobStorage = new MongoStorage("mongodb://localhost:*****", "TestDB", new MongoStorageOptions()
{
QueuePollInterval = TimeSpan.FromSeconds(30)
});
var _Server = new BackgroundJobServer(options, jobStorage);
它创建 Jobserver 对象,然后,我正在创建 Schedule, Recurring Jobs,如下所示。
var InitJob = BackgroundJob.Schedule<TestInitializationJob>(job => job.Execute(), TimeSpan.FromSeconds(5));
var secondJob = BackgroundJob.ContinueWith<Test_SecondJob>(InitJob, job => job.Execute());
BackgroundJob.ContinueWith<Third_Job>(secondJob, job => job.Execute());
RecurringJob.AddOrUpdate<RecurringJobInit>("test-recurring-job", job => job.Execute(), Cron.MinuteInterval(1));
之后,我想在我的应用程序停止或关闭时删除或停止所有作业。所以在我的应用程序的 OnStop 事件中,我编写了以下代码。
var monitoringApi = JobStorage.Current.GetMonitoringApi();
var queues = monitoringApi.Queues();// BUT this is not returning all queues and all jobs
foreach (QueueWithTopEnqueuedJobsDto queue in queues)
{
var jobList = monitoringApi.EnqueuedJobs(queue.Name, 0, 100);
foreach (var item in jobList)
{
BackgroundJob.Delete(item.Key);
}
}
但是,上面获取所有作业和所有队列的代码不起作用。它总是返回 "default" 队列而不返回所有作业。
谁能想到使用 Hangfire JobStorage 获取所有作业并在应用程序停止时停止这些作业?
任何帮助将不胜感激!
谢谢
【问题讨论】:
-
您是否尝试过这样的工作:
monitoringApi.ScheduledJobs(0, 100)?对我来说解决了这个问题。
标签: c# asp.net-mvc asp.net-core-2.0 hangfire hangfire-autofac