Quartz.NET Running Job Self-Reschedule?

I created Quartz.NET work completely programmatically (without a configuration file, etc.). It works great on schedule. The job is initialized with a cron line that runs every 5 minutes. I would like the work to change the schedule of itself based on the environment (for example, errors occur over time, so cron should change to 30 minutes).

I am trying to determine what to write in

protected override void ExecuteInternal( IJobExecutionContext context ) 

therefore, work "changes itself." I am setting something in context. Sheduler Property? Do I have to go to the Scheduler itself and stop the work and recreate it (sounds hard to me, though)?

Any ideas appreciated, thanks.

+4
source share
3 answers

the skarist put me on the right track. Including my findings here for the next child who is facing this.

 protected override void ExecuteInternal( IJobExecutionContext jobContext ) { // ... do work... var tnew = new CronTriggerImpl( #nameofcurrentlyrunningjob# ) { CronExpressionString = "my new cron string is here" , TimeZone = TimeZoneInfo.Utc }; var jobNew = new JobDetailImpl( #nameofcurrentlyrunningjob# , typeof( CurrentJobType ) ); jobContext.Scheduler.RescheduleJob( new TriggerKey( #nameofcurrentlyrunningjob# ) , tnew ); } 

I also added the [DisallowConcurrentExecution] attribute to the class, but it is not directly related.

+2
source

Until I used Quartz.NET, I used Quartz in Java projects, and I think they are similar. I implemented a solution similar to what you are describing. In the executeInteral method, you have access to the execution context of the job. This is mainly due to the creation of a new trigger and the subsequent re-planning of the task (rescheduleJob). Therefore, when a condition arises, you will do something like:

 protected void ExecuteInternal( IJobExecutionContext context ) { // ... some code if (the_condition) { // figure out startTime // figure out endTime // figure out repeat time // figoure out repeatInterval Trigger trigger = new SimpleTrigger("OurNewTrigger","GROUP_NAME", context.getJobDetail().getName(),context.getJobDetail().getGroup(), startTime, endTime,repeatTime,repeatInterval); context.getScheduler().rescheduleJob("OurNewTrigger","GROUP_NAME",trigger); } // ... some more code } 

Along these lines. Hope this helps.

+5
source

I wanted to run the task in an arbitrary time range, between 5 and 25 minutes later after the last execution time, using this answer (from @skarist) and making some changes to it and using quartz documentation , I solved my problem:

 public class YourJob : IJob { public void Execute(IJobExecutionContext context) { //write your execution code //reschedule code after execution code: var random = new Random(); var num = random.Next(5,25); var date = DateTime.Now; var hour = date.AddMinutes(num).Hour; var min = date.AddMinutes(num).Minute; ITrigger trigger = TriggerBuilder.Create() .WithIdentity(context.Trigger.Key.Name) .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(hour, min)) .StartAt(DateTime.Now) .WithPriority(1) .Build(); IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.RescheduleJob(new TriggerKey(context.Trigger.Key.Name), trigger); } } 
+3
source

All Articles