Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron Schedule Misfire Instruction #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: java
jdk: oraclejdk8
jdk: openjdk8
sudo: false
2 changes: 1 addition & 1 deletion gojek-commons-amqp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-amqp</artifactId>
<name>gojek-commons-amqp</name>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-application</artifactId>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-cache</artifactId>
<name>gojek-commons-cache</name>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-core</artifactId>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-guice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-guice</artifactId>
<name>gojek-commons-guice-bundle</name>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-job/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-job</artifactId>
<name>gojek-commons-job-bundle</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static class Schedule {

private String cron;

private int cronMisfireInstruction;

private Integer interval;

/**
Expand Down Expand Up @@ -109,6 +111,20 @@ public Integer getInterval() {
public void setInterval(Integer interval) {
this.interval = interval;
}

/**
* @return the cron misfire instruction
*/
public int getCronMisfireInstruction() {
return cronMisfireInstruction;
}

/**
* @param cronMisfireInstruction one of {@value org.quartz.CronTrigger#MISFIRE_INSTRUCTION_FIRE_ONCE_NOW} or {@value org.quartz.CronTrigger#MISFIRE_INSTRUCTION_DO_NOTHING}
*/
public void setCronMisfireInstruction(int cronMisfireInstruction) {
this.cronMisfireInstruction = cronMisfireInstruction;
}
}

/**
Expand Down
23 changes: 9 additions & 14 deletions gojek-commons-job/src/main/java/com/gojek/job/JobManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Seconds;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.TriggerListener;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.spi.JobFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -286,7 +274,14 @@ protected ScheduleBuilder createSchedule(Schedule schedule) {
return null;
}
if (nonNull(schedule.getCron())) {
return CronScheduleBuilder.cronSchedule(schedule.getCron()).inTimeZone(DateTimeZone.UTC.toTimeZone());
CronScheduleBuilder cb = CronScheduleBuilder.cronSchedule(schedule.getCron()).inTimeZone(DateTimeZone.UTC.toTimeZone());
switch (schedule.getCronMisfireInstruction()) {
case CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW:
return cb.withMisfireHandlingInstructionFireAndProceed();
case CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING:
return cb.withMisfireHandlingInstructionDoNothing();
}
return cb;
} else {
return SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(schedule.getInterval()).repeatForever();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ public void shouldScheduleJobsOnServerStarted() throws Exception {
CronTrigger trigger1 = (CronTrigger) jobManager.getScheduler().getTrigger(TriggerKey.triggerKey("some-job", "some-group"));
assertEquals(jobDetail1.getJobDataMap().getWrappedMap(), data);
assertEquals(trigger1.getCronExpression(), schedule.getCron());
assertEquals(trigger1.getMisfireInstruction(), CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY);
JobDetail jobDetail2 = jobManager.getScheduler().getJobDetail(JobKey.jobKey("some-other-job", "some-other-group"));
CronTrigger trigger2 = (CronTrigger) jobManager.getScheduler().getTrigger(TriggerKey.triggerKey("some-other-job", "some-other-group"));
assertEquals(jobDetail2.getJobDataMap().getWrappedMap(), data);
assertEquals(trigger2.getCronExpression(), schedule.getCron());
assertEquals(trigger2.getMisfireInstruction(), CronTrigger.MISFIRE_INSTRUCTION_SMART_POLICY);
}

@Test(expectedExceptions=IllegalArgumentException.class)
Expand Down Expand Up @@ -414,7 +416,23 @@ public void shouldReturnLongRunningJobsWithThresholdFromJobData() throws Excepti
doReturn(scheduler).when(jobManager).getScheduler();
jobManager.getLongRunningJobs();
}


@Test
public void shouldHonorMisfireInstructionForCronScheduler() throws Exception {
Map<String, Object> data = Maps.newHashMap();
data.put("some-key", "some-value");
Schedule schedule = new Schedule("1 * * * * ?");
schedule.setCronMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
Job job1 = new Job("some-job", TestJob.class, "some-group", schedule, data, true);
jobConfiguration.setJobs(Lists.newArrayList(job1));

jobManager.serverStarted(mock(Server.class));
CronTrigger trigger1 = (CronTrigger) jobManager.getScheduler().getTrigger(TriggerKey.triggerKey("some-job", "some-group"));
assertEquals(trigger1.getCronExpression(), schedule.getCron());
assertEquals(trigger1.getMisfireInstruction(), CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
}


/**
* @author ganeshs
*
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-jpa</artifactId>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-kafka</artifactId>
<name>gojek-commons-kafka</name>
Expand Down
2 changes: 1 addition & 1 deletion gojek-commons-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
</parent>
<artifactId>gojek-commons-util</artifactId>
<name>Gojek commons util</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gojek</groupId>
<artifactId>gojek-commons</artifactId>
<version>2.2.5-SNAPSHOT</version>
<version>2.2.6-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Gojek Commons</name>
<description>Gojek commons library</description>
Expand Down