【问题标题】:Hangfire : How to stop processing of background jobs and process them in a certain time periodHangfire:如何停止处理后台作业并在特定时间段内处理它们
【发布时间】:2019-03-22 05:12:35
【问题描述】:

我们有一个场景,我们希望作业继续在服务器中排队但不处理。作业的处理应在特定时间段内完成,例如上午 11 点至下午 13 点。对于重复作业,这可以通过使用 CRON 表达式来完成。有什么方法可以通过排队的后台作业实现相同的目标。

对于正常处理,我们在用户定义的队列中排队:

var state = new EnqueuedState(queue.Name);
_client.Create(methodCall, state);

_client 的类型为 IBackgroundJobClient

【问题讨论】:

    标签: c# hangfire


    【解决方案1】:

    您可以从另一个后台作业调度后台作业。因此,您可以将调度决策逻辑放入您正在调度的方法中。

    假设你有以下方法:

    void DoSomethingAndScheduleAgain()
    {
       // Do some work...
    
       bool shouldContinue = //Some condition (e.g. it is not 13 pm yet)
       if (shouldeContinue)
          BackgroundJob.Schedule(DoSomethingAndScheduleAgain), TimeSpan.FromMinutes(5));
       else
          BackgroundJob.Schedule(DoSomethingAndScheduleAgain), TimeSpan.FromHours(22)); //Wait until tomorrow
    }
    

    现在您只是第一次调度此方法,该方法将在需要时继续自行调度。

    BackgroundJob.Schedule(DoSomethingAndScheduleAgain), TimeSpan.FromMinutes(1));
    
    

    【讨论】:

    • BackgroundJob.Schedule 将添加到我们不想要的默认队列中。很抱歉在前面的问题中错过了这一点。
    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多