Search in sources :

Example 1 with PipelineJobSchedule

use of gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule in project beneficiary-fhir-data by CMSgov.

the class PipelineManagerIT method runUninterruptibleJobsThenStop.

/**
 * Verifies that {@link PipelineManager#stop()} works, as expected.
 *
 * @throws Exception Any unhandled {@link Exception}s will cause this test case to fail.
 */
@Test
public void runUninterruptibleJobsThenStop() throws Exception {
    // Create the pipeline and have it run a mock job.
    PipelineJobRecordStore jobRecordStore = new PipelineJobRecordStore(PipelineTestUtils.get().getPipelineApplicationState().getMetrics());
    try (PipelineManager pipelineManager = new PipelineManager(PipelineTestUtils.get().getPipelineApplicationState().getMetrics(), jobRecordStore)) {
        MockJob mockJob = new MockJob(Optional.of(new PipelineJobSchedule(1, ChronoUnit.MILLIS)), false, () -> {
            return PipelineJobOutcome.WORK_DONE;
        });
        pipelineManager.registerJob(mockJob);
        jobRecordStore.submitPendingJob(MockJob.JOB_TYPE, null);
        // Wait until the mock job has started.
        Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isStarted()).findAny().isPresent());
        // Stop the pipeline. If this doesn't hang, we're good.
        pipelineManager.stop();
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) LoggerFactory(org.slf4j.LoggerFactory) SchedulerJob(gov.cms.bfd.pipeline.app.scheduler.SchedulerJob) PipelineJob(gov.cms.bfd.pipeline.sharedutils.PipelineJob) Callable(java.util.concurrent.Callable) VolunteerJob(gov.cms.bfd.pipeline.app.volunteer.VolunteerJob) Disabled(org.junit.jupiter.api.Disabled) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineTestUtils(gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PipelineJobOutcome(gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) Set(java.util.Set) PipelineJobType(gov.cms.bfd.pipeline.sharedutils.PipelineJobType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Slf4jReporter(com.codahale.metrics.Slf4jReporter) Optional(java.util.Optional) Awaitility(org.awaitility.Awaitility) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) Test(org.junit.jupiter.api.Test)

Example 2 with PipelineJobSchedule

use of gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule in project beneficiary-fhir-data by CMSgov.

the class PipelineManagerIT method runInterruptibleJobsThenStop.

/**
 * Verifies that {@link PipelineManager#stop()} works, as expected.
 *
 * @throws Exception Any unhandled {@link Exception}s will cause this test case to fail.
 */
@Test
public void runInterruptibleJobsThenStop() throws Exception {
    // Create the pipeline and have it run a mock job.
    PipelineJobRecordStore jobRecordStore = new PipelineJobRecordStore(PipelineTestUtils.get().getPipelineApplicationState().getMetrics());
    try (PipelineManager pipelineManager = new PipelineManager(PipelineTestUtils.get().getPipelineApplicationState().getMetrics(), jobRecordStore)) {
        MockJob mockJob = new MockJob(Optional.of(new PipelineJobSchedule(1, ChronoUnit.MILLIS)), () -> {
            // Add an artificial delay that we'll be able to measure.
            Thread.sleep(500);
            return PipelineJobOutcome.WORK_DONE;
        });
        pipelineManager.registerJob(mockJob);
        jobRecordStore.submitPendingJob(MockJob.JOB_TYPE, null);
        // Wait until the mock job has started.
        Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isStarted()).findAny().isPresent());
        // Stop the pipeline and then make sure that the job was actually interrupted.
        pipelineManager.stop();
        PipelineJobRecord<NullPipelineJobArguments> mockJobRecord = jobRecordStore.findMostRecent(MockJob.JOB_TYPE).get();
        assertTrue(mockJobRecord.getCanceledTime().isPresent());
        assertTrue(mockJobRecord.getDuration().get().toMillis() < 500);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) LoggerFactory(org.slf4j.LoggerFactory) SchedulerJob(gov.cms.bfd.pipeline.app.scheduler.SchedulerJob) PipelineJob(gov.cms.bfd.pipeline.sharedutils.PipelineJob) Callable(java.util.concurrent.Callable) VolunteerJob(gov.cms.bfd.pipeline.app.volunteer.VolunteerJob) Disabled(org.junit.jupiter.api.Disabled) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineTestUtils(gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PipelineJobOutcome(gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) Set(java.util.Set) PipelineJobType(gov.cms.bfd.pipeline.sharedutils.PipelineJobType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Slf4jReporter(com.codahale.metrics.Slf4jReporter) Optional(java.util.Optional) Awaitility(org.awaitility.Awaitility) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) Test(org.junit.jupiter.api.Test)

Example 3 with PipelineJobSchedule

use of gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule in project beneficiary-fhir-data by CMSgov.

the class PipelineManagerIT method runSuccessfulScheduledJob.

/**
 * Verifies that {@link PipelineManager} runs a successful mock scheduled job, as expected.
 *
 * @throws Exception Any unhandled {@link Exception}s will cause this test case to fail.
 */
@Test
public void runSuccessfulScheduledJob() throws Exception {
    // Create the pipeline and have it run a mock job.
    PipelineJobRecordStore jobRecordStore = new PipelineJobRecordStore(PipelineTestUtils.get().getPipelineApplicationState().getMetrics());
    try (PipelineManager pipelineManager = new PipelineManager(PipelineTestUtils.get().getPipelineApplicationState().getMetrics(), jobRecordStore)) {
        MockJob mockJob = new MockJob(Optional.of(new PipelineJobSchedule(1, ChronoUnit.MILLIS)), () -> PipelineJobOutcome.WORK_DONE);
        pipelineManager.registerJob(mockJob);
        jobRecordStore.submitPendingJob(MockJob.JOB_TYPE, null);
        // Wait until a completed iteration of the mock job can be found.
        Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isCompleted()).findAny().isPresent());
        // Verify that one of the completed mock job iterations looks correct.
        Optional<PipelineJobRecord<?>> mockJobRecord = jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isCompleted()).findAny();
        assertEquals(Optional.of(PipelineJobOutcome.WORK_DONE), mockJobRecord.get().getOutcome());
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) LoggerFactory(org.slf4j.LoggerFactory) SchedulerJob(gov.cms.bfd.pipeline.app.scheduler.SchedulerJob) PipelineJob(gov.cms.bfd.pipeline.sharedutils.PipelineJob) Callable(java.util.concurrent.Callable) VolunteerJob(gov.cms.bfd.pipeline.app.volunteer.VolunteerJob) Disabled(org.junit.jupiter.api.Disabled) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineTestUtils(gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PipelineJobOutcome(gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) Set(java.util.Set) PipelineJobType(gov.cms.bfd.pipeline.sharedutils.PipelineJobType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Slf4jReporter(com.codahale.metrics.Slf4jReporter) Optional(java.util.Optional) Awaitility(org.awaitility.Awaitility) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) Test(org.junit.jupiter.api.Test)

Example 4 with PipelineJobSchedule

use of gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule in project beneficiary-fhir-data by CMSgov.

the class SchedulerJob method call.

/**
 * @see gov.cms.bfd.pipeline.sharedutils.PipelineJob#call()
 */
@Override
public PipelineJobOutcome call() throws Exception {
    boolean scheduledAJob = false;
    while (true) {
        try (Timer.Context timer = appMetrics.timer(MetricRegistry.name(getClass().getSimpleName(), "call", "iteration")).time()) {
            Instant now = Instant.now();
            Set<PipelineJob<NullPipelineJobArguments>> scheduledJobs = pipelineManager.getScheduledJobs();
            for (PipelineJob<NullPipelineJobArguments> scheduledJob : scheduledJobs) {
                PipelineJobSchedule jobSchedule = scheduledJob.getSchedule().get();
                Optional<PipelineJobRecord<NullPipelineJobArguments>> mostRecentExecution = jobRecordsStore.findMostRecent(scheduledJob.getType());
                /* Calculate whether or not we should trigger an execution of the next job. */
                boolean shouldTriggerJob;
                if (!mostRecentExecution.isPresent()) {
                    // If the job has never run, we'll always trigger it now, regardless of schedule.
                    shouldTriggerJob = true;
                } else {
                    if (!mostRecentExecution.get().isCompleted()) {
                        // If the job's still pending or running, don't double-trigger it.
                        shouldTriggerJob = false;
                    } else {
                        if (mostRecentExecution.get().isCompletedSuccessfully()) {
                            // If the job's not running, check to see if it's time to trigger it again.
                            // Note: This calculation is based on completion time, not submission or start time.
                            Instant nextExecution = mostRecentExecution.get().getStartedTime().get().plus(jobSchedule.getRepeatDelay(), jobSchedule.getRepeatDelayUnit());
                            shouldTriggerJob = now.equals(nextExecution) || now.isAfter(nextExecution);
                        } else {
                            // We don't re-run failed jobs.
                            shouldTriggerJob = false;
                        }
                    }
                }
                // If we shouldn't trigger this job, move on to the next.
                if (!shouldTriggerJob) {
                    continue;
                }
                // Trigger the job (for future execution, when VolunteerJob picks it up)!
                jobRecordsStore.submitPendingJob(scheduledJob.getType(), null);
            }
        }
        try {
            Thread.sleep(SCHEDULER_TICK_MILLIS);
        } catch (InterruptedException e) {
            /*
         * Jobs are only interrupted/cancelled as part of application shutdown, so when encountered,
         * we'll break out of our scheduling loop and close up shop here.
         */
            break;
        }
    }
    /*
     * Did we schedule at least one job? If we ever move to an autoscaled version of this
     * application, it will be important to ensure that we "collude" with the PipelineJobRecordStore
     * to ignore this PipelineJobOutcome and ensure that the record doesn't get marked as completed,
     * even when the application shuts down. (If that happened, then scheduled triggers would stop
     * firing.)
     */
    return scheduledAJob ? PipelineJobOutcome.WORK_DONE : PipelineJobOutcome.NOTHING_TO_DO;
}
Also used : PipelineJob(gov.cms.bfd.pipeline.sharedutils.PipelineJob) Timer(com.codahale.metrics.Timer) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) Instant(java.time.Instant) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord)

