use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class MotechSchedulerServiceImpl method safeScheduleJob.
@Override
public void safeScheduleJob(CronSchedulableJob cronSchedulableJob) {
logObjectIfNotNull(cronSchedulableJob);
assertCronJob(cronSchedulableJob);
JobId jobId = new CronJobId(cronSchedulableJob.getMotechEvent());
try {
unscheduleJob(jobId.value());
} catch (MotechSchedulerException e) {
LOGGER.error("Error while unscheduling job with id {}", jobId.value(), e);
}
scheduleJob(cronSchedulableJob);
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class MotechSchedulerServiceImpl method getScheduledJobTimingsWithPrefix.
/*
* Loads all triggers and then loops over them to find the applicable trigger using string comparison. This
* will work regardless of the jobId being cron or repeating.
*/
@Override
public List<DateTime> getScheduledJobTimingsWithPrefix(String subject, String externalJobIdPrefix, DateTime startDate, DateTime endDate) {
JobId jobId = new CronJobId(subject, externalJobIdPrefix);
List<Date> messageTimings = new ArrayList<>();
try {
List<TriggerKey> triggerKeys = new ArrayList<TriggerKey>(scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains(JOB_GROUP_NAME)));
for (TriggerKey triggerKey : triggerKeys) {
if (StringUtils.isNotEmpty(externalJobIdPrefix) && triggerKey.getName().contains(jobId.value())) {
Trigger trigger = scheduler.getTrigger(triggerKey);
messageTimings.addAll(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 externalJobIdPrefix for dates : %s %s %s %s %s", subject, externalJobIdPrefix, startDate.toString(), endDate.toString(), e.getMessage()), e);
}
return DateUtil.datesToDateTimes(messageTimings);
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class MotechSchedulerServiceImpl method rescheduleJob.
@Override
public void rescheduleJob(String subject, String externalId, String cronExpression) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(subject + " " + externalId + " " + cronExpression);
}
assertArgumentNotNull("Subject", subject);
assertArgumentNotNull("ExternalId", externalId);
assertArgumentNotNull("Cron expression", cronExpression);
JobId jobId = new CronJobId(subject, externalId);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Rescheduling the Job: %s new cron expression: %s", jobId, cronExpression));
}
CronTrigger trigger;
JobDetail job;
try {
trigger = (CronTrigger) scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME));
if (trigger == null) {
throw new MotechSchedulerException(format("Can not reschedule the job: %s The job does not exist (not scheduled)", jobId));
}
job = scheduler.getJobDetail(trigger.getJobKey());
} catch (SchedulerException e) {
throw new MotechSchedulerException(String.format("Can not reschedule the job: %s.\n Can not get a trigger associated with that job %s", jobId, e.getMessage()), e);
} catch (ClassCastException e) {
throw new MotechSchedulerException(String.format("Can not reschedule the job: %s.\n The trigger associated with that job is not a CronTrigger", jobId), e);
}
CronScheduleBuilder newCronSchedule;
try {
newCronSchedule = cronSchedule(cronExpression);
} catch (RuntimeException e) {
throw new MotechSchedulerException(String.format("Can not reschedule the job: %s Invalid Cron expression: %s", jobId, cronExpression), e);
}
CronTrigger newTrigger = newTrigger().withIdentity(trigger.getKey()).forJob(job).withSchedule(newCronSchedule).startAt(trigger.getStartTime()).endAt(trigger.getEndTime()).build();
try {
scheduler.rescheduleJob(triggerKey(jobId.value(), JOB_GROUP_NAME), newTrigger);
} catch (SchedulerException e) {
throw new MotechSchedulerException(String.format("Can not reschedule the job: %s %s", jobId, e.getMessage()), e);
}
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class JobIdTest method initializeUsingMotechEvent.
@Test
public void initializeUsingMotechEvent() {
MotechEvent motechEvent = new MotechEvent(SUBJECT_VALUE);
motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, JOB_ID_VALUE);
JobId jobId = new CronJobId(motechEvent);
assertEquals(String.format("%s-%s", SUBJECT_VALUE, JOB_ID_VALUE), jobId.value());
jobId = new RepeatingJobId(motechEvent);
assertEquals(String.format("%s-%s%s", SUBJECT_VALUE, JOB_ID_VALUE, RepeatingJobId.SUFFIX_REPEATJOBID), jobId.value());
}
use of org.motechproject.scheduler.contract.CronJobId in project motech by motech.
the class TestEventListener method shouldGetPreviousFireTime.
@Test
public void shouldGetPreviousFireTime() throws InterruptedException {
try {
fakeNow(new DateTime());
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);
DateTime now = new DateTime();
StringBuilder cron = new StringBuilder();
cron.append(now.getSecondOfMinute()).append(" ").append(now.getMinuteOfHour()).append(" ");
cron.append(now.getHourOfDay()).append(" * * ?");
schedulerService.scheduleJob(new CronSchedulableJob(event, cron.toString()));
Thread.sleep(1000);
DateTime dateTime = schedulerService.getPreviousFireDate(new CronJobId(event));
assertEquals(dateTime.getHourOfDay(), now.getHourOfDay());
assertEquals(dateTime.getMinuteOfHour(), now.getMinuteOfHour());
assertEquals(dateTime.getSecondOfMinute(), now.getSecondOfMinute());
} finally {
stopFakingTime();
}
}
Aggregations