Search in sources :

Example 1 with CronJobId

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);
}
Also used : 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 2 with CronJobId

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);
}
Also used : TriggerKey(org.quartz.TriggerKey) 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) ArrayList(java.util.ArrayList) 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) Date(java.util.Date)

Example 3 with CronJobId

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);
    }
}
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 4 with CronJobId

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

Example 5 with CronJobId

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();
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) CronJobId(org.motechproject.scheduler.contract.CronJobId) HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

CronJobId (org.motechproject.scheduler.contract.CronJobId)10 JobId (org.motechproject.scheduler.contract.JobId)7 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)7 RepeatingPeriodJobId (org.motechproject.scheduler.contract.RepeatingPeriodJobId)5 RunOnceJobId (org.motechproject.scheduler.contract.RunOnceJobId)5 MotechSchedulerException (org.motechproject.scheduler.exception.MotechSchedulerException)5 Test (org.junit.Test)4 MotechEvent (org.motechproject.event.MotechEvent)4 CronTrigger (org.quartz.CronTrigger)4 SchedulerException (org.quartz.SchedulerException)4 HashMap (java.util.HashMap)3 DateTime (org.joda.time.DateTime)3 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)3 SimpleTrigger (org.quartz.SimpleTrigger)3 Trigger (org.quartz.Trigger)3 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)3 OperableTrigger (org.quartz.spi.OperableTrigger)3 Date (java.util.Date)2 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2