From 5f324139f2d1d11922758e6b4b0dbd4ac857b64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ramos?= Date: Tue, 24 Oct 2023 11:54:24 +0100 Subject: [PATCH] [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 --- .../scheduler2/quartz/QuartzScheduler.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/scheduler/src/main/java/org/pentaho/platform/scheduler2/quartz/QuartzScheduler.java b/scheduler/src/main/java/org/pentaho/platform/scheduler2/quartz/QuartzScheduler.java index 2d47e508680..79c2b97974c 100644 --- a/scheduler/src/main/java/org/pentaho/platform/scheduler2/quartz/QuartzScheduler.java +++ b/scheduler/src/main/java/org/pentaho/platform/scheduler2/quartz/QuartzScheduler.java @@ -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() ); } }