5

我正在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并在应用程序停止时停止这些工作?

任何帮助将不胜感激!

谢谢

4

1 回答 1

0

单服务器设置

要获取所有重复作业,您可以使用作业存储(例如,通过静态实例或 DI):

using (var connection = JobStorage.Current.GetConnection())
{
  var recurringJobs = connection.GetRecurringJobs();
  foreach (var recurringJob in recurringJobs)
  {
    if (NonRemovableJobs.ContainsKey(recurringJob.Id)) continue;
    logger.LogWarning($"Removing job with id [{recurringJob.Id}]");
    jobManager.RemoveIfExists(recurringJob.Id);
  }
}

如果您的应用程序充当单个 Hangfire 服务器,则一旦应用程序停止,所有作业处理都将停止。在这种情况下,它们甚至不需要被移除。

多服务器设置

在为多个服务器使用相同 Hangfire 表的多实例设置中,您将遇到并非所有应用程序都具有所有可用程序集的问题。使用上面的方法,Hangfire 会尝试反序列化它找到的每个作业,这会导致“Assembly Not Found”异常。

为了防止这种情况,我使用了以下解决方法,它从表“哈希”中加载列“键”。它采用“recurring-jobs:{YourJobIdentifier}”格式。然后在必要时使用作业 ID 删除作业:

var queue = 'MyInstanceQueue'; // probably using queues in a multi server setup
var recurringJobsRaw = await dbContext.HangfireHashes.FromSqlInterpolated($"SELECT [Key] FROM [Hangfire].[Hash] where Field='Queue' AND Value='{queue}'").ToListAsync();
var recJobIds = recurringJobsRaw.Select(s => s.Key.Split(":").Last());
foreach (var id in recJobIds)
{
  if (NonRemovableJobs.ContainsKey(id)) continue;
  logger.LogWarning($"Removing job with id [{id}]");
  jobManager.RemoveIfExists(id);
}

PS:为了使其与 EF Core 一起使用,我为 Hangfire.Hash 表使用了无密钥实体

于 2021-08-17T06:51:47.427 回答