Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.NET Running Job Self-Reschedule?

I have created a Quartz.NET job completely programmatically (no config file, etc). It runs on-schedule fine. The job is initialized with a cron string to run every 5 minutes. I would like to have the job change the schedule of itself based on the environment (eg errors happen over time so cron should change to 30 minutes).

I am trying to determine what to write in the

protected override void ExecuteInternal( IJobExecutionContext context )

method so the job "changes itself". Do I set something in the context.Scheduler property? Do I have to go to the Scheduler itself and terminate the job and re-create it (sounds kind of heavy handed to me though)?

All ideas appreciated, thanks.

like image 615
Snowy Avatar asked Dec 26 '22 06:12

Snowy


1 Answers

While I haven't used Quartz.NET I have used Quartz in Java projects, and I would think they are similar. I have implemented a solution similar to what you describe. In the executeInteral method you have access to the jobexecution context. Basically it involves the creation of a new trigger and then reschedule the job (rescheduleJob). So when the condition arise you would 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.

like image 145
skarist Avatar answered Jan 14 '23 04:01

skarist