Skip to content

Commit

Permalink
Merge pull request #5420 from andreramos89/BISERVER-14971
Browse files Browse the repository at this point in the history
[BISERVER-14971] - Schedule runs twice if using 'Run Now' after the time that a daily (every weekday) schedule is due to run - Fixing issue with execution before first schedulled
  • Loading branch information
smmribeiro authored Oct 31, 2023
2 parents 7fb53a9 + 5f32413 commit f3bd1ab
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,32 @@ public void triggerNow( String jobId ) throws SchedulerException {
for ( Trigger trigger : scheduler.getTriggersOfJob( jobId, groupName ) ) {
//BISERVER-14971 - the error was that the trigger was not being replaced, as it was not considered a new trigger
// force the trigger to be updated with the previous fire time
Date nextIntendedFireDate = new Date( System.currentTimeMillis() + 1000 );
Date firstExecutionDate = null;
boolean scheduleAlreadyFired = false;
if ( trigger instanceof SimpleTrigger ) {
SimpleTrigger t = (SimpleTrigger) trigger.clone();
scheduleAlreadyFired = t.getPreviousFireTime() != null;
t.setPreviousFireTime( new Date() );
//BISERVER-14971 - reschedulling job to one second after this trigger, so the listener picks it up and understands it is not the old trigger
t.setNextFireTime( new Date(System.currentTimeMillis() + 1000) );
scheduler.rescheduleJob( jobId, groupName, t );
//BISERVER-14971 - reschedulling job to one second after this trigger, so the listener picks it up and
// understands it is not the old trigger
t.setNextFireTime( nextIntendedFireDate );
firstExecutionDate = scheduler.rescheduleJob( jobId, groupName, t );

} else if ( trigger instanceof CronTrigger ) {
CronTrigger t = (CronTrigger) trigger.clone() ;
CronTrigger t = (CronTrigger) trigger.clone();
scheduleAlreadyFired = t.getPreviousFireTime() != null;
t.setPreviousFireTime( new Date() );
//BISERVER-14971 - reschedulling job to one second after this trigger, so the listener picks it up and understands it is not the old trigger
t.setNextFireTime( new Date(System.currentTimeMillis() + 1000) );
scheduler.rescheduleJob( jobId, groupName, t );
//BISERVER-14971 - reschedulling job to one second after this trigger, so the listener picks it up and
// understands it is not the old trigger
t.setNextFireTime( nextIntendedFireDate );
firstExecutionDate = scheduler.rescheduleJob( jobId, groupName, t );
}
//In the case the job is being executed before the originally scheduled job,
// it won't run because Quartz will set the next fire date to the trigger's
// originally configured fist execution date. Therefore, we trigger manually
if ( !scheduleAlreadyFired || nextIntendedFireDate.before( firstExecutionDate ) ) {
scheduler.triggerJob( jobId, jobKey.getUserName() );
}
}

Expand Down

0 comments on commit f3bd1ab

Please sign in to comment.