Search in sources :

Example 1 with CronSchedulableJob

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

Example 2 with CronSchedulableJob

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

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

Example 4 with CronSchedulableJob

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

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

the class TestEventListener method shouldResumeJobIfItIsUiDefined.

@Test
public void shouldResumeJobIfItIsUiDefined() 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_PAUSED, "test_event-job_id", "default", "start-time", "nex-fire-time", "end-time", JobBasicInfo.JOBTYPE_CRON, "test-info", true);
    CronSchedulableJob job = new CronSchedulableJob(new MotechEvent("test_event", params), "0 0 12 * * ?");
    job.setUiDefined(true);
    schedulerService.scheduleJob(job);
    scheduler.pauseJob(new JobKey(info.getName(), info.getGroup()));
    assertEquals(PAUSED, scheduler.getTriggerState(triggerKey("test_event-job_id", "default")));
    schedulerService.resumeJob(info);
    assertEquals(NORMAL, scheduler.getTriggerState(triggerKey("test_event-job_id", "default")));
}
Also used : CronSchedulableJob(org.motechproject.scheduler.contract.CronSchedulableJob) JobKey(org.quartz.JobKey) HashMap(java.util.HashMap) JobBasicInfo(org.motechproject.scheduler.contract.JobBasicInfo) 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