[英]Hangfire : How to stop processing of background jobs and process them in a certain time period
我们有一种情况,我们希望作业继续在服务器中排队但不进行处理。 作业的处理应在特定的时间段内进行,例如上午11点至下午13点。 对于周期性作业,可以使用CRON表达式来完成。 有什么方法可以使排队的后台工作实现相同的目的。
对于正常处理,我们将用户定义的队列排队为:
var state = new EnqueuedState(queue.Name);
_client.Create(methodCall, state);
_client是IBackgroundJobClient
类型。
您可以从另一个后台作业安排一个后台作业。 因此,您可以将调度决策逻辑放入正在调度的方法中。
说您有以下方法:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.