Search in sources :

Example 11 with JobId

use of org.motechproject.scheduler.contract.JobId 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());
}
Also used : CronJobId(org.motechproject.scheduler.contract.CronJobId) JobId(org.motechproject.scheduler.contract.JobId) CronJobId(org.motechproject.scheduler.contract.CronJobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) Test(org.junit.Test)

Example 12 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class MotechSchedulerServiceImpl method safeScheduleRunOnceJob.

@Override
public void safeScheduleRunOnceJob(RunOnceSchedulableJob schedulableJob) {
    logObjectIfNotNull(schedulableJob);
    assertArgumentNotNull("RunOnceSchedulableJob", schedulableJob);
    JobId jobId = new RunOnceJobId(schedulableJob.getMotechEvent());
    try {
        unscheduleJob(jobId);
    } catch (MotechSchedulerException e) {
        LOGGER.error("Unable to unschedule run once job with ID {}", jobId.value(), e);
    }
    scheduleRunOnceJob(schedulableJob);
}
Also used : 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) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId)

Example 13 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class MotechSchedulerServiceImpl method unscheduleRepeatingJob.

@Override
public void unscheduleRepeatingJob(String subject, String externalId) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("unscheduling repeating job: " + LOG_SUBJECT_EXTERNAL_ID, subject, externalId));
    }
    JobId jobId = new RepeatingJobId(subject, externalId);
    logObjectIfNotNull(jobId);
    unscheduleJob(jobId.value());
}
Also used : RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) 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 14 with JobId

use of org.motechproject.scheduler.contract.JobId in project motech by motech.

the class MotechSchedulerServiceImpl method scheduleRepeatingJob.

private void scheduleRepeatingJob(RepeatingSchedulableJob job, boolean update) {
    logObjectIfNotNull(job);
    validateRepeatingSchedulableJob(job);
    MotechEvent motechEvent = job.getMotechEvent();
    DateTime jobStartTime = job.getStartDate();
    DateTime jobEndTime = job.getEndDate();
    Integer repeatIntervalInSeconds = job.getRepeatIntervalInSeconds();
    Integer jobRepeatCount = job.getRepeatCount();
    if (null == jobRepeatCount) {
        jobRepeatCount = MAX_REPEAT_COUNT;
    }
    JobId jobId = new RepeatingJobId(motechEvent);
    JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
    putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
    jobDetail.getJobDataMap().put(EVENT_METADATA, createMetadataForMisfireSchedulableJob(job, motechEvent));
    try {
        if (scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME)) != null) {
            unscheduleJob(jobId);
        }
    } 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);
    }
    ScheduleBuilder scheduleBuilder;
    if (!job.isUseOriginalFireTimeAfterMisfire()) {
        SimpleScheduleBuilder simpleSchedule = simpleSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withRepeatCount(jobRepeatCount);
        simpleSchedule = setMisfirePolicyForSimpleTrigger(simpleSchedule, schedulerSettings.getProperty("scheduler.repeating.trigger.misfire.policy"));
        scheduleBuilder = simpleSchedule;
    } else {
        if (job.getRepeatCount() != null) {
            final double half = 0.5;
            jobEndTime = new DateTime((long) (job.getStartDate().getMillis() + repeatIntervalInSeconds * MILLISECOND * (job.getRepeatCount() + half)));
        }
        scheduleBuilder = CalendarIntervalScheduleBuilder.calendarIntervalSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withMisfireHandlingInstructionFireAndProceed();
    }
    Trigger trigger = buildJobDetail(job, DateUtil.toDate(jobStartTime), DateUtil.toDate(jobEndTime), jobId, jobDetail, scheduleBuilder);
    scheduleJob(jobDetail, trigger, update);
}
Also used : JobDetail(org.quartz.JobDetail) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) PeriodIntervalScheduleBuilder(org.motechproject.scheduler.trigger.PeriodIntervalScheduleBuilder) ScheduleBuilder(org.quartz.ScheduleBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) CalendarIntervalScheduleBuilder(org.quartz.CalendarIntervalScheduleBuilder) CronScheduleBuilder(org.quartz.CronScheduleBuilder) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) MotechEvent(org.motechproject.event.MotechEvent) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) 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 15 with JobId

use of org.motechproject.scheduler.contract.JobId 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);
}
Also used : CronScheduleBuilder(org.quartz.CronScheduleBuilder) CronTrigger(org.quartz.CronTrigger) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) HashMap(java.util.HashMap) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Date(java.util.Date) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) CronJobId(org.motechproject.scheduler.contract.CronJobId) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) MotechEvent(org.motechproject.event.MotechEvent) 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)

Aggregations

JobId (org.motechproject.scheduler.contract.JobId)17 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)17 CronJobId (org.motechproject.scheduler.contract.CronJobId)16 RepeatingPeriodJobId (org.motechproject.scheduler.contract.RepeatingPeriodJobId)13 RunOnceJobId (org.motechproject.scheduler.contract.RunOnceJobId)13 MotechSchedulerException (org.motechproject.scheduler.exception.MotechSchedulerException)9 CronTrigger (org.quartz.CronTrigger)7 MotechEvent (org.motechproject.event.MotechEvent)6 SimpleTrigger (org.quartz.SimpleTrigger)6 Trigger (org.quartz.Trigger)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)6 OperableTrigger (org.quartz.spi.OperableTrigger)6 JobDetail (org.quartz.JobDetail)5 SchedulerException (org.quartz.SchedulerException)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)4 Test (org.junit.Test)3 SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 DateTime (org.joda.time.DateTime)2