【问题标题】:How to create recurring Hangfire job every 10 minutes for only 24 hours如何在仅 24 小时内每 10 分钟创建一次重复的 Hangfire 作业
【发布时间】:2018-06-10 14:17:40
【问题描述】:

当我有一个未完成该流程的新客户注册时,我会向他们发送一封包含后续步骤的电子邮件。我需要创建一个在注册后的前 24 小时内每 10 分钟运行一次的作业。在那之后,有另一个进程接管。我这样安排工作:

RecurringJob.AddOrUpdate(customerId, () => new NewCustomerProcess().checkNewCustomerStatus(customerId)), "*/10 * * * *");

如果我在作业类中添加作业开始时间:

private DateTime _jobstart = DateTime.UtcNow;

我可以在工作中检查它以确定 24 小时过去的时间然后删除工作吗?

RecurringJob.RemoveIfExists(customerId);

Hangfire 每次运行时都会重新实例化作业类吗?

【问题讨论】:

    标签: c# cron hangfire


    【解决方案1】:

    如果我正确理解你的问题。

    Hangfire 每次都会创建一个新的作业类实例。 所以如果我需要解决这个问题,我会在每次创建作业时将 DateTime 作为参数传入:

    RecurringJob.AddOrUpdate(customerId, () => new 
    NewCustomerProcess().checkNewCustomerStatus(customerId, DateTime.Now.AddDays(2))), "*/10 * * * *");
    

    然后在 checkNewCustomerStatus 中与 DateTime.Now 进行比较

          if (DateTime.Now > dateEnqueued)
            {
                //Job is complete
                return;
            }
    

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 2014-10-16
      • 2020-07-01
      • 2017-10-26
      • 2015-03-08
      • 1970-01-01
      • 2010-12-22
      • 2022-12-18
      • 2015-08-31
      相关资源
      最近更新 更多