Search in sources :

Example 21 with StageEntity

use of pipelite.entity.StageEntity in project pipelite by enasequence.

the class StageService method createExecution.

/**
 * Assigns a stage entity to the stage. Uses a saved stage entity if it exists or creates a new
 * one. Saves the stage.
 *
 * @param pipelineName the pipeline name
 * @param processId the process id
 * @param stage the stage
 */
@Timed("pipelite.transactional")
public void createExecution(String pipelineName, String processId, Stage stage) {
    // Uses saved stage if it exists.
    Optional<StageEntity> savedStageEntity = repository.findById(new StageEntityId(processId, pipelineName, stage.getStageName()));
    if (savedStageEntity.isPresent()) {
        stage.setStageEntity(savedStageEntity.get());
    }
    StageEntity.createExecution(pipelineName, processId, stage);
    if (stage.getStageEntity() == null) {
        throw new PipeliteException("Failed to create new stage entity");
    }
    saveStage(stage);
}
Also used : StageEntityId(pipelite.entity.StageEntityId) PipeliteException(pipelite.exception.PipeliteException) StageEntity(pipelite.entity.StageEntity) Timed(io.micrometer.core.annotation.Timed)

Example 22 with StageEntity

use of pipelite.entity.StageEntity in project pipelite by enasequence.

the class StageService method saveStage.

/**
 * Saves the stage.
 *
 * @param stage the stage to save
 * @return the saved stage entity
 */
@Timed("pipelite.transactional")
public StageEntity saveStage(Stage stage) {
    prepareSaveStage(stage);
    StageEntity stageEntity = stage.getStageEntity();
    log.atFiner().log("Saving stage: " + stageEntity.toString());
    return repository.save(stageEntity);
}
Also used : StageEntity(pipelite.entity.StageEntity) Timed(io.micrometer.core.annotation.Timed)

Example 23 with StageEntity

use of pipelite.entity.StageEntity in project pipelite by enasequence.

the class TestType method spyStageService.

public static void spyStageService(StageService stageServiceSpy) {
    if (!Mockito.mockingDetails(stageServiceSpy).isSpy()) {
        throw new RuntimeException("StageService must be a spy");
    }
    // Last and next exit code
    doAnswer(invocation -> {
        StageEntity stageEntity = (StageEntity) invocation.callRealMethod();
        AtomicReference<TestType> testTypeRef = new AtomicReference<>();
        handleErrors(testTypeRef, () -> {
            Stage stage = invocation.getArgument(0);
            StageExecutorResult result = invocation.getArgument(1);
            StageMapKey key = registeredKey(stage);
            TestType testType = testType(key);
            testTypeRef.set(testType);
            String lastExitCode = result.getAttribute(StageExecutorResultAttribute.EXIT_CODE);
            if (lastExitCode == null) {
                testType.failedAsserts.add("Stage has no exit code");
            } else if (!lastExitCode.equals(stageNextExitCode.get(key))) {
                testType.failedAsserts.add("Unexpected stage exit code " + lastExitCode + " and not " + stageNextExitCode.get(key));
            }
            setLastExitCode(lastExitCode, key);
            setNextExitCode(testType, stage, key);
        });
        return stageEntity;
    }).when(stageServiceSpy).endExecution(any(), any());
    // Stage deserialization
    doAnswer(invocation -> {
        Stage stage = invocation.getArgument(0);
        StageEntity savedStageEntity = (StageEntity) invocation.callRealMethod();
        AtomicReference<TestType> testTypeRef = new AtomicReference<>();
        handleErrors(testTypeRef, () -> {
            StageMapKey key = registeredKey(stage);
            TestType testType = testType(key);
            testTypeRef.set(testType);
            if (!stage.getStageEntity().equals(savedStageEntity)) {
                testType.failedAsserts.add("Stage entity " + stage.getStageEntity().toString() + " is different from saved stage entity: " + savedStageEntity.toString());
            }
        });
        return null;
    }).when(stageServiceSpy).saveStage(any());
}
Also used : StageExecutorResult(pipelite.stage.executor.StageExecutorResult) Stage(pipelite.stage.Stage) AtomicReference(java.util.concurrent.atomic.AtomicReference) StageEntity(pipelite.entity.StageEntity)

Example 24 with StageEntity

use of pipelite.entity.StageEntity in project pipelite by enasequence.

the class RetryServiceTest method retryFailedSchedule.

@Test
public void retryFailedSchedule() {
    String serviceName = PipeliteIdCreator.serviceName();
    String processId = PipeliteIdCreator.processId();
    RegisteredPipeline registeredPipeline = registeredPipelineService.getRegisteredPipeline(SCHEDULE_NAME);
    Process process = ProcessFactory.create(processId, registeredPipeline);
    // Failed process
    process.setProcessEntity(processService.createExecution(SCHEDULE_NAME, processId, 1));
    processService.startExecution(process.getProcessEntity());
    ProcessEntity processEntity = processService.endExecution(process, ProcessState.FAILED);
    // Failed stage
    Stage stage = process.getStage(STAGE_NAME).get();
    stageService.createExecution(SCHEDULE_NAME, processId, stage);
    stageService.startExecution(stage);
    stageService.endExecution(stage, StageExecutorResult.error());
    StageEntity stageEntity = stage.getStageEntity();
    assertThat(processEntity.getProcessState()).isEqualTo(ProcessState.FAILED);
    assertThat(stageEntity.getStageState()).isEqualTo(StageState.ERROR);
    // Failed schedule
    scheduleService.createSchedule(serviceName, SCHEDULE_NAME, CRON);
    ScheduleEntity scheduleEntity = scheduleService.startExecution(SCHEDULE_NAME, processId);
    ZonedDateTime nextTime = CronUtils.launchTime(CRON, ZonedDateTime.now().plusHours(1));
    scheduleEntity = scheduleService.endExecution(processEntity, nextTime);
    assertThat(scheduleEntity.isFailed()).isTrue();
    // Create schedule runner
    processRunnerPoolManager._createScheduleRunner();
    ScheduleRunner scheduleRunner = runnerService.getScheduleRunner();
    scheduleRunner.setMaximumExecutions(SCHEDULE_NAME, 1);
    assertThat(scheduleRunner.getScheduleCrons().size()).isOne();
    assertThat(scheduleRunner.getScheduleCrons().get(0).getPipelineName()).isEqualTo(SCHEDULE_NAME);
    // Check schedule state
    assertSetupSchedule(serviceName, SCHEDULE_NAME, processId, CRON, nextTime);
    ZonedDateTime retryTime = ZonedDateTime.now();
    Time.wait(Duration.ofMillis(1000));
    scheduleRunner.startUp();
    // Retry
    retryService.retry(SCHEDULE_NAME, processId);
    while (!scheduleRunner.isIdle()) {
        scheduleRunner.runOneIteration();
        Time.wait(Duration.ofMillis(100));
    }
    // Check schedule state
    assertRetriedSchedule(serviceName, SCHEDULE_NAME, processId, CRON, retryTime);
    // Check process state
    processEntity = processService.getSavedProcess(SCHEDULE_NAME, processId).get();
    assertThat(processEntity.getPipelineName()).isEqualTo(SCHEDULE_NAME);
    assertThat(processEntity.getProcessId()).isEqualTo(processId);
    assertThat(processEntity.getStartTime()).isNotNull();
    assertThat(processEntity.getEndTime()).isNotNull();
    assertThat(processEntity.getProcessState()).isEqualTo(ProcessState.COMPLETED);
    // Check stage state
    stageEntity = stageService.getSavedStage(SCHEDULE_NAME, processId, STAGE_NAME).get();
    assertThat(stageEntity.getPipelineName()).isEqualTo(SCHEDULE_NAME);
    assertThat(stageEntity.getProcessId()).isEqualTo(processId);
    assertThat(stageEntity.getStageName()).isEqualTo(STAGE_NAME);
    assertThat(stageEntity.getStageState()).isEqualTo(StageState.SUCCESS);
    assertThat(stageEntity.getStartTime()).isNotNull();
    assertThat(stageEntity.getEndTime()).isNotNull();
}
Also used : ScheduleRunner(pipelite.runner.schedule.ScheduleRunner) ZonedDateTime(java.time.ZonedDateTime) ProcessEntity(pipelite.entity.ProcessEntity) Stage(pipelite.stage.Stage) Process(pipelite.process.Process) StageEntity(pipelite.entity.StageEntity) ScheduleEntity(pipelite.entity.ScheduleEntity) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 25 with StageEntity

use of pipelite.entity.StageEntity in project pipelite by enasequence.

the class PipelineRunnerFailureTest method assertStageEntities.

private void assertStageEntities(TestPipeline f, String processId) {
    String pipelineName = f.pipelineName();
    int stageCnt = 4;
    for (int i = 0; i < stageCnt; ++i) {
        StageEntity stageEntity = stageService.getSavedStage(f.pipelineName(), processId, "STAGE" + i).get();
        assertThat(stageEntity.getPipelineName()).isEqualTo(pipelineName);
        assertThat(stageEntity.getProcessId()).isEqualTo(processId);
        TestProcess t = f.testProcessConfiguration();
        if ((i > 0 && t.stageTestResult == StageTestResult.FIRST_ERROR) || (i > 1 && t.stageTestResult == StageTestResult.SECOND_ERROR) || (i > 2 && t.stageTestResult == StageTestResult.THIRD_ERROR) || (i > 3 && t.stageTestResult == StageTestResult.FOURTH_ERROR)) {
            // Stage has been created but not executed.
            assertThat(stageEntity.getExecutionCount()).isZero();
            assertThat(stageEntity.getStartTime()).isNull();
            assertThat(stageEntity.getEndTime()).isNull();
            assertThat(stageEntity.getExecutorName()).isEqualTo("pipelite.executor.SyncTestExecutor");
            assertThat(stageEntity.getExecutorData()).isNull();
            assertThat(stageEntity.getExecutorParams()).isEqualTo("{\n" + "  \"timeout\" : 10000,\n" + "  \"maximumRetries\" : 0,\n" + "  \"immediateRetries\" : 0,\n" + "  \"logLines\" : 1000\n" + "}");
            assertThat(stageEntity.getStageState()).isEqualTo(StageState.PENDING);
            assertThat(stageEntity.getResultParams()).isNull();
        } else {
            // Stage has been executed.
            assertThat(stageEntity.getExecutionCount()).isEqualTo(1);
            assertThat(stageEntity.getStartTime()).isNotNull();
            assertThat(stageEntity.getEndTime()).isNotNull();
            assertThat(stageEntity.getStartTime()).isBeforeOrEqualTo(stageEntity.getEndTime());
            assertThat(stageEntity.getExecutorName()).isEqualTo("pipelite.executor.SyncTestExecutor");
            assertThat(stageEntity.getExecutorData()).isNull();
            assertThat(stageEntity.getExecutorParams()).isEqualTo("{\n" + "  \"timeout\" : 10000,\n" + "  \"maximumRetries\" : 0,\n" + "  \"immediateRetries\" : 0,\n" + "  \"logLines\" : 1000\n" + "}");
            if ((i == 0 && t.stageTestResult == StageTestResult.FIRST_ERROR) || (i == 1 && t.stageTestResult == StageTestResult.SECOND_ERROR) || (i == 2 && t.stageTestResult == StageTestResult.THIRD_ERROR) || (i == 3 && t.stageTestResult == StageTestResult.FOURTH_ERROR)) {
                assertThat(stageEntity.getStageState()).isEqualTo(StageState.ERROR);
                assertThat(stageEntity.getResultParams()).isNull();
            } else {
                assertThat(stageEntity.getStageState()).isEqualTo(StageState.SUCCESS);
                assertThat(stageEntity.getResultParams()).isNull();
            }
        }
    }
}
Also used : StageEntity(pipelite.entity.StageEntity)

Aggregations

StageEntity (pipelite.entity.StageEntity)32 Stage (pipelite.stage.Stage)15 Test (org.junit.jupiter.api.Test)11 Process (pipelite.process.Process)8 StageState (pipelite.stage.StageState)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Timed (io.micrometer.core.annotation.Timed)3 ProcessEntity (pipelite.entity.ProcessEntity)3 ProcessBuilder (pipelite.process.builder.ProcessBuilder)3 ExecutorParameters (pipelite.stage.parameters.ExecutorParameters)3 StageLogEntity (pipelite.entity.StageLogEntity)2 JsonSerializableExecutor (pipelite.executor.JsonSerializableExecutor)2 SyncTestExecutor (pipelite.executor.SyncTestExecutor)2 StageExecutorResult (pipelite.stage.executor.StageExecutorResult)2 SimpleLsfExecutorParameters (pipelite.stage.parameters.SimpleLsfExecutorParameters)2 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Assertions (org.assertj.core.api.Assertions)1