How do I force a quartz.net job to restart intervall after completion

后端 未结 4 946
醉话见心
醉话见心 2021-01-22 19:27

I have a project where I use TopShelf and TopShelf.Quartz

Following this example I am building my jobs with

                s.         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 19:49

    The best way I found is to add simple Job Listener. In my example it reschedules job, just after failure. Of cause you can add delay in .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);
        }
    }
    

    Also, I think it's useful to add class extension

    public static class QuartzExtensions
    {
        public static void RepeatJobAfterFall(this IScheduler scheduler, IJobDetail job)
        {
            scheduler.ListenerManager.AddJobListener(
                new QuartzRetryJobListner(),
                KeyMatcher.KeyEquals(job.Key));
        }
    }
    

    Just for simplify usage.

    _scheduler.ScheduleJob(job, trigger);
    //In case of failue repeat job immediately
    _scheduler.RepeatJobAfterFall(job);
    

提交回复
热议问题