.NET Core Quartz 异常重试作业
.NET Core Quartz retry job on exception
我有一个带有 Quartz 的 .NET 5 工作者服务,我已经准备好每天在特定时间执行的作业。但是,job Execute(IJobExecutionContext context)
方法中发生了异常。由于我刚刚在方法中添加了一个 throw;
,服务继续正常工作,但没有重新触发作业。
Program.cs:
services.AddQuartz(properties, options =>
{
options.UseMicrosoftDependencyInjectionScopedJobFactory();
});
services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
DailyJob.cs:
[DisallowConcurrentExecution]
public class DailyJob: IJob
{
private readonly ILogger<DailyJob> _logger
public DailyJob(
ILogger<DailyJob> logger
)
{
_logger = logger;
}
public Task Execute(IJobExecutionContext context)
{
try
{
// DoStuff
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
throw;
}
return Task.CompletedTask;
}
}
现在,我的问题是我是否可以通过立即重新解雇作业来避免这种情况?我见过带有参数 bool refireImmediately
的类型 JobExecutionException
,但如果我抛出该异常,它会做任何事情吗?它仍然是一个异常,我正在抛出 Execute 方法。
我在这里找到了一些有用的信息:
Refire quartz.net trigger after 15 minutes if job fails with exception
应该是这样的:
public Task Execute(IJobExecutionContext context)
{
try
{
// DoStuff
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
SimpleTriggerImpl retryTrigger = new SimpleTriggerImpl(Guid.NewGuid().ToString());
retryTrigger.Description = "RetryTrigger";
retryTrigger.RepeatCount = 0;
retryTrigger.JobKey = context.JobDetail.Key; // connect trigger with current job
retryTrigger.StartTimeUtc = DateBuilder.NextGivenSecondDate(DateTime.Now, 30); // Execute after 30 seconds from now
context.Scheduler.ScheduleJob(retryTrigger); // schedule the trigger
JobExecutionException jex = new JobExecutionException(ex, false);
throw jex;
}
return Task.CompletedTask;
}
我有一个带有 Quartz 的 .NET 5 工作者服务,我已经准备好每天在特定时间执行的作业。但是,job Execute(IJobExecutionContext context)
方法中发生了异常。由于我刚刚在方法中添加了一个 throw;
,服务继续正常工作,但没有重新触发作业。
Program.cs:
services.AddQuartz(properties, options =>
{
options.UseMicrosoftDependencyInjectionScopedJobFactory();
});
services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
DailyJob.cs:
[DisallowConcurrentExecution]
public class DailyJob: IJob
{
private readonly ILogger<DailyJob> _logger
public DailyJob(
ILogger<DailyJob> logger
)
{
_logger = logger;
}
public Task Execute(IJobExecutionContext context)
{
try
{
// DoStuff
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
throw;
}
return Task.CompletedTask;
}
}
现在,我的问题是我是否可以通过立即重新解雇作业来避免这种情况?我见过带有参数 bool refireImmediately
的类型 JobExecutionException
,但如果我抛出该异常,它会做任何事情吗?它仍然是一个异常,我正在抛出 Execute 方法。
我在这里找到了一些有用的信息: Refire quartz.net trigger after 15 minutes if job fails with exception
应该是这样的:
public Task Execute(IJobExecutionContext context)
{
try
{
// DoStuff
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
SimpleTriggerImpl retryTrigger = new SimpleTriggerImpl(Guid.NewGuid().ToString());
retryTrigger.Description = "RetryTrigger";
retryTrigger.RepeatCount = 0;
retryTrigger.JobKey = context.JobDetail.Key; // connect trigger with current job
retryTrigger.StartTimeUtc = DateBuilder.NextGivenSecondDate(DateTime.Now, 30); // Execute after 30 seconds from now
context.Scheduler.ScheduleJob(retryTrigger); // schedule the trigger
JobExecutionException jex = new JobExecutionException(ex, false);
throw jex;
}
return Task.CompletedTask;
}