Search in sources :

Example 6 with MotechSchedulerException

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);
    }
}
Also used : JobKey(org.quartz.JobKey) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException)

Example 7 with MotechSchedulerException

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);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) ArrayList(java.util.ArrayList) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException)

Example 8 with MotechSchedulerException

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()));
    }
}
Also used : JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException)

Example 9 with MotechSchedulerException

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);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronJobId(org.motechproject.scheduler.contract.CronJobId) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) RepeatingPeriodJobId(org.motechproject.scheduler.contract.RepeatingPeriodJobId) CronJobId(org.motechproject.scheduler.contract.CronJobId)

Example 10 with MotechSchedulerException

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()));
    }
}
Also used : MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) DateTime(org.joda.time.DateTime)

Aggregations

MotechSchedulerException (org.motechproject.scheduler.exception.MotechSchedulerException)19 SchedulerException (org.quartz.SchedulerException)11 CronJobId (org.motechproject.scheduler.contract.CronJobId)9 JobId (org.motechproject.scheduler.contract.JobId)9 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)9 RepeatingPeriodJobId (org.motechproject.scheduler.contract.RepeatingPeriodJobId)9 RunOnceJobId (org.motechproject.scheduler.contract.RunOnceJobId)9 CronTrigger (org.quartz.CronTrigger)6 SimpleTrigger (org.quartz.SimpleTrigger)5 Trigger (org.quartz.Trigger)5 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 OperableTrigger (org.quartz.spi.OperableTrigger)5 DateTime (org.joda.time.DateTime)4 JobDetail (org.quartz.JobDetail)4 MotechEvent (org.motechproject.event.MotechEvent)3 CronScheduleBuilder (org.quartz.CronScheduleBuilder)3 JobKey (org.quartz.JobKey)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)2