Search in sources :

Example 1 with State

use of io.kestra.core.models.flows.State in project kestra by kestra-io.

the class SchedulerConditionTest method schedule.

@Test
void schedule() throws Exception {
    // mock flow listeners
    MemoryFlowListeners flowListenersServiceSpy = spy(this.flowListenersService);
    SchedulerExecutionStateInterface executionRepositorySpy = spy(this.executionState);
    CountDownLatch queueCount = new CountDownLatch(4);
    Flow flow = createScheduleFlow();
    triggerState.save(Trigger.builder().namespace(flow.getNamespace()).flowId(flow.getId()).flowRevision(flow.getRevision()).triggerId("hourly").date(ZonedDateTime.parse("2021-09-06T02:00:00+01:00[Europe/Paris]")).build());
    doReturn(Collections.singletonList(flow)).when(flowListenersServiceSpy).flows();
    // mock the backfill execution is ended
    doAnswer(invocation -> Optional.of(Execution.builder().state(new State().withState(State.Type.SUCCESS)).build())).when(executionRepositorySpy).findById(any());
    // scheduler
    try (AbstractScheduler scheduler = new DefaultScheduler(applicationContext, flowListenersServiceSpy, executionRepositorySpy, triggerState)) {
        // wait for execution
        executionQueue.receive(SchedulerConditionTest.class, execution -> {
            if (execution.getState().getCurrent() == State.Type.CREATED) {
                executionQueue.emit(execution.withState(State.Type.SUCCESS));
                queueCount.countDown();
                if (queueCount.getCount() == 0) {
                    assertThat((ZonedDateTime) execution.getTrigger().getVariables().get("date"), is(ZonedDateTime.parse("2022-01-03T00:00:00+01:00")));
                }
            }
            assertThat(execution.getFlowId(), is(flow.getId()));
        });
        scheduler.run();
        queueCount.await(1, TimeUnit.MINUTES);
        assertThat(queueCount.getCount(), is(0L));
    }
}
Also used : State(io.kestra.core.models.flows.State) MemoryFlowListeners(io.kestra.runner.memory.MemoryFlowListeners) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test)

Example 2 with State

use of io.kestra.core.models.flows.State in project kestra by kestra-io.

the class SchedulerScheduleTest method schedule.

@Test
void schedule() throws Exception {
    // mock flow listeners
    MemoryFlowListeners flowListenersServiceSpy = spy(this.flowListenersService);
    SchedulerExecutionStateInterface executionRepositorySpy = spy(this.executionState);
    CountDownLatch queueCount = new CountDownLatch(5);
    Flow flow = createScheduleFlow();
    doReturn(Collections.singletonList(flow)).when(flowListenersServiceSpy).flows();
    // mock the backfill execution is ended
    doAnswer(invocation -> Optional.of(Execution.builder().state(new State().withState(State.Type.SUCCESS)).build())).when(executionRepositorySpy).findById(any());
    // scheduler
    try (AbstractScheduler scheduler = new DefaultScheduler(applicationContext, flowListenersServiceSpy, executionRepositorySpy, triggerState)) {
        // wait for execution
        executionQueue.receive(SchedulerScheduleTest.class, execution -> {
            assertThat(execution.getInputs().get("testInputs"), is("test-inputs"));
            queueCount.countDown();
            if (execution.getState().getCurrent() == State.Type.CREATED) {
                executionQueue.emit(execution.withState(State.Type.SUCCESS));
            }
            assertThat(execution.getFlowId(), is(flow.getId()));
        });
        scheduler.run();
        queueCount.await(1, TimeUnit.MINUTES);
        assertThat(queueCount.getCount(), is(0L));
    }
}
Also used : State(io.kestra.core.models.flows.State) MemoryFlowListeners(io.kestra.runner.memory.MemoryFlowListeners) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test)

Example 3 with State

use of io.kestra.core.models.flows.State in project kestra by kestra-io.

the class SchedulerThreadTest method thread.

