use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class TestEventListener method shouldGetNextFireTime.
@Test
public void shouldGetNextFireTime() {
try {
DateTime fireDate = new DateTime(2020, 7, 15, 10, 0, 0);
fakeNow(fireDate);
Map<String, Object> params = new HashMap<>();
MotechEvent event = new MotechEvent("test_event", params);
final String jobId = id("jobId");
params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
schedulerService.scheduleJob(new CronSchedulableJob(event, "0 0 10 * * ?"));
DateTime dateTime = schedulerService.getNextFireDate(new CronJobId(event));
assertEquals(fireDate, dateTime);
} finally {
stopFakingTime();
}
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class JobIdTest method value.
@Test
public void value() {
JobId jobId = new CronJobId(SUBJECT_VALUE, JOB_ID_VALUE);
assertEquals(String.format("%s-%s", SUBJECT_VALUE, JOB_ID_VALUE), jobId.value());
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class MotechScheduler method unscheduleTestEvent.
private static void unscheduleTestEvent() {
try {
LOGGER.info("Unscheduling the test job: " + TEST_EVENT_NAME);
schedulerService.unscheduleJob(new CronJobId(TEST_SUBJECT, TEST_EVENT_NAME));
} catch (RuntimeException e) {
LOGGER.error(String.format("Can not unschedule the test job %s:", TEST_EVENT_NAME), e);
}
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class MotechSchedulerServiceImpl method scheduleCronJob.
private void scheduleCronJob(CronSchedulableJob job, boolean isDayOfWeek, boolean update) {
logObjectIfNotNull(job);
validateCronSchedulableJob(job);
MotechEvent motechEvent = job.getMotechEvent();
JobId jobId = new CronJobId(motechEvent);
JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
Map<String, Object> metadata = new HashMap<>();
metadata.put(IS_DAY_OF_WEEK, isDayOfWeek);
metadata.put(UI_DEFINED, job.isUiDefined());
metadata.put(IGNORE_PAST_FIRES_AT_START, job.isIgnorePastFiresAtStart());
metadata.putAll(motechEvent.getMetadata());
jobDetail.getJobDataMap().put(EVENT_METADATA, metadata);
CronScheduleBuilder cronSchedule = cronSchedule(job.getCronExpression());
// TODO: should take readable names rather than integers
cronSchedule = setMisfirePolicyForCronTrigger(cronSchedule, schedulerSettings.getProperty("scheduler.cron.trigger.misfire.policy"));
CronTrigger trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(cronSchedule).startAt(job.getStartDate() != null ? job.getStartDate().toDate() : now().toDate()).endAt(DateUtil.toDate(job.getEndDate())).build();
Trigger existingTrigger;
try {
existingTrigger = scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME));
} catch (SchedulerException e) {
throw new MotechSchedulerException(format("Schedule or reschedule the job: %s.\n%s", jobId, e.getMessage()), "scheduler.error.cantRescheduleJob", Arrays.asList(jobId.value(), e.getMessage()), e);
}
if (existingTrigger != null) {
unscheduleJob(jobId.value());
}
DateTime now = now();
if (job.isIgnorePastFiresAtStart() && (job.getStartDate() == null || job.getStartDate().isBefore(now))) {
Date newStartTime = trigger.getFireTimeAfter(now.toDate());
if (newStartTime == null) {
newStartTime = now.toDate();
}
trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(cronSchedule).startAt(newStartTime).endAt(DateUtil.toDate(job.getEndDate())).build();
}
scheduleJob(jobDetail, trigger, update);
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class MotechSchedulerServiceImpl method getScheduledJobTimings.
/*
* Assumes that the externalJobId is non-repeating in nature. Thus the fetch is for jobId.value() and not
* jobId.repeatingId()
* Uses quartz API to fetch the exact triggers. Fast
*/
@Override
public List<DateTime> getScheduledJobTimings(String subject, String externalJobId, DateTime startDate, DateTime endDate) {
JobId jobId = new CronJobId(subject, externalJobId);
Trigger trigger;
try {
trigger = scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME));
return DateUtil.datesToDateTimes(TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger, new BaseCalendar(), DateUtil.toDate(startDate), DateUtil.toDate(endDate)));
} catch (SchedulerException e) {
throw new MotechSchedulerException(String.format("Can not get scheduled job timings given subject and externalJobId for dates : %s %s %s %s %s", subject, externalJobId, startDate.toString(), endDate.toString(), e.getMessage()), e);
}
}
Aggregations