Skip to content

Commit

Permalink
Fix bug for triggers with no next execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
jlinn committed Dec 30, 2016
1 parent e076935 commit fe7ffd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/net/joelinn/quartz/jobstore/RedisStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,16 @@ public List<TriggerFiredResult> triggersFired(List<OperableTrigger> triggers, Je
trigger.triggered(calendar);

// set the trigger state to WAITING
final long nextFireTime = trigger.getNextFireTime().getTime();
jedis.hset(triggerHashKey, TRIGGER_NEXT_FIRE_TIME, Long.toString(nextFireTime));
setTriggerState(RedisTriggerState.WAITING, (double) nextFireTime, triggerHashKey, jedis);
final Date nextFireDate = trigger.getNextFireTime();
long nextFireTime = 0;
if (nextFireDate != null) {
nextFireTime = nextFireDate.getTime();
jedis.hset(triggerHashKey, TRIGGER_NEXT_FIRE_TIME, Long.toString(nextFireTime));
setTriggerState(RedisTriggerState.WAITING, (double) nextFireTime, triggerHashKey, jedis);
}

JobDetail job = retrieveJob(trigger.getJobKey(), jedis);
TriggerFiredBundle triggerFiredBundle = new TriggerFiredBundle(job, trigger, calendar, false, new Date(), previousFireTime, previousFireTime, trigger.getNextFireTime());
TriggerFiredBundle triggerFiredBundle = new TriggerFiredBundle(job, trigger, calendar, false, new Date(), previousFireTime, previousFireTime, nextFireDate);

// handling jobs for which concurrent execution is disallowed
if (isJobConcurrentExecutionDisallowed(job.getJobClass())){
Expand Down Expand Up @@ -727,7 +731,7 @@ public List<TriggerFiredResult> triggersFired(List<OperableTrigger> triggers, Je
pipe.set(redisSchema.jobBlockedKey(job.getKey()), schedulerInstanceId);
pipe.sadd(redisSchema.blockedJobsSet(), jobHashKey);
pipe.sync();
} else if(trigger.getNextFireTime() != null){
} else if(nextFireDate != null){
jedis.hset(triggerHashKey, TRIGGER_NEXT_FIRE_TIME, Long.toString(nextFireTime));
logger.debug(String.format("Releasing trigger %s with next fire time %s. Setting state to WAITING.", triggerHashKey, nextFireTime));
setTriggerState(RedisTriggerState.WAITING, (double) nextFireTime, triggerHashKey, jedis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import org.junit.Test;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.matchers.NameMatcher;

import static net.joelinn.quartz.TestUtils.createCronTrigger;
import static net.joelinn.quartz.TestUtils.createJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
* @author Joe Linn
Expand Down Expand Up @@ -35,4 +38,20 @@ public void testMisfireListener() throws Exception {
// wait for MisfireListener.triggerMisfired() to be called
waiter.await(2500);
}


@Test
public void testSingleExecution() throws Exception {
final String jobName = "oneJob";
JobDetail jobDetail = createJob(TestJob.class, jobName, "oneGroup");

SimpleTrigger trigger = TriggerBuilder.newTrigger().withSchedule(simpleSchedule().withRepeatCount(0).withIntervalInMilliseconds(200)).build();

Waiter waiter = new Waiter();
scheduler.getListenerManager().addTriggerListener(new CompleteListener(waiter), NameMatcher.triggerNameEquals(trigger.getKey().getName()));

scheduler.scheduleJob(jobDetail, trigger);

waiter.await(2000);
}
}

0 comments on commit fe7ffd7

Please sign in to comment.