@Test
void thread() throws Exception {
    // mock flow listeners
    MemoryFlowListeners flowListenersServiceSpy = spy(this.flowListenersService);
    SchedulerExecutionStateInterface schedulerExecutionStateSpy = spy(this.executionState);
    CountDownLatch queueCount = new CountDownLatch(2);
    Flow flow = createThreadFlow();
    doReturn(Collections.singletonList(flow)).when(flowListenersServiceSpy).flows();
    // mock the backfill execution is ended
    doAnswer(invocation -> Optional.of(Execution.builder().state(new State().withState(State.Type.SUCCESS)).build())).when(schedulerExecutionStateSpy).findById(any());
    // scheduler
    try (AbstractScheduler scheduler = new DefaultScheduler(applicationContext, flowListenersServiceSpy, schedulerExecutionStateSpy, triggerState)) {
        AtomicReference<Execution> last = new AtomicReference<>();
        // wait for execution
        executionQueue.receive(SchedulerThreadTest.class, execution -> {
            last.set(execution);
            assertThat(execution.getFlowId(), is(flow.getId()));
            if (execution.getState().getCurrent() != State.Type.SUCCESS) {
                executionQueue.emit(execution.withState(State.Type.SUCCESS));
                queueCount.countDown();
            }
        });
        scheduler.run();
        queueCount.await(1, TimeUnit.MINUTES);
        assertThat(last.get().getVariables().get("defaultInjected"), is("done"));
        assertThat(last.get().getVariables().get("counter"), is(3));
        AbstractSchedulerTest.COUNTER = 0;
    }
}
Also used : Execution(io.kestra.core.models.executions.Execution) State(io.kestra.core.models.flows.State) MemoryFlowListeners(io.kestra.runner.memory.MemoryFlowListeners) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Flow(io.kestra.core.models.flows.Flow) Test(org.junit.jupiter.api.Test)

Example 4 with State

use of io.kestra.core.models.flows.State in project kestra by kestra-io.

the class ExecutionControllerTest method restartFromTaskId.

@Test
void restartFromTaskId() throws Exception {
    final String flowId = "restart_with_inputs";
    final String referenceTaskId = "instant";
    // Run execution until it ends
    Execution parentExecution = runnerUtils.runOne(TESTS_FLOW_NS, flowId, null, (flow, execution1) -> runnerUtils.typedInputs(flow, execution1, inputs));
    Optional<Flow> flow = flowRepositoryInterface.findById(TESTS_FLOW_NS, flowId);
    assertThat(flow.isPresent(), is(true));
    // Run child execution starting from a specific task and wait until it finishes
    Execution finishedChildExecution = runnerUtils.awaitChildExecution(flow.get(), parentExecution, throwRunnable(() -> {
        Thread.sleep(100);
        Execution createdChidExec = client.toBlocking().retrieve(HttpRequest.POST("/api/v1/executions/" + parentExecution.getId() + "/replay?taskRunId=" + parentExecution.findTaskRunByTaskIdAndValue(referenceTaskId, List.of()).getId(), ImmutableMap.of()), Execution.class);
        assertThat(createdChidExec, notNullValue());
        assertThat(createdChidExec.getParentId(), is(parentExecution.getId()));
        assertThat(createdChidExec.getTaskRunList().size(), is(4));
        assertThat(createdChidExec.getState().getCurrent(), is(State.Type.RESTARTED));
        IntStream.range(0, 3).mapToObj(value -> createdChidExec.getTaskRunList().get(value)).forEach(taskRun -> assertThat(taskRun.getState().getCurrent(), is(State.Type.SUCCESS)));
        assertThat(createdChidExec.getTaskRunList().get(3).getState().getCurrent(), is(State.Type.RESTARTED));
        assertThat(createdChidExec.getTaskRunList().get(3).getAttempts().size(), is(1));
    }), Duration.ofSeconds(15));
    assertThat(finishedChildExecution, notNullValue());
    assertThat(finishedChildExecution.getParentId(), is(parentExecution.getId()));
    assertThat(finishedChildExecution.getTaskRunList().size(), is(5));
    finishedChildExecution.getTaskRunList().stream().map(TaskRun::getState).forEach(state -> assertThat(state.getCurrent(), is(State.Type.SUCCESS)));
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TaskRun(io.kestra.core.models.executions.TaskRun) java.util(java.util) InputsTest(io.kestra.core.runners.InputsTest) QueueInterface(io.kestra.core.queues.QueueInterface) TimeoutException(java.util.concurrent.TimeoutException) Client(io.micronaut.http.client.annotation.Client) RxHttpClient(io.micronaut.rxjava2.http.client.RxHttpClient) State(io.kestra.core.models.flows.State) IdUtils(io.kestra.core.utils.IdUtils) Event(io.micronaut.http.sse.Event) RxSseClient(io.micronaut.rxjava2.http.client.sse.RxSseClient) HttpStatus(io.micronaut.http.HttpStatus) MediaType(io.micronaut.http.MediaType) Duration(java.time.Duration) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) Argument(io.micronaut.core.type.Argument) HttpRequest(io.micronaut.http.HttpRequest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Webhook(io.kestra.core.models.triggers.types.Webhook) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) PagedResults(io.kestra.webserver.responses.PagedResults) ImmutableMap(com.google.common.collect.ImmutableMap) QueueFactoryInterface(io.kestra.core.queues.QueueFactoryInterface) Rethrow.throwRunnable(io.kestra.core.utils.Rethrow.throwRunnable) Matchers(org.hamcrest.Matchers) Execution(io.kestra.core.models.executions.Execution) File(java.io.File) Test(org.junit.jupiter.api.Test) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) FileMetas(io.kestra.core.models.storage.FileMetas) Flow(io.kestra.core.models.flows.Flow) Inject(jakarta.inject.Inject) AbstractMemoryRunnerTest(io.kestra.core.runners.AbstractMemoryRunnerTest) FlowRepositoryInterface(io.kestra.core.repositories.FlowRepositoryInterface) Named(jakarta.inject.Named) Execution(io.kestra.core.models.executions.Execution) Flow(io.kestra.core.models.flows.Flow) InputsTest(io.kestra.core.runners.InputsTest) Test(org.junit.jupiter.api.Test) AbstractMemoryRunnerTest(io.kestra.core.runners.AbstractMemoryRunnerTest)

Example 5 with State

use of io.kestra.core.models.flows.State in project kestra by kestra-io.

the class ExecutionTest method hasTaskRunJoinableFailedExecutionFromExecutor.

@Test
void hasTaskRunJoinableFailedExecutionFromExecutor() {
    Execution execution = Execution.builder().taskRunList(Collections.singletonList(TASK_RUN.state(new State(State.Type.FAILED, new State().withState(State.Type.RUNNING))).build())).build();
    assertThat(execution.hasTaskRunJoinable(TASK_RUN.state(new State(State.Type.RUNNING, new State())).build()), is(false));
}
Also used : State(io.kestra.core.models.flows.State) Test(org.junit.jupiter.api.Test)

Aggregations

State (io.kestra.core.models.flows.State)29 Test (org.junit.jupiter.api.Test)16 Execution (io.kestra.core.models.executions.Execution)13 Flow (io.kestra.core.models.flows.Flow)9 TaskRun (io.kestra.core.models.executions.TaskRun)8 Inject (jakarta.inject.Inject)5 java.util (java.util)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 QueueFactoryInterface (io.kestra.core.queues.QueueFactoryInterface)4 QueueInterface (io.kestra.core.queues.QueueInterface)4 FlowRepositoryInterface (io.kestra.core.repositories.FlowRepositoryInterface)4 Duration (java.time.Duration)4 TaskRunAttempt (io.kestra.core.models.executions.TaskRunAttempt)3 MemoryFlowListeners (io.kestra.runner.memory.MemoryFlowListeners)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 InternalException (io.kestra.core.exceptions.InternalException)2 TimeoutExceededException (io.kestra.core.exceptions.TimeoutExceededException)2 MetricRegistry (io.kestra.core.metrics.MetricRegistry)2 FileMetas (io.kestra.core.models.storage.FileMetas)2