Search in sources :

Example 16 with MotechSchedulerException

use of org.motechproject.scheduler.exception.MotechSchedulerException in project motech by motech.

the class MotechSchedulerServiceImpl method deleteJob.

@Override
public void deleteJob(JobBasicInfo info) {
    try {
        JobKey key = new JobKey(info.getName(), info.getGroup());
        validateJob(key);
        scheduler.deleteJob(key);
    } catch (MotechSchedulerException | SchedulerException e) {
        throw new MotechSchedulerException(String.format("Can not delete 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 17 with MotechSchedulerException

use of org.motechproject.scheduler.exception.MotechSchedulerException in project motech by motech.

the class MotechSchedulerServiceImpl method scheduleJob.

private void scheduleJob(JobDetail jobDetail, Trigger trigger, boolean update) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Scheduling job:" + jobDetail);
    }
    try {
        Set<Trigger> triggerSet = new HashSet<>();
        triggerSet.add(trigger);
        scheduler.scheduleJob(jobDetail, triggerSet, update);
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(String.format("Can not schedule the job:\n %s\n%s\n%s", jobDetail.toString(), trigger.toString(), e.getMessage()), "scheduler.error.schedulerError", Arrays.asList(e.getMessage()), e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) HashSet(java.util.HashSet)

Example 18 with MotechSchedulerException

use of org.motechproject.scheduler.exception.MotechSchedulerException 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);
    }
}
Also used : OperableTrigger(org.quartz.spi.OperableTrigger) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronJobId(org.motechproject.scheduler.contract.CronJobId) BaseCalendar(org.quartz.impl.calendar.BaseCalendar) 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 19 with MotechSchedulerException

use of org.motechproject.scheduler.exception.MotechSchedulerException in project motech by motech.

the class SchedulableJobBuilder method buildJob.

/**
 * Builds a job based on the given key, data map and trigger.
 *
 * @param key  the job key
 * @param dataMap  the job data map
 * @param trigger  the job trigger
 * @return  the created job
 * @throws SchedulerException when there were problems while building job
 */
public static SchedulableJob buildJob(JobKey key, JobDataMap dataMap, Trigger trigger) throws SchedulerException {
    SchedulableJob job;
    Map<String, Object> params = dataMap.getWrappedMap();
    Map<String, Object> metadata = (Map<String, Object>) params.get(EVENT_METADATA);
    params.remove(EVENT_METADATA);
    params.putAll(metadata);
    JobDataMap map = new JobDataMap(params);
    switch(getJobType(key, map)) {
        case CRON:
            job = buildCronSchedulableJob(trigger, map);
            break;
        case REPEATING:
            job = buildRepeatingSchedulableJob(trigger, map);
            break;
        case REPEATING_PERIOD:
            job = buildRepeatingPeriodSchedulableJob(trigger, map);
            break;
        case DAY_OF_WEEK:
            job = buildDayOfWeekSchedulableJob(trigger, map);
            break;
        case RUN_ONCE:
            job = buildRunOnceSchedulableJob();
            break;
        default:
            throw new MotechSchedulerException(String.format("Unknown job type: \n %s\n %s", key.getName(), key.getGroup()));
    }
    job.setMotechEvent(new MotechEvent(dataMap.getString(EVENT_TYPE_KEY_NAME), map.getWrappedMap()));
    job.setUiDefined(map.getBoolean(UI_DEFINED));
    job.setStartDate(new DateTime(trigger.getStartTime()));
    return job;
}
Also used : RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) SchedulableJob(org.motechproject.scheduler.contract.SchedulableJob) RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) RepeatingPeriodSchedulableJob(org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob) CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) DayOfWeekSchedulableJob(org.motechproject.scheduler.contract.DayOfWeekSchedulableJob) JobDataMap(org.quartz.JobDataMap) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap) MotechEvent(org.motechproject.event.MotechEvent) 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