如何强制quartz.net作业在完成后重新开始intervall
本文关键字:重新开始 intervall 何强制 quartz net 作业 | 更新日期: 2023-09-27 18:23:46
我有一个使用TopShelf
和TopShelf.Quartz
的项目
按照这个例子,我正在与建立我的工作
s.ScheduleQuartzJob(q =>
q.WithJob(() => JobBuilder.Create<MyJob>().Build())
.AddTrigger(() => TriggerBuilder.Create()
.WithSimpleSchedule(builder => builder
.WithIntervalInSeconds(5)
.RepeatForever())
.Build())
);
它每五秒钟启动一次我的工作,即使前一个还在运行。我真正想实现的是开始一份工作,完成后等待五秒钟再开始。这可能吗?或者我必须实现自己的逻辑(例如通过静态变量)。
@NateKerkhofs提出的作业监听器将起作用,如下所示:
public class RepeatAfterCompletionJobListener : IJobListener
{
private readonly TimeSpan interval;
public RepeatAfterCompletionJobListener(TimeSpan interval)
{
this.interval = interval;
}
public void JobExecutionVetoed(IJobExecutionContext context)
{
}
public void JobToBeExecuted(IJobExecutionContext context)
{
}
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
string triggerKey = context.JobDetail.Key.Name + ".trigger";
var trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.StartAt(new DateTimeOffset(DateTime.UtcNow.Add(interval)))
.Build();
context.Scheduler.RescheduleJob(new TriggerKey(triggerKey), trigger);
}
public string Name
{
get
{
return "RepeatAfterCompletionJobListener";
}
}
}
然后将监听器添加到调度器:
var jobKey = "myJobKey";
var schedule = new StdSchedulerFactory().GetScheduler();
listener = new
RepeatAfterCompletionJobListener(TimeSpan.FromSeconds(5));
schedule.ListenerManager.AddJobListener
(listener, KeyMatcher<JobKey>.KeyEquals(new JobKey(jobKey)));
var job = JobBuilder.Create(MyJob)
.WithIdentity(jobKey)
.Build();
// Schedule the job to start in 5 seconds to give the service time to initialise
var trigger = TriggerBuilder.Create()
.WithIdentity(CreateTriggerKey(jobKey))
.StartAt(DateTimeOffset.Now.AddSeconds(5))
.Build();
schedule.ScheduleJob(job, trigger);
不幸的是,我不知道如何用Typshelk.Quartz库使用的流畅语法来实现这一点(或者是否可以实现),我将其与TopShelf和常规Quartz.Net.
您可以使用TriggerListener(http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/trigger-and-job-listeners.html)当触发完成时收听,然后在5秒内重新安排。
另一个选项是将下一个作业安排为作业本身的执行中的最后一个操作。
http://www.quartz-scheduler.net/documentation/faq.html在2/3的地方有一个问题,可以解释更多关于它的信息。
JobListener解决方案是一种非常强大和灵活的方法,可以在完成后重新安排工作。感谢Nate Kerkhofs和stuartd的投入。
在我的情况下,用DisallowConcurrentExecution
属性装饰我的Job类就足够了,因为我的作业没有不同的实例
[DisallowConcurrentExecution]
public class MyJob : IJob
{
}
仅供参考:使用带有TopShelf.Quartz
的JobListener,代码可能看起来像这个
var jobName = "MyJob";
var jobKey = new JobKey(jobName);
s.ScheduleQuartzJob(q =>
q.WithJob(() => JobBuilder.Create<MyJob>()
.WithIdentity(jobKey).Build())
.AddTrigger(() => TriggerBuilder.Create()
.WithSimpleSchedule(builder => builder
.WithIntervalInSeconds(5)
.Build())
var listener = new RepeatAfterCompletionJobListener(TimeSpan.FromSeconds(5));
var listenerManager = ScheduleJobServiceConfiguratorExtensions
.SchedulerFactory().ListenerManager;
listenerManager.AddJobListener(listener, KeyMatcher<JobKey>.KeyEquals(jobKey));
如果您正在使用TopShelf.Quartz.Ninject
(和我一样),请不要忘记在调用ScheduleJobServiceConfiguratorExtensions.SchedulerFactory()
之前先调用UseQuartzNinject()
我发现的最好的方法是添加简单的Job Listener。在我的例子中,它会在失败后重新安排工作。当然,你可以在.StartAt(DateTime.UtcNow)
中添加延迟
public class QuartzRetryJobListner : IJobListener
{
public string Name => GetType().Name;
public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default) => await Task.CompletedTask;
public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default) => await Task.CompletedTask;
public async Task JobWasExecuted(
IJobExecutionContext context,
JobExecutionException jobException,
CancellationToken cancellationToken = default)
{
if (jobException == null) return;
// Create and schedule new trigger
ITrigger retryTrigger = TriggerBuilder.Create()
.StartAt(DateTime.UtcNow)
.Build();
await context.Scheduler.ScheduleJob(context.JobDetail, new[] { retryTrigger }, true);
}
}
此外,我认为添加类扩展是有用的
public static class QuartzExtensions
{
public static void RepeatJobAfterFall(this IScheduler scheduler, IJobDetail job)
{
scheduler.ListenerManager.AddJobListener(
new QuartzRetryJobListner(),
KeyMatcher<JobKey>.KeyEquals(job.Key));
}
}
只是为了简化使用。
_scheduler.ScheduleJob(job, trigger);
//In case of failue repeat job immediately
_scheduler.RepeatJobAfterFall(job);