Example 5 with PipelineJobSchedule

use of gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule in project beneficiary-fhir-data by CMSgov.

the class PipelineManagerIT method runFailingScheduledJob.

/**
 * Verifies that {@link PipelineManager} runs a failing mock scheduled job, as expected.
 *
 * @throws Exception Any unhandled {@link Exception}s will cause this test case to fail.
 */
@Test
public void runFailingScheduledJob() throws Exception {
    // Create the pipeline and have it run a mock job.
    PipelineJobRecordStore jobRecordStore = new PipelineJobRecordStore(PipelineTestUtils.get().getPipelineApplicationState().getMetrics());
    try (PipelineManager pipelineManager = new PipelineManager(PipelineTestUtils.get().getPipelineApplicationState().getMetrics(), jobRecordStore)) {
        MockJob mockJob = new MockJob(Optional.of(new PipelineJobSchedule(1, ChronoUnit.MILLIS)), () -> {
            throw new RuntimeException("boom");
        });
        pipelineManager.registerJob(mockJob);
        jobRecordStore.submitPendingJob(MockJob.JOB_TYPE, null);
        // Wait until a completed job can be found.
        Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isCompleted()).findAny().isPresent());
        // Verify that one of the completed mock job iterations looks correct.
        Optional<PipelineJobRecord<?>> mockJobRecord = jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isCompleted()).findAny();
        assertEquals(RuntimeException.class, mockJobRecord.get().getFailure().get().getType());
        assertEquals("boom", mockJobRecord.get().getFailure().get().getMessage());
        // Make sure that the job stopped trying to execute after it failed.
        assertEquals(1, jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType())).count());
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) LoggerFactory(org.slf4j.LoggerFactory) SchedulerJob(gov.cms.bfd.pipeline.app.scheduler.SchedulerJob) PipelineJob(gov.cms.bfd.pipeline.sharedutils.PipelineJob) Callable(java.util.concurrent.Callable) VolunteerJob(gov.cms.bfd.pipeline.app.volunteer.VolunteerJob) Disabled(org.junit.jupiter.api.Disabled) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineTestUtils(gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PipelineJobOutcome(gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) Set(java.util.Set) PipelineJobType(gov.cms.bfd.pipeline.sharedutils.PipelineJobType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Slf4jReporter(com.codahale.metrics.Slf4jReporter) Optional(java.util.Optional) Awaitility(org.awaitility.Awaitility) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) Test(org.junit.jupiter.api.Test)

Aggregations

NullPipelineJobArguments (gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments)6 PipelineJob (gov.cms.bfd.pipeline.sharedutils.PipelineJob)6 PipelineJobSchedule (gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule)6 PipelineJobRecord (gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord)6 MetricRegistry (com.codahale.metrics.MetricRegistry)5 Slf4jReporter (com.codahale.metrics.Slf4jReporter)5 SchedulerJob (gov.cms.bfd.pipeline.app.scheduler.SchedulerJob)5 VolunteerJob (gov.cms.bfd.pipeline.app.volunteer.VolunteerJob)5 PipelineJobOutcome (gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome)5 PipelineJobType (gov.cms.bfd.pipeline.sharedutils.PipelineJobType)5 PipelineTestUtils (gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils)5 PipelineJobRecordStore (gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore)5 BadCodeMonkeyException (gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException)5 ChronoUnit (java.time.temporal.ChronoUnit)5 Optional (java.util.Optional)5 Set (java.util.Set)5 Callable (java.util.concurrent.Callable)5 TimeUnit (java.util.concurrent.TimeUnit)5 Collectors (java.util.stream.Collectors)5 Awaitility (org.awaitility.Awaitility)5