如果作业因异常而失败,则在 15 分钟后重新触发 quartz.net 触发器 [英] Refire quartz.net trigger after 15 minutes if job fails with exception
问题描述
如果作业引发异常,我已经搜索了有关如何在一段时间后重新触发作业的答案.我看不到这样做的任何简单方法.
I have searched for an answer on how to retrigger a job after a ceratin amount of time, if the job throws an exception. I cannot see any simple way of doing this.
如果我这样设置触发器:
if I set my trigger up like this:
JobDetail job = new JobDetail("Download catalog", null, typeof(MyJob));
job .Durable = true;
Trigger trigger= TriggerUtils.MakeDailyTrigger(12, 0);
trigger.StartTimeUtc = DateTime.UtcNow;
trigger.Name = "trigger name";
scheduler.ScheduleJob(job , trigger);
MyJob 看起来像这样:
And MyJob look like this:
public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
var service = new service();
try
{
service.Download();
}
catch (Exception)
{
throw;
}
}
}
如果 service.Download() 调用抛出某种异常,我如何在 15 分钟后重新触发/重新触发?
how do I make the trigger to refire/retrigger after there is gone 15 minutes if the service.Download() call throws some sort of Exception?
推荐答案
我认为你唯一的选择是捕获错误并告诉 Quartz.net 立即重新触发:
I think the only option you have is to trap the error and tell Quartz.net to refire immediately:
public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
var service = new service();
try
{
service.Download();
}
catch (Exception ex)
{
JobExecutionException qe = new JobExecutionException(ex);
qe.RefireImmediately = true; // this job will refire immediately
throw qe;
}
}
}
更新:
我做了一些测试,看来你可以在一个正在执行的作业中安排一个新的触发器.
你可以试试这样的:
I did some tests and it seems that you can schedule a new trigger inside an executing job.
You can try something like this:
public class MyJob : IJob
{
public void Execute(JobExecutionContext context)
{
var service = new service();
try
{
service.Download();
}
catch (Exception ex)
{
JobExecutionException qe = new JobExecutionException(ex);
// qe.RefireImmediately = true; // this job will refire immediately
// throw qe;
OnErrorScheduleJob(context);
}
}
private void OnErrorScheduleJob(JobExecutionContext context)
{
var jobOnError = context.Scheduler.GetJobDetail("ONERRORJOB", "ERROR");
if (jobOnError == null)
{
JobDetail job = new JobDetail("ONERRORJOB", "ERROR", typeof(MyJob));
job.Durable = false;
job.Volatile = false;
job.RequestsRecovery = false;
SimpleTrigger trigger = new SimpleTrigger("ONERRORTRIGGER",
"ERROR",
DateTime.UtcNow.AddMinutes(15),
null,
1,
TimeSpan.FromMinutes(100));
context.Scheduler.ScheduleJob(job, trigger);
}
}
}
这篇关于如果作业因异常而失败,则在 15 分钟后重新触发 quartz.net 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!