【问题标题】:Delete all the Hangfire jobs删除所有 Hangfire 作业
【发布时间】:2020-05-19 16:18:21
【问题描述】:

在启动时,我尝试删除所有现有的 hangfire 作业,但我使用的方法似乎都不起作用。 请注意,我使用的是 MongoDB 数据库,但不确定它是否与问题有关。

这是我添加工作的方式:

RecurringJob.AddOrUpdate<IPostIndexerJob>(j => j.Execute(), configuration.GetValue<string>("Job:Interval"));

这是我的代码,如果有人有帮助的想法?

static private void ScheduleJobs(IConfiguration configuration)
        {
            PurgeJobs1();
            PurgeJobs2();
            PurgeJobs3();
        }

        static private void PurgeJobs1()
        {
            var mon = JobStorage.Current.GetMonitoringApi();
            mon.EnqueuedJobs("socloze-elasticsearch-post-indexer", 0, 99999999).ForEach(x =>
            {
                BackgroundJob.Delete(x.Key);
            });
        }

        static private void PurgeJobs2()
        {
            var monitor = JobStorage.Current.GetMonitoringApi();
            foreach (var queue in monitor.Queues())
            {
                PurgeQueue(queue.Name);
            }
        }

        static private void PurgeJobs3()
        {
            // Clean
            foreach (var item in Hangfire.JobStorage.Current.GetConnection().GetRecurringJobs())
                RecurringJob.RemoveIfExists(item.Id);
        }

        static private void PurgeQueue(string queueName)
        {
            var toDelete = new List<string>();
            var monitor = JobStorage.Current.GetMonitoringApi();

            var queue = monitor.Queues().First(x => x.Name == queueName);
            for (var i = 0; i < System.Math.Ceiling(queue.Length / 1000d); i++)
            {
                monitor.EnqueuedJobs(queue.Name, 1000 * i, 1000)
                    .ForEach(x => toDelete.Add(x.Key));
            }
            foreach (var jobId in toDelete)
            {
                BackgroundJob.Delete(jobId);
            }
        }

【问题讨论】:

    标签: mongodb hangfire


    【解决方案1】:

    要删除重复作业(我认为您的意思),您应该首先要求您的存储为您提供您设置的所有重复作业:

    jobStorage.GetConnection().GetRecurringJobs(); // where jobStorage is your storage instance, you can access it via JobStorage.Current in static context.
    

    然后删除所需的条目:

    recurringJobManager.RemoveIfExists("someId"); // where recurringJobManager is instance of IRecurringJobManager
    

    然后PurgeJobs3() 应该按预期工作(因为它在后台使用 IRecurringJobManager)。也许工作标识符与您的目标不匹配?

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2019-05-01
      相关资源
      最近更新 更多