Search in sources :

Example 21 with CronSchedulableJob

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

the class TestEventListener method shouldNotIgnoreFiresInPastWhenSchedulingCronJob.

@Test
public // See http://quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigJobStoreTX
void shouldNotIgnoreFiresInPastWhenSchedulingCronJob() throws InterruptedException, SchedulerException {
    final String eventSubject = id("eve");
    try {
        TestEventListener listener = new TestEventListener();
        eventRegistry.registerListener(listener, eventSubject);
        DateTime now = findSuitableTimeToScheduleWithSafeBufferFromTriggerTime();
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        DateTime jobStartTimeInPast = now.minusMinutes(3);
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent(eventSubject, params), "0 0/1 * 1/1 * ? *", jobStartTimeInPast, null, false));
        synchronized (listener.getReceivedEvents()) {
            listener.getReceivedEvents().wait(5000);
        }
        assertTrue("Listener didn't receive misfired events.", listener.getReceivedEvents().size() > 0);
    } finally {
        eventRegistry.clearListenersForBean(eventSubject);
        schedulerService.unscheduleAllJobs(eventSubject + "-job_id");
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) 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 22 with CronSchedulableJob

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

the class TestEventListener method shouldRescheduleCronJob.

@Test
public void shouldRescheduleCronJob() throws SchedulerException {
    try {
        fakeNow(new DateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        final String jobId = id("jobId");
        params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 10 * * ?"));
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 14 * * ?"));
        List<DateTime> first3FireTimes = getFireTimes("test_event-" + jobId).subList(0, 3);
        assertEquals(asList(newDateTime(2020, 7, 15, 14, 0, 0), newDateTime(2020, 7, 16, 14, 0, 0), newDateTime(2020, 7, 17, 14, 0, 0)), first3FireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) 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 23 with CronSchedulableJob

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

the class TestEventListener method shouldRescheduleCronJobWithNewSchedule.

@Test
public void shouldRescheduleCronJobWithNewSchedule() throws SchedulerException {
    try {
        fakeNow(new DateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 10 * * ?"));
        schedulerService.rescheduleJob("test_event", "job_id", "0 0 14 * * ?");
        List<DateTime> first3FireTimes = getFireTimes("test_event-job_id").subList(0, 3);
        assertEquals(asList(newDateTime(2020, 7, 15, 14, 0, 0), newDateTime(2020, 7, 16, 14, 0, 0), newDateTime(2020, 7, 17, 14, 0, 0)), first3FireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) 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 24 with CronSchedulableJob

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

the class TestEventListener method shouldDeleteJobIfItIsUiDefined.

@Test
public void shouldDeleteJobIfItIsUiDefined() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
    JobBasicInfo info = new JobBasicInfo(JobBasicInfo.ACTIVITY_ACTIVE, JobBasicInfo.STATUS_OK, "test_event-job_id", "default", "start-time", "nex-fire-time", "end-time", JobBasicInfo.JOBTYPE_CRON, "test-info", false);
    CronSchedulableJob job = new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 12 * * ?");
    job.setUiDefined(true);
    schedulerService.scheduleJob(job);
    schedulerService.deleteJob(info);
    assertNull(scheduler.getTrigger(triggerKey("test_event-job_id", "default")));
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) HashMap(java.util.HashMap) JobBasicInfo(org.motechproject.scheduler.contract.JobBasicInfo) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 25 with CronSchedulableJob

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

the class TestEventListener method shouldUnscheduleAllJobsWithAGivenJobIdPrefix.

@Test
public void shouldUnscheduleAllJobsWithAGivenJobIdPrefix() throws SchedulerException {
    Map<String, Object> params = new HashMap<>();
    params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
    schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event1", params), "0 0 12 * * ?"));
    schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event2", params), "0 0 13 * * ?"));
    schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event3", params), "0 0 14 * * ?"));
    schedulerService.unscheduleAllJobs("test_event");
    assertNull(scheduler.getTrigger(triggerKey("test_event1-job_id", "default")));
    assertNull(scheduler.getTrigger(triggerKey("test_event2-job_id", "default")));
    assertNull(scheduler.getTrigger(triggerKey("test_event3-job_id", "default")));
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) HashMap(java.util.HashMap) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Aggregations

CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)33 MotechEvent (org.motechproject.event.MotechEvent)27 Test (org.junit.Test)25 HashMap (java.util.HashMap)24 DateTime (org.joda.time.DateTime)10 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)10 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)8 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)4 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)4 JobKey (org.quartz.JobKey)3 Time (org.motechproject.commons.date.model.Time)2 CronJobId (org.motechproject.scheduler.contract.CronJobId)2 JobsSearchSettings (org.motechproject.scheduler.contract.JobsSearchSettings)2 ArrayList (java.util.ArrayList)1 Period (org.joda.time.Period)1 Before (org.junit.Before)1 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)1 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)1 SchedulableJobValidator.validateCronSchedulableJob (org.motechproject.scheduler.validation.SchedulableJobValidator.validateCronSchedulableJob)1 TimeFaker.stopFakingTime (org.motechproject.testing.utils.TimeFaker.stopFakingTime)1