Search in sources :

Example 16 with State

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

the class ExecutionService method mapTaskRun.

private TaskRun mapTaskRun(Flow flow, TaskRun originalTaskRun, Map<String, String> mappingTaskRunId, String newExecutionId, State.Type newStateType, Boolean toRestart) throws InternalException {
    boolean isFlowable = flow.findTaskByTaskId(originalTaskRun.getTaskId()).isFlowable();
    State alterState;
    if (!isFlowable) {
        // The current task run is the reference task run, its default state will be newState
        alterState = originalTaskRun.withState(newStateType).getState();
    } else {
        // The current task run is an ascendant of the reference task run
        alterState = originalTaskRun.withState(State.Type.RUNNING).getState();
    }
    return originalTaskRun.forChildExecution(mappingTaskRunId, newExecutionId, toRestart ? alterState : null);
}
Also used : State(io.kestra.core.models.flows.State)

Example 17 with State

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

the class Worker method runAttempt.

private WorkerTask runAttempt(WorkerTask workerTask) {
    RunnableTask<?> task = (RunnableTask<?>) workerTask.getTask();
    RunContext runContext = workerTask.getRunContext().forWorker(this.applicationContext, workerTask.getTaskRun());
    Logger logger = runContext.logger();
    TaskRunAttempt.TaskRunAttemptBuilder builder = TaskRunAttempt.builder().state(new State().withState(State.Type.RUNNING));
    AtomicInteger metricRunningCount = getMetricRunningCount(workerTask);
    metricRunningCount.incrementAndGet();
    WorkerThread workerThread = new WorkerThread(logger, workerTask, task, runContext, metricRegistry);
    workerThread.start();
    // emit attempts
    this.workerTaskResultQueue.emit(new WorkerTaskResult(workerTask.withTaskRun(workerTask.getTaskRun().withAttempts(this.addAttempt(workerTask, builder.build())))));
    // run it
    State.Type state;
    try {
        synchronized (this) {
            workerThreadReferences.add(workerThread);
        }
        workerThread.join();
        state = workerThread.getTaskState();
    } catch (InterruptedException e) {
        logger.error("Failed to join WorkerThread {}", e.getMessage(), e);
        state = State.Type.FAILED;
    } finally {
        synchronized (this) {
            workerThreadReferences.remove(workerThread);
        }
    }
    metricRunningCount.decrementAndGet();
    // attempt
    TaskRunAttempt taskRunAttempt = builder.metrics(runContext.metrics()).build().withState(state);
    // logs
    if (workerThread.getTaskOutput() != null) {
        log.debug("Outputs\n{}", JacksonMapper.log(workerThread.getTaskOutput()));
    }
    if (runContext.metrics().size() > 0) {
        log.trace("Metrics\n{}", JacksonMapper.log(runContext.metrics()));
    }
    // save outputs
    List<TaskRunAttempt> attempts = this.addAttempt(workerTask, taskRunAttempt);
    TaskRun taskRun = workerTask.getTaskRun().withAttempts(attempts);
    try {
        taskRun = taskRun.withOutputs(workerThread.getTaskOutput() != null ? workerThread.getTaskOutput().toMap() : ImmutableMap.of());
    } catch (Exception e) {
        logger.warn("Unable to save output on taskRun '{}'", taskRun, e);
    }
    return workerTask.withTaskRun(taskRun);
}
Also used : Logger(org.slf4j.Logger) IOException(java.io.IOException) QueueException(io.kestra.core.queues.QueueException) TimeoutExceededException(io.kestra.core.exceptions.TimeoutExceededException) TaskRunAttempt(io.kestra.core.models.executions.TaskRunAttempt) TaskRun(io.kestra.core.models.executions.TaskRun) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) State(io.kestra.core.models.flows.State) RunnableTask(io.kestra.core.models.tasks.RunnableTask)

Example 18 with State

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

the class ExecutionTest method hasTaskRunJoinableRestartSuccess.

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

Example 19 with State

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

the class ExecutionTest method hasTaskRunJoinableTrue.

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

Example 20 with State

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

the class ExecutionTest method hasTaskRunJoinableRestartFailed.

@Test
void hasTaskRunJoinableRestartFailed() {
    Execution execution = Execution.builder().taskRunList(Collections.singletonList(TASK_RUN.state(new State(State.Type.CREATED, new State().withState(State.Type.RUNNING).withState(State.Type.FAILED))).build())).build();
    assertThat(execution.hasTaskRunJoinable(TASK_RUN.state(new State(State.Type.FAILED, new State().withState(State.Type.RUNNING))).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