简体   繁体   English

Hangfire:如何停止处理后台作业并在特定时间段内对其进行处理

[英]Hangfire : How to stop processing of background jobs and process them in a certain time period

We have a scenario where we want the jobs to keep on queuing in the server but not processing. 我们有一种情况,我们希望作业继续在服务器中排队但不进行处理。 Processing of jobs should be done in a specific time period for eg, 11 am to 13 pm. 作业的处理应在特定的时间段内进行,例如上午11点至下午13点。 For recurring jobs, this can be done by using CRON expressions. 对于周期性作业,可以使用CRON表达式来完成。 Is there any way we can achieve the same thing with Background jobs enqueued. 有什么方法可以使排队的后台工作实现相同的目的。

For normal processing, we are queuing in user-defined queues as : 对于正常处理,我们将用户定义的队列排队为:

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

_client is of type IBackgroundJobClient . _client是IBackgroundJobClient类型。

You can schedule a background job from another background job. 您可以从另一个后台作业安排一个后台作业。 So, you could maybe put the scheduling decision logic inside the method you are scheduling. 因此,您可以将调度决策逻辑放入正在调度的方法中。

Say you have the following method: 说您有以下方法:

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
}

Now you just schedule this method for the first time and the method will continue scheduling itself when needed. 现在,您只是第一次计划此方法,该方法将在需要时继续进行自身计划。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM