Search in sources :

Example 6 with RunOnceSchedulableJob

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

the class MotechSchedulerDatabaseServiceImplBundleIT method shouldGetScheduledJobDetailedInfo.

@Test
public void shouldGetScheduledJobDetailedInfo() throws SchedulerException, SQLException {
    try {
        fakeNow(newDateTime(CURRENT_YEAR + 6, 7, 15, 10, 0, 0));
        JobDetailedInfo jobDetailedInfo = null;
        Map<String, Object> params = new HashMap<>();
        params.put(MotechSchedulerService.JOB_ID_KEY, "job_id_2");
        params.put("param1", "value1");
        params.put("param2", "value2");
        schedulerService.scheduleRunOnceJob(new RunOnceSchedulableJob(new MotechEvent("test_event_2", params), newDateTime(CURRENT_YEAR + 6, 7, 15, 12, 0, 0)));
        JobsSearchSettings jobsSearchSettings = getGridSettings(0, 10, "name", "asc");
        for (JobBasicInfo job : databaseService.getScheduledJobsBasicInfo(jobsSearchSettings)) {
            if (job.getName().equals("test_event_2-job_id_2-runonce")) {
                jobDetailedInfo = databaseService.getScheduledJobDetailedInfo(job);
            }
        }
        assertNotNull(jobDetailedInfo);
        assertEquals("test_event_2", jobDetailedInfo.getEventInfoList().get(0).getSubject());
        assertEquals(4, jobDetailedInfo.getEventInfoList().get(0).getParameters().size());
    } finally {
        stopFakingTime();
    }
}
Also used : JobDetailedInfo(org.motechproject.scheduler.contract.JobDetailedInfo) HashMap(java.util.HashMap) JobBasicInfo(org.motechproject.scheduler.contract.JobBasicInfo) MotechEvent(org.motechproject.event.MotechEvent) JobsSearchSettings(org.motechproject.scheduler.contract.JobsSearchSettings) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) Test(org.junit.Test)

Example 7 with RunOnceSchedulableJob

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

the class SchedulerBundleIT method testRunOnceJob.

@Test
public void testRunOnceJob() throws InterruptedException {
    waitForEventConsumerToStart();
    final List<String> receivedEvents = new ArrayList<>();
    eventRegistry.registerListener(new EventListener() {

        @Override
        public void handle(MotechEvent event) {
            synchronized (receivedEvents) {
                receivedEvents.add(event.getSubject());
                receivedEvents.notify();
            }
        }

        @Override
        public String getIdentifier() {
            return TEST_SUBJECT;
        }
    }, TEST_SUBJECT);
    final MotechEvent motechEvent = new MotechEvent(TEST_SUBJECT);
    motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, "jobId");
    schedulerService.unscheduleAllJobs("SchedulerBundleIT");
    schedulerService.scheduleRunOnceJob(new RunOnceSchedulableJob(motechEvent, DateTime.now().plusSeconds(5)));
    synchronized (receivedEvents) {
        System.out.print("\nEvent waiting " + new Date() + "\n");
        receivedEvents.wait(15000);
    }
    assertEquals(1, receivedEvents.size());
    assertEquals(receivedEvents.get(0), TEST_SUBJECT);
}
Also used : ArrayList(java.util.ArrayList) EventListener(org.motechproject.event.listener.EventListener) MotechEvent(org.motechproject.event.MotechEvent) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) Date(java.util.Date) Test(org.junit.Test)

Example 8 with RunOnceSchedulableJob

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

the class SchedulableJobValidatorTest method shouldThrowMotechSchedulerExceptionIfStartDateIsInThePast.

@Test(expected = MotechSchedulerException.class)
public void shouldThrowMotechSchedulerExceptionIfStartDateIsInThePast() {
    try {
        fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
        RunOnceSchedulableJob job = new RunOnceSchedulableJob(motechEvent, DateUtil.now().minusHours(1), false);
        SchedulableJobValidator.validateRunOnceSchedulableJob(job);
    } finally {
        stopFakingTime();
    }
}
Also used : RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) Test(org.junit.Test)

Example 9 with RunOnceSchedulableJob

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

the class SchedulableJobValidatorTest method shouldThrowIllegalArgumentExceptionIfMotechEventIsNullInRunOnceSchedulableJob.

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionIfMotechEventIsNullInRunOnceSchedulableJob() {
    try {
        fakeNow(newDateTime(2020, 7, 15, 10, 0, 0));
        RunOnceSchedulableJob job = new RunOnceSchedulableJob(null, DateUtil.now().plusHours(1), false);
        SchedulableJobValidator.validateRunOnceSchedulableJob(job);
    } finally {
        stopFakingTime();
    }
}
Also used : RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) Test(org.junit.Test)

Example 10 with RunOnceSchedulableJob

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

the class MotechSchedulerListener method handleScheduleRepeatingJobEvent.

/**
 * Handles the motech event scheduling a new repeating job.
 *
 * @param event the event to be handled
 */
@MotechListener(subjects = { SCHEDULE_REPEATING_JOB })
public void handleScheduleRepeatingJobEvent(MotechEvent event) {
    Map<String, Object> parameters = event.getParameters();
    Map<String, Object> metadata = event.getMetadata();
    String jobSubject = (String) metadata.get(JOB_SUBJECT);
    Integer jobStart = (Integer) metadata.get(JOB_START);
    // Reset jobID that is appended with each retry and becomes too long
    parameters.put(JOB_ID, null);
    MotechEvent jobEvent = new MotechEvent(jobSubject, parameters, null, metadata);
    RunOnceSchedulableJob runOnceJob = new RunOnceSchedulableJob(jobEvent, DateTime.now().plusSeconds(jobStart));
    schedulerService.scheduleRunOnceJob(runOnceJob);
}
Also used : MotechEvent(org.motechproject.event.MotechEvent) RunOnceSchedulableJob(org.motechproject.scheduler.contract.RunOnceSchedulableJob) MotechListener(org.motechproject.event.listener.annotations.MotechListener)

Aggregations

RunOnceSchedulableJob (org.motechproject.scheduler.contract.RunOnceSchedulableJob)15 Test (org.junit.Test)11 MotechEvent (org.motechproject.event.MotechEvent)10 HashMap (java.util.HashMap)7 CronSchedulableJob (org.motechproject.scheduler.contract.CronSchedulableJob)4 RepeatingSchedulableJob (org.motechproject.scheduler.contract.RepeatingSchedulableJob)4 DateTime (org.joda.time.DateTime)3 JobBasicInfo (org.motechproject.scheduler.contract.JobBasicInfo)3 JobsSearchSettings (org.motechproject.scheduler.contract.JobsSearchSettings)3 ArrayList (java.util.ArrayList)2 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)2 Date (java.util.Date)1 Period (org.joda.time.Period)1 Before (org.junit.Before)1 Time (org.motechproject.commons.date.model.Time)1 EventListener (org.motechproject.event.listener.EventListener)1 MotechListener (org.motechproject.event.listener.annotations.MotechListener)1 DayOfWeekSchedulableJob (org.motechproject.scheduler.contract.DayOfWeekSchedulableJob)1 JobDetailedInfo (org.motechproject.scheduler.contract.JobDetailedInfo)1 RepeatingPeriodSchedulableJob (org.motechproject.scheduler.contract.RepeatingPeriodSchedulableJob)1