use of org.motechproject.scheduler.exception.MotechSchedulerException in project motech by motech.
the class MotechSchedulerServiceImpl method resumeJob.
@Override
public JobBasicInfo resumeJob(JobBasicInfo info) {
try {
JobKey key = new JobKey(info.getName(), info.getGroup());
validateJob(key);
scheduler.resumeJob(key);
info.setStatus(JobBasicInfo.STATUS_OK);
return info;
} catch (MotechSchedulerException | SchedulerException e) {
throw new MotechSchedulerException(String.format("Can not resume the job:\n %s\n%s\n%s", info.getName(), info.getGroup(), e.getMessage()), e);
}
}
use of org.motechproject.scheduler.exception.MotechSchedulerException in project motech by motech.
the class MotechSchedulerServiceImpl method unscheduleAllJobs.
@Override
public void unscheduleAllJobs(String jobIdPrefix) {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Unscheduling jobs with prefix: ", jobIdPrefix);
}
List<TriggerKey> triggerKeys = new ArrayList<>(scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains(JOB_GROUP_NAME)));
List<String> triggerNames = extractTriggerNames(triggerKeys);
for (String triggerName : triggerNames) {
if (StringUtils.isNotEmpty(jobIdPrefix) && triggerName.contains(jobIdPrefix)) {
unscheduleJob(triggerName);
}
}
} catch (SchedulerException e) {
throw new MotechSchedulerException(String.format("Can not unschedule jobs given jobIdPrefix: %s %s", jobIdPrefix, e.getMessage()), e);
}
}
use of org.motechproject.scheduler.exception.MotechSchedulerException in project motech by motech.
the class MotechSchedulerServiceImpl method validateJob.
private void validateJob(JobKey key) throws SchedulerException {
JobDetail detail = scheduler.getJobDetail(key);
if (detail == null) {
throw new MotechSchedulerException(String.format("Job doesn't exist:\n %s\n %s", key.getName(), key.getGroup()));
}
JobDataMap map = detail.getJobDataMap();
if (map != null && !isJobUIDefined(map)) {
throw new MotechSchedulerException(String.format("Job is not ui defined:\n %s\n %s", key.getName(), key.getGroup()));
}
}
use of org.motechproject.scheduler.exception.MotechSchedulerException 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.exception.MotechSchedulerException in project motech by motech.
the class SchedulableJobValidator method validateRunOnceSchedulableJob.
/**
* Validates if the given {@code job} has motech event and start date fields set.
*
* @param job the job to be validated
*/
public static void validateRunOnceSchedulableJob(RunOnceSchedulableJob job) {
notNull(job, JOB_CANNOT_BE_NULL);
validateSchedulableJob(job);
DateTime startDate = job.getStartDate();
DateTime currentDate = DateUtil.now();
if (job.getStartDate().isBefore(currentDate)) {
String errorMessage = "Invalid RunOnceSchedulableJob. The job start date can not be in the past. \n" + " Job start date: " + startDate.toString() + " Attempted to schedule at:" + currentDate.toString();
throw new MotechSchedulerException(errorMessage, "scheduler.error.startDateInThePast", Arrays.asList(startDate.toString(), currentDate.toString()));
}
}
Aggregations