use of org.motechproject.scheduler.contract.RepeatingSchedulableJob in project motech by motech.
the class MotechSchedulerActionProxyServiceImpl method scheduleRepeatingJob.
@Override
public // NO CHECKSTYLE More than 7 parameters (found 8).
void scheduleRepeatingJob(// NO CHECKSTYLE More than 7 parameters (found 8).
String subject, Map<Object, Object> parameters, DateTime startTime, DateTime endTime, Integer repeatCount, Integer repeatIntervalInSeconds, Boolean ignorePastFiresAtStart, Boolean useOriginalFireTimeAfterMisfire) {
MotechEvent motechEvent = new MotechEvent(subject, createMotechEventParameters(parameters));
RepeatingSchedulableJob job = new RepeatingSchedulableJob();
job.setMotechEvent(motechEvent);
job.setStartDate(startTime);
job.setEndDate(endTime);
job.setRepeatCount(repeatCount);
job.setRepeatIntervalInSeconds(repeatIntervalInSeconds);
job.setIgnorePastFiresAtStart(ignorePastFiresAtStart);
job.setUseOriginalFireTimeAfterMisfire(useOriginalFireTimeAfterMisfire);
scheduler.scheduleRepeatingJob(job);
}
use of org.motechproject.scheduler.contract.RepeatingSchedulableJob in project motech by motech.
the class MotechSchedulerServiceImpl method buildJobDetail.
private Trigger buildJobDetail(SchedulableJob schedulableJob, Date jobStartTime, Date jobEndTime, JobId jobId, JobDetail jobDetail, ScheduleBuilder scheduleBuilder) {
Trigger trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(jobStartTime).endAt(jobEndTime).build();
DateTime now = now();
if (schedulableJob.isIgnorePastFiresAtStart() && newDateTime(jobStartTime).isBefore(now)) {
List<Date> pastTriggers = TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger, null, jobStartTime, now.toDate());
if (pastTriggers.size() > 0) {
if (scheduleBuilder instanceof SimpleScheduleBuilder && ((RepeatingSchedulableJob) schedulableJob).getRepeatCount() != null) {
((SimpleScheduleBuilder) scheduleBuilder).withRepeatCount(((RepeatingSchedulableJob) schedulableJob).getRepeatCount() - pastTriggers.size());
}
Date newStartTime = getFirstTriggerInFuture(trigger, now);
trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(newStartTime).endAt(jobEndTime).build();
}
}
return trigger;
}
use of org.motechproject.scheduler.contract.RepeatingSchedulableJob in project motech by motech.
the class TestListener method scheduleJobs.
private void scheduleJobs(MotechSchedulerFactoryBean schedulerFactoryBean, SettingsFacade settings) throws InterruptedException, SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getQuartzScheduler();
scheduler.clear();
try {
Map<String, Object> params = new HashMap<>();
params.put("foo", "bar");
params.put("fii", "bor");
params.put("fuu", "baz");
String eventSubject = "test_event";
MotechEvent motechEvent = new MotechEvent(eventSubject, params);
int maxJobs = 1;
int repeatCount = 1;
TestListener listener = new TestListener("test_listener", maxJobs, repeatCount);
eventListenerRegistryService.registerListener(listener, eventSubject);
DateTime startTime = now().plusSeconds(0);
Integer repeatInterval = 1;
System.out.println("startTime: " + startTime);
NanoStopWatch timeToSchedule = new NanoStopWatch().start();
for (int i = 0; i < maxJobs; i++) {
params.put(MotechSchedulerService.JOB_ID_KEY, "test_job_" + String.valueOf(i));
RepeatingSchedulableJob repeatingJob = new RepeatingSchedulableJob(motechEvent, repeatCount, repeatInterval, startTime, null, false);
repeatingJob.setUseOriginalFireTimeAfterMisfire(false);
schedulerService.scheduleRepeatingJob(repeatingJob);
}
System.out.println(format("Time to schedule : %dms", timeToSchedule.duration()));
scheduler.start();
synchronized (listener) {
while (scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains("default")).size() > 0) {
listener.wait(5000);
}
}
System.out.println(listener.getReport());
assertEquals(maxJobs * (repeatCount + 1), listener.getCount());
} finally {
eventListenerRegistryService.clearListenersForBean("test_listener");
schedulerFactoryBean.getQuartzScheduler().standby();
}
}
use of org.motechproject.scheduler.contract.RepeatingSchedulableJob in project motech by motech.
the class TestEventListener method shouldScheduleInterveningRepeatJobWithoutEndDate.
@Test
public void shouldScheduleInterveningRepeatJobWithoutEndDate() throws SchedulerException {
try {
fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
Map<String, Object> params = new HashMap<>();
params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
RepeatingSchedulableJob repeatJob = new RepeatingSchedulableJob(new MotechEvent("test_event", params), 3, DateTimeConstants.SECONDS_PER_DAY, newDateTime(2020, 7, 13, 12, 0, 0), null, true);
repeatJob.setUseOriginalFireTimeAfterMisfire(false);
schedulerService.scheduleRepeatingJob(repeatJob);
List<DateTime> fireTimes = getFireTimes("test_event-job_id-repeat");
assertEquals(asList(newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 16, 12, 0, 0)), fireTimes);
} finally {
stopFakingTime();
}
}
use of org.motechproject.scheduler.contract.RepeatingSchedulableJob in project motech by motech.
the class TestEventListener method shouldScheduleRepeatJobBoundByEndDate.
@Test
public void shouldScheduleRepeatJobBoundByEndDate() throws SchedulerException {
try {
fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
final String jobId = id("jobId");
Map<String, Object> params = new HashMap<>();
params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
schedulerService.scheduleRepeatingJob(new RepeatingSchedulableJob(new MotechEvent("test_event", params), DateTimeConstants.SECONDS_PER_DAY, newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 18, 12, 0, 0), false));
List<DateTime> fireTimes = getFireTimes("test_event-" + jobId + "-repeat");
assertEquals(asList(newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 16, 12, 0, 0), newDateTime(2020, 7, 17, 12, 0, 0)), fireTimes);
} finally {
stopFakingTime();
}
}
Aggregations