Skip to content

Commit

Permalink
Merge pull request #4233 from cravobranco/PDI-17517
Browse files Browse the repository at this point in the history
[PDI-17517] - Inconsistent Time zone during scheduling job/transforma…
  • Loading branch information
Marc Batchelor committed Sep 18, 2018
2 parents 0e26c89 + e131090 commit b43ad1c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.pentaho.platform.scheduler2.recur.SequentialRecurrence;
import org.pentaho.platform.util.ActionUtil;
import org.quartz.Calendar;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
Expand All @@ -74,6 +75,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -315,9 +317,17 @@ protected Job createJob( String jobName, Map<String, Serializable> jobParams, IJ
.debug( MessageFormat
.format(
"Scheduling job {0} with trigger {1} and job parameters [ {2} ]", jobId.toString(), trigger, prettyPrintMap( jobParams ) ) ); //$NON-NLS-1$

if ( quartzTrigger instanceof CronTrigger ) {
Serializable timezone = jobParams.get( "timezone" );
if ( timezone != null ) {
setTimezone( (CronTrigger) quartzTrigger, timezone.toString() );
}
}

scheduler.scheduleJob( jobDetail, quartzTrigger );

logger.debug( MessageFormat.format("Scheduled job {0} successfully", jobId.toString() ) ); //$NON-NLS-1$
logger.debug( MessageFormat.format( "Scheduled job {0} successfully", jobId.toString() ) ); //$NON-NLS-1$
} catch ( org.quartz.SchedulerException e ) {
throw new SchedulerException( Messages.getInstance().getString(
"QuartzScheduler.ERROR_0001_FAILED_TO_SCHEDULE_JOB", jobName ), e ); //$NON-NLS-1$
Expand Down Expand Up @@ -805,6 +815,23 @@ private static List<ITimeRecurrence> parseRecurrence( String cronExpression, int
return timeRecurrence;
}

/**
* Update cronTrigger's timezone based on the info from caller
*
* @param cronTrigger the cron trigger to update
* @param timezone the timezone to set
*/
void setTimezone( CronTrigger cronTrigger, String timezone ) throws SchedulerException {
try {
CronExpression cronEx = new CronExpression( cronTrigger.getCronExpression() );
cronEx.setTimeZone( TimeZone.getTimeZone( timezone ) );
cronTrigger.setCronExpression( cronEx );
} catch ( ParseException e ) {
throw new SchedulerException( Messages.getInstance().getString(
"ComplexJobTrigger.ERROR_0001_InvalidCronExpression" ), e ); //$NON-NLS-1$
}
}

/** {@inheritDoc} */
public SchedulerStatus getStatus() throws SchedulerException {
SchedulerStatus schedulerStatus = SchedulerStatus.STOPPED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
import org.pentaho.platform.api.repository2.unified.RepositoryFile;
import org.pentaho.platform.api.scheduler2.SchedulerException;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;

import java.text.ParseException;
import java.util.Collections;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class QuartzSchedulerTest {

Expand Down Expand Up @@ -123,5 +130,16 @@ public void testValidateParamsSchedulingAllowed() throws SchedulerException {
"input = /home/admin/allowed.ktr : output = /home/admin/allowed." ) );
}

@Test
public void testSetTimezone() throws Exception {

CronTrigger cronTrigger = new CronTrigger();
cronTrigger.setCronExpression( new CronExpression( "0 15 10 ? * 6L 2002-2018" ) );
String currentTimezoneId = TimeZone.getDefault().getID();

}
new QuartzScheduler().setTimezone( cronTrigger, currentTimezoneId );

assertNotNull( cronTrigger.getTimeZone() );
assertEquals( currentTimezoneId, cronTrigger.getTimeZone().getID() );
}
}

0 comments on commit b43ad1c

Please sign in to comment.