Search in sources :

Example 11 with TaskRun

use of io.kestra.core.models.executions.TaskRun in project kestra by kestra-io.

the class ExecutorService method onNexts.

public Execution onNexts(Flow flow, Execution execution, List<TaskRun> nexts) {
    if (log.isTraceEnabled()) {
        log.trace("[namespace: {}] [flow: {}] [execution: {}] Found {} next(s) {}", execution.getNamespace(), execution.getFlowId(), execution.getId(), nexts.size(), nexts);
    }
    List<TaskRun> executionTasksRun;
    Execution newExecution;
    if (execution.getTaskRunList() == null) {
        executionTasksRun = nexts;
    } else {
        executionTasksRun = new ArrayList<>(execution.getTaskRunList());
        executionTasksRun.addAll(nexts);
    }
    // update Execution
    newExecution = execution.withTaskRunList(executionTasksRun);
    if (execution.getState().getCurrent() == State.Type.CREATED) {
        metricRegistry.counter(MetricRegistry.KESTRA_EXECUTOR_EXECUTION_STARTED_COUNT, metricRegistry.tags(execution)).increment();
        flow.logger().info("[namespace: {}] [flow: {}] [execution: {}] Flow started", execution.getNamespace(), execution.getFlowId(), execution.getId());
        newExecution = newExecution.withState(State.Type.RUNNING);
    }
    metricRegistry.counter(MetricRegistry.KESTRA_EXECUTOR_TASKRUN_NEXT_COUNT, metricRegistry.tags(execution)).increment(nexts.size());
    return newExecution;
}
Also used : TaskRun(io.kestra.core.models.executions.TaskRun) NextTaskRun(io.kestra.core.models.executions.NextTaskRun) Execution(io.kestra.core.models.executions.Execution)

Example 12 with TaskRun

use of io.kestra.core.models.executions.TaskRun in project kestra by kestra-io.

the class ExecutionService method restart.

public Execution restart(final Execution execution, @Nullable Integer revision) throws Exception {
    if (!execution.getState().isTerninated()) {
        throw new IllegalStateException("Execution must be terminated to be restarted, " + "current state is '" + execution.getState().getCurrent() + "' !");
    }
    final Flow flow = flowRepositoryInterface.findByExecution(execution);
    Set<String> taskRunToRestart = this.taskRunWithAncestors(execution, execution.getTaskRunList().stream().filter(taskRun -> taskRun.getState().getCurrent().isFailed()).collect(Collectors.toList()));
    if (taskRunToRestart.size() == 0) {
        throw new IllegalArgumentException("No failed task found to restart execution from !");
    }
    Map<String, String> mappingTaskRunId = this.mapTaskRunId(execution, revision == null);
    final String newExecutionId = revision != null ? IdUtils.create() : null;
    List<TaskRun> newTaskRuns = execution.getTaskRunList().stream().map(throwFunction(originalTaskRun -> this.mapTaskRun(flow, originalTaskRun, mappingTaskRunId, newExecutionId, State.Type.RESTARTED, taskRunToRestart.contains(originalTaskRun.getId())))).collect(Collectors.toList());
    // Build and launch new execution
    Execution newExecution = execution.childExecution(newExecutionId, newTaskRuns, execution.withState(State.Type.RESTARTED).getState());
    return revision != null ? newExecution.withFlowRevision(revision) : newExecution;
}
Also used : TaskRun(io.kestra.core.models.executions.TaskRun) Execution(io.kestra.core.models.executions.Execution) Flow(io.kestra.core.models.flows.Flow)

Example 13 with TaskRun

use of io.kestra.core.models.executions.TaskRun in project kestra by kestra-io.

the class ExecutionService method markAs.

public Execution markAs(final Execution execution, String taskRunId, State.Type newState) throws Exception {
    if (!execution.getState().isTerninated()) {
        throw new IllegalStateException("Execution must be terminated to be restarted, " + "current state is '" + execution.getState().getCurrent() + "' !");
    }
    final Flow flow = flowRepositoryInterface.findByExecution(execution);
    Set<String> taskRunToRestart = this.taskRunWithAncestors(execution, execution.getTaskRunList().stream().filter(taskRun -> taskRun.getId().equals(taskRunId)).collect(Collectors.toList()));
    if (taskRunToRestart.size() == 0) {
        throw new IllegalArgumentException("No task found to restart execution from !");
    }
    Execution newExecution = execution;
    for (String s : taskRunToRestart) {
        TaskRun originalTaskRun = newExecution.findTaskRunByTaskRunId(s);
        boolean isFlowable = flow.findTaskByTaskId(originalTaskRun.getTaskId()).isFlowable();
        if (!isFlowable || s.equals(taskRunId)) {
            TaskRun newTaskRun = originalTaskRun.withState(newState);
            if (originalTaskRun.getAttempts() != null && originalTaskRun.getAttempts().size() > 0) {
                ArrayList<TaskRunAttempt> attempts = new ArrayList<>(originalTaskRun.getAttempts());
                attempts.set(attempts.size() - 1, attempts.get(attempts.size() - 1).withState(newState));
                newTaskRun = newTaskRun.withAttempts(attempts);
            }
            newExecution = newExecution.withTaskRun(newTaskRun);
        } else {
            newExecution = newExecution.withTaskRun(originalTaskRun.withState(State.Type.RUNNING));
        }
    }
    return newExecution.withState(State.Type.RESTARTED);
}
Also used : TaskRunAttempt(io.kestra.core.models.executions.TaskRunAttempt) TaskRun(io.kestra.core.models.executions.TaskRun) Execution(io.kestra.core.models.executions.Execution) Flow(io.kestra.core.models.flows.Flow)

Example 14 with TaskRun

use of io.kestra.core.models.executions.TaskRun 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 15 with TaskRun

use of io.kestra.core.models.executions.TaskRun in project kestra by kestra-io.

the class Flow method createWorkerTaskResult.

public WorkerTaskResult createWorkerTaskResult(@Nullable RunContextFactory runContextFactory, WorkerTaskExecution workerTaskExecution, @Nullable io.kestra.core.models.flows.Flow flow, Execution execution) {
    TaskRun taskRun = workerTaskExecution.getTaskRun();
    Output.OutputBuilder builder = Output.builder().executionId(execution.getId());
    if (workerTaskExecution.getTask().getOutputs() != null && runContextFactory != null) {
        RunContext runContext = runContextFactory.of(flow, workerTaskExecution.getTask(), execution, workerTaskExecution.getTaskRun());
        try {
            builder.outputs(runContext.render(workerTaskExecution.getTask().getOutputs()));
        } catch (Exception e) {
            runContext.logger().warn("Failed to extract ouputs with error: '" + e.getMessage() + "'", e);
            taskRun = taskRun.withState(State.Type.FAILED).withOutputs(builder.build().toMap());
            return WorkerTaskResult.builder().taskRun(taskRun).build();
        }
    }
    builder.state(execution.getState().getCurrent());
    taskRun = taskRun.withOutputs(builder.build().toMap());
    if (transmitFailed && (execution.getState().isFailed() || execution.getState().getCurrent() == State.Type.KILLED || execution.getState().getCurrent() == State.Type.WARNING)) {
        taskRun = taskRun.withState(execution.getState().getCurrent());
    } else {
        taskRun = taskRun.withState(State.Type.SUCCESS);
    }
    return WorkerTaskResult.builder().taskRun(taskRun).build();
}
Also used : TaskRun(io.kestra.core.models.executions.TaskRun)

Aggregations

TaskRun (io.kestra.core.models.executions.TaskRun)23 Execution (io.kestra.core.models.executions.Execution)14 Flow (io.kestra.core.models.flows.Flow)9 State (io.kestra.core.models.flows.State)8 Test (org.junit.jupiter.api.Test)6 NextTaskRun (io.kestra.core.models.executions.NextTaskRun)5 Task (io.kestra.core.models.tasks.Task)5 Inject (jakarta.inject.Inject)5 java.util (java.util)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 TaskRunAttempt (io.kestra.core.models.executions.TaskRunAttempt)4 FlowableTask (io.kestra.core.models.tasks.FlowableTask)4 AbstractMemoryRunnerTest (io.kestra.core.runners.AbstractMemoryRunnerTest)4 Duration (java.time.Duration)4 InternalException (io.kestra.core.exceptions.InternalException)3 QueueFactoryInterface (io.kestra.core.queues.QueueFactoryInterface)3 QueueInterface (io.kestra.core.queues.QueueInterface)3 FlowRepositoryInterface (io.kestra.core.repositories.FlowRepositoryInterface)3 TimeoutExceededException (io.kestra.core.exceptions.TimeoutExceededException)2 MetricRegistry (io.kestra.core.metrics.MetricRegistry)2