Search in sources :

Example 1 with RepeatingSchedulableJob

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

the class MotechSchedulerActionProxyServiceImpl method scheduleRepeatingJob.

@Override
public // NO CHECKSTYLE More than 7 parameters (found 8).
void scheduleRepeatingJob(// NO CHECKSTYLE More than 7 parameters (found 8).
String subject, Map<Object, Object> parameters, DateTime startTime, DateTime endTime, Integer repeatCount, Integer repeatIntervalInSeconds, Boolean ignorePastFiresAtStart, Boolean useOriginalFireTimeAfterMisfire) {
    MotechEvent motechEvent = new MotechEvent(subject, createMotechEventParameters(parameters));
    RepeatingSchedulableJob job = new RepeatingSchedulableJob();
    job.setMotechEvent(motechEvent);
    job.setStartDate(startTime);
    job.setEndDate(endTime);
    job.setRepeatCount(repeatCount);
    job.setRepeatIntervalInSeconds(repeatIntervalInSeconds);
    job.setIgnorePastFiresAtStart(ignorePastFiresAtStart);
    job.setUseOriginalFireTimeAfterMisfire(useOriginalFireTimeAfterMisfire);
    scheduler.scheduleRepeatingJob(job);
}
Also used : RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) MotechEvent(org.motechproject.event.MotechEvent)

Example 2 with RepeatingSchedulableJob

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

the class MotechSchedulerServiceImpl method buildJobDetail.

private Trigger buildJobDetail(SchedulableJob schedulableJob, Date jobStartTime, Date jobEndTime, JobId jobId, JobDetail jobDetail, ScheduleBuilder scheduleBuilder) {
    Trigger trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(jobStartTime).endAt(jobEndTime).build();
    DateTime now = now();
    if (schedulableJob.isIgnorePastFiresAtStart() && newDateTime(jobStartTime).isBefore(now)) {
        List<Date> pastTriggers = TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger, null, jobStartTime, now.toDate());
        if (pastTriggers.size() > 0) {
            if (scheduleBuilder instanceof SimpleScheduleBuilder && ((RepeatingSchedulableJob) schedulableJob).getRepeatCount() != null) {
                ((SimpleScheduleBuilder) scheduleBuilder).withRepeatCount(((RepeatingSchedulableJob) schedulableJob).getRepeatCount() - pastTriggers.size());
            }
            Date newStartTime = getFirstTriggerInFuture(trigger, now);
            trigger = newTrigger().withIdentity(triggerKey(jobId.value(), JOB_GROUP_NAME)).forJob(jobDetail).withSchedule(scheduleBuilder).startAt(newStartTime).endAt(jobEndTime).build();
        }
    }
    return trigger;
}
Also used : SchedulableJobValidator.validateRepeatingSchedulableJob(org.motechproject.scheduler.validation.SchedulableJobValidator.validateRepeatingSchedulableJob) RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 3 with RepeatingSchedulableJob

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

the class TestListener method scheduleJobs.

private void scheduleJobs(MotechSchedulerFactoryBean schedulerFactoryBean, SettingsFacade settings) throws InterruptedException, SchedulerException {
    Scheduler scheduler = schedulerFactoryBean.getQuartzScheduler();
    scheduler.clear();
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("foo", "bar");
        params.put("fii", "bor");
        params.put("fuu", "baz");
        String eventSubject = "test_event";
        MotechEvent motechEvent = new MotechEvent(eventSubject, params);
        int maxJobs = 1;
        int repeatCount = 1;
        TestListener listener = new TestListener("test_listener", maxJobs, repeatCount);
        eventListenerRegistryService.registerListener(listener, eventSubject);
        DateTime startTime = now().plusSeconds(0);
        Integer repeatInterval = 1;
        System.out.println("startTime: " + startTime);
        NanoStopWatch timeToSchedule = new NanoStopWatch().start();
        for (int i = 0; i < maxJobs; i++) {
            params.put(MotechSchedulerService.JOB_ID_KEY, "test_job_" + String.valueOf(i));
            RepeatingSchedulableJob repeatingJob = new RepeatingSchedulableJob(motechEvent, repeatCount, repeatInterval, startTime, null, false);
            repeatingJob.setUseOriginalFireTimeAfterMisfire(false);
            schedulerService.scheduleRepeatingJob(repeatingJob);
        }
        System.out.println(format("Time to schedule : %dms", timeToSchedule.duration()));
        scheduler.start();
        synchronized (listener) {
            while (scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains("default")).size() > 0) {
                listener.wait(5000);
            }
        }
        System.out.println(listener.getReport());
        assertEquals(maxJobs * (repeatCount + 1), listener.getCount());
    } finally {
        eventListenerRegistryService.clearListenersForBean("test_listener");
        schedulerFactoryBean.getQuartzScheduler().standby();
    }
}
Also used : RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) NanoStopWatch(org.motechproject.commons.api.NanoStopWatch) HashMap(java.util.HashMap) Scheduler(org.quartz.Scheduler) MotechEvent(org.motechproject.event.MotechEvent) DateTime(org.joda.time.DateTime)

Example 4 with RepeatingSchedulableJob

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

the class TestEventListener method shouldScheduleInterveningRepeatJobWithoutEndDate.

@Test
public void shouldScheduleInterveningRepeatJobWithoutEndDate() throws SchedulerException {
    try {
        fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        RepeatingSchedulableJob repeatJob = new RepeatingSchedulableJob(new MotechEvent("test_event", params), 3, DateTimeConstants.SECONDS_PER_DAY, newDateTime(2020, 7, 13, 12, 0, 0), null, true);
        repeatJob.setUseOriginalFireTimeAfterMisfire(false);
        schedulerService.scheduleRepeatingJob(repeatJob);
        List<DateTime> fireTimes = getFireTimes("test_event-job_id-repeat");
        assertEquals(asList(newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 16, 12, 0, 0)), fireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) 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 5 with RepeatingSchedulableJob

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

the class TestEventListener method shouldScheduleRepeatJobBoundByEndDate.

@Test
public void shouldScheduleRepeatJobBoundByEndDate() throws SchedulerException {
    try {
        fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
        final String jobId = id("jobId");
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
        schedulerService.scheduleRepeatingJob(new RepeatingSchedulableJob(new MotechEvent("test_event", params), DateTimeConstants.SECONDS_PER_DAY, newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 18, 12, 0, 0), false));
        List<DateTime> fireTimes = getFireTimes("test_event-" + jobId + "-repeat");
        assertEquals(asList(newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 16, 12, 0, 0), newDateTime(2020, 7, 17, 12, 0, 0)), fireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : RepeatingSchedulableJob(org.motechproject.scheduler.contract.RepeatingSchedulableJob) 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

RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)20 Test (org.junit.Test)14 HashMap (java.util.HashMap)12 MotechEvent (org.motechproject.event.MotechEvent)12 DateTime (org.joda.time.DateTime)7 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)6 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)4 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)4 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)3 ArrayList (java.util.ArrayList)2 Period (org.joda.time.Period)2 JobsSearchSettings (org.motechproject.scheduler.contract.JobsSearchSettings)2 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)2 SimpleTrigger (org.quartz.SimpleTrigger)2 Date (java.util.Date)1 Before (org.junit.Before)1 NanoStopWatch (org.motechproject.commons.api.NanoStopWatch)1 Time (org.motechproject.commons.date.model.Time)1 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)1 SchedulableJobValidator.validateRepeatingSchedulableJob (org.motechproject.scheduler.validation.SchedulableJobValidator.validateRepeatingSchedulableJob)1