Search in sources :

Example 26 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestEventListener method shouldScheduleCronJob.

@Test
public void shouldScheduleCronJob() throws SchedulerException {
    try {
        fakeNow(new DateTime(2020, 7, 15, 10, 0, 0));
        Map<String, Object> params = new HashMap<>();
        final String job_id = id("job_id");
        params.put(MotechSchedulerService.JOB_ID_KEY, job_id);
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 10 * * ?"));
        List<DateTime> first3FireTimes = getFireTimes("test_event-" + job_id).subList(0, 3);
        assertEquals(asList(newDateTime(2020, 7, 15, 10, 0, 0), newDateTime(2020, 7, 16, 10, 0, 0), newDateTime(2020, 7, 17, 10, 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 27 with MotechEvent

use of org.motechproject.event.MotechEvent 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 28 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestEventListener method shouldScheduleDayOfWeekJob.

@Test
public void shouldScheduleDayOfWeekJob() 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");
        schedulerService.scheduleDayOfWeekJob(new DayOfWeekSchedulableJob(new MotechEvent("test_event", params), // friday
        newDateTime(2020, 7, 10), newDateTime(2020, 7, 22), asList(DayOfWeek.Monday, DayOfWeek.Thursday), new Time(10, 10), false));
        List<DateTime> fireTimes = getFireTimes("test_event-job_id");
        assertEquals(asList(newDateTime(2020, 7, 13, 10, 10, 0), newDateTime(2020, 7, 16, 10, 10, 0), newDateTime(2020, 7, 20, 10, 10, 0)), fireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : HashMap(java.util.HashMap) DayOfWeekSchedulableJob(org.motechproject.scheduler.contract.DayOfWeekSchedulableJob) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) Time(org.motechproject.commons.date.model.Time) DateTime(org.joda.time.DateTime) TimeFaker.stopFakingTime(org.motechproject.testing.utils.TimeFaker.stopFakingTime) MotechEvent(org.motechproject.event.MotechEvent) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 29 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestEventListener method shouldScheduleRepeatingPeriodSchedulableJob.

@Test
public void shouldScheduleRepeatingPeriodSchedulableJob() 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.scheduleRepeatingPeriodJob(new RepeatingPeriodSchedulableJob(new MotechEvent("test_event_3", params), newDateTime(2020, 7, 15, 12, 0, 0), newDateTime(2020, 7, 16, 12, 0, 0), new Period(4, 0, 0, 0), true));
        List<DateTime> fireTimes = getFireTimes("test_event_3-job_id-period");
        assertEquals(asList(new DateTime(2020, 7, 15, 12, 0, 0), new DateTime(2020, 7, 15, 16, 0, 0), new DateTime(2020, 7, 15, 20, 0, 0), new DateTime(2020, 7, 16, 0, 0, 0), new DateTime(2020, 7, 16, 4, 0, 0), new DateTime(2020, 7, 16, 8, 0, 0), new DateTime(2020, 7, 16, 12, 0, 0)), fireTimes);
    } finally {
        stopFakingTime();
    }
}
Also used : RepeatingPeriodSchedulableJob(org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob) HashMap(java.util.HashMap) Period(org.joda.time.Period) MotechEvent(org.motechproject.event.MotechEvent) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 30 with MotechEvent

use of org.motechproject.event.MotechEvent in project motech by motech.

the class TestEventListener method shouldIgnoreFiresInPastWhenSchedulingCronJob.

@Test
public void shouldIgnoreFiresInPastWhenSchedulingCronJob() throws InterruptedException, SchedulerException {
    String subject = "cron_ignore_misfire";
    try {
        TestEventListener listener = new TestEventListener();
        eventRegistry.registerListener(listener, subject);
        DateTime now;
        for (now = now(); now.getSecondOfMinute() > 55 || now.getSecondOfMinute() < 5; now = now()) {
            // we don't want triggers now, only misfires
            Thread.sleep(1000);
        }
        DateTime jobStartTime = now.minusMinutes(3);
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id");
        schedulerService.scheduleJob(new CronSchedulableJob(new MotechEvent(subject, params), "0 0/1 * 1/1 * ? *", jobStartTime, null, true));
        synchronized (listener.getReceivedEvents()) {
            listener.getReceivedEvents().wait(2000);
        }
        assertTrue(listener.getReceivedEvents().size() == 0);
    } finally {
        eventRegistry.clearListenersForBean("test");
        schedulerService.unscheduleAllJobs(subject);
    }
}
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)

Aggregations

MotechEvent (org.motechproject.event.MotechEvent)138 Test (org.junit.Test)87 HashMap (java.util.HashMap)79 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)28 DateTime (org.joda.time.DateTime)25 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)20 ArrayList (java.util.ArrayList)14 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)13 RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)11 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)10 TaskActionInformation (org.motechproject.tasks.domain.mds.task.TaskActionInformation)8 Matchers.anyString (org.mockito.Matchers.anyString)7 EventListener (org.motechproject.event.listener.EventListener)7 CronJobId (org.motechproject.scheduler.contract.CronJobId)7 Task (org.motechproject.tasks.domain.mds.task.Task)7 JobId (org.motechproject.scheduler.contract.JobId)6 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)6 Time (org.motechproject.commons.date.model.Time)5 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)5 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)5