在Quartz.net中,如何将所有计划的作业延迟一定时间? [英] In Quartz.net, how can I delay all scheduled jobs by a certain time span?

查看:309
本文介绍了在Quartz.net中,如何将所有计划的作业延迟一定时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Quartz.net安排各种API调用. 我使用的API会限制每个时间段内可以发出的请求数量,如果超出该数量,则我的帐户将在下一分钟受到罚款(无法发出请求).

I am using Quartz.net to schedule various API calls. The API I am using restricts the number of requests that can be made per time period and if that is exceeded, then my account is penalized for the next minute (no requests can be made).

如果我收到我发出太多请求的通知,并且我的帐户在下一分钟将被限制,那么我将需要确保在此期间不执行任何计划的作业.如何最好地将所有计划的作业延迟一两分钟?

If I ever receive a notification that I have made too many requests and my account will be throttled for the next minute, I will need to ensure that no scheduled jobs fire during that period. How can I best delay firing of all scheduled jobs by a minute or two?

我本来打算调用Scheduler.GetTriggerKeys()并循环并更新每个现有触发器,如下所示:

I was originally intending to call Scheduler.GetTriggerKeys() and loop over and update every existing trigger like so:

foreach(var triggerKey in SchedInstance.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup()))
{
    var oldTrigger = SchedInstance.GetTrigger(triggerKey);
    TriggerBuilder tb = oldTrigger.GetTriggerBuilder();
    // Update the schedule associated with the builder and build the new trigger
    tb.StartAt(oldTrigger.StartTimeUtc.AddSeconds(63));
    var newTrigger = tb.Build();
    SchedInstance.RescheduleJob(oldTrigger.Key, newTrigger);
}

这是正确的方法吗?或者只是在相同时间段内停止调度程序然后重新启动它会更好吗?

Is this the right approach or would it be better to simply stop the scheduler for the same time period and then restart it?

推荐答案

您有两种方法可以实现. 如您所述,您可以停止调度程序或在触发器上循环.但这对我来说听起来不是最好的选择.

You have a couple of possibilities to achieve that. As you stated, you can stop the scheduler or loop over your triggers. But this sounds for me not like the best option.

TriggerListener

TriggerListener

您可以实现ITriggerListener接口并使用VetoJobExecution()方法.该实现可以如下所示:

You can implement the ITriggerListener interface and use the VetoJobExecution() method. The implementation can look like this:

public class SystemThrottledTriggerListener : ITriggerListener
{
    public string Name => "System Throttled Trigger Listener";

    public void TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode)
    {
        // no need for implementation
    }

    public void TriggerFired(ITrigger trigger, IJobExecutionContext context)
    {
        // no need for implementation
    }

    public void TriggerMisfired(ITrigger trigger)
    {
        // no need for implementation
    }

    public bool VetoJobExecution(ITrigger trigger, IJobExecutionContext context)
    {
        // If you return true, then the Trigger is vetoed and the job is not executed.
        // The Job will automatically scheduled for his next execution
        return IsSystemThrottled();
    }
}

然后只需将侦听器添加到您的调度程序中,如果您的系统受到限制,所有触发器都将被否决:

Then simply add the Listener to your scheduler and all triggers are vetoed if your system is throttled:

Scheduler.ListenerManager.AddTriggerListener(new SystemThrottledTriggerListener());

JobExecutionException

JobExecutionException

您可以抛出JobExecutionException来停止作业的执行.为此,您需要在执行开始时检查系统是否受到限制,然后抛出错误提示.这是Quartz的唯一例外,您可以告诉Quartz应该立即重新执行该作业.所有其他异常将被吞下并停止执行作业. 该实现可以如下所示:

You can throw a JobExecutionException to stop the execution of your job. To do this, you need to check at the beginning of the execution that your system is throttled and then throw the exeception. This is the only exception for Quartz at which you can tell Quartz that it should refire the job immediately. All other exceptions will be swallowed and will stop the execution of the job. The implementation can look like this:

public class MyJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        if(IsSystemThrottled())
            throw new JobExecutionException(true);

        // your other stuff
    }
}

如果使用参数true创建异常,则作业将立即重新启动.并将一次又一次地触发,直到您的系统不再受到限制为止.

If you create the exception with the parameter true, the job will immediately refired. And will refired again and again until your system is no longer throttled.

如果您有很多工作,我建议您使用一个可以抛出JobExecutionException的工作基类,并且您只能从此类中派生您的工作.

If you have many jobs, i would recommend to use a job base class that can throw the JobExecutionException and you derive your jobs only from this class.

这篇关于在Quartz.net中,如何将所有计划的作业延迟一定时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