如何仅每24小时每10分钟创建一次重复的Hangfire作业

时间:2018-06-10 14:17:40

标签: c# cron hangfire

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

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

如果我将作业开始时间添加到作业类:

private DateTime _jobstart = DateTime.UtcNow;

我可以在工作中检查一下24小时后再找出工作吗?

RecurringJob.RemoveIfExists(customerId);

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

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题。

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;
        }
相关问题