首页 文章

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

提问于
浏览
0

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

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

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

private DateTime _jobstart = DateTime.UtcNow;

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

RecurringJob.RemoveIfExists(customerId);

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

1 回答

  • 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;
            }
    

相关问题