Search in sources :

Example 6 with CronJobId

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

the class TestEventListener method shouldGetNextFireTime.

@Test
public void shouldGetNextFireTime() {
    try {
        DateTime fireDate = new DateTime(2020, 7, 15, 10, 0, 0);
        fakeNow(fireDate);
        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);
        schedulerService.scheduleJob(new CronSchedulableJob(event, "0 0 10 * * ?"));
        DateTime dateTime = schedulerService.getNextFireDate(new CronJobId(event));
        assertEquals(fireDate, dateTime);
    } 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)

Example 7 with CronJobId

use of org.motechproject.scheduler.contract.CronJobId 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 8 with CronJobId

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

the class MotechScheduler method unscheduleTestEvent.

private static void unscheduleTestEvent() {
    try {
        LOGGER.info("Unscheduling the test job: " + TEST_EVENT_NAME);
        schedulerService.unscheduleJob(new CronJobId(TEST_SUBJECT, TEST_EVENT_NAME));
    } catch (RuntimeException e) {
        LOGGER.error(String.format("Can not unschedule the test job %s:", TEST_EVENT_NAME), e);
    }
}
Also used : CronJobId(org.motechproject.scheduler.contract.CronJobId)

Example 9 with CronJobId

use of org.motechproject.scheduler.contract.CronJobId 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)

Example 10 with CronJobId

use of org.motechproject.scheduler.contract.CronJobId 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)

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