Search in sources :

Example 1 with ResolvedTask

use of io.kestra.core.models.tasks.ResolvedTask in project kestra by kestra-io.

the class ExecutorService method handleKilling.

private Executor handleKilling(Executor executor) {
    if (executor.getExecution().getState().getCurrent() != State.Type.KILLING) {
        return executor;
    }
    List<ResolvedTask> currentTasks = executor.getExecution().findTaskDependingFlowState(ResolvedTask.of(executor.getFlow().getTasks()), ResolvedTask.of(executor.getFlow().getErrors()));
    if (executor.getExecution().hasRunning(currentTasks) || executor.getExecution().hasCreated()) {
        return executor;
    }
    Execution newExecution = executor.getExecution().withState(State.Type.KILLED);
    return executor.withExecution(newExecution, "handleKilling");
}
Also used : Execution(io.kestra.core.models.executions.Execution) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask)

Example 2 with ResolvedTask

use of io.kestra.core.models.tasks.ResolvedTask in project kestra by kestra-io.

the class ExecutorService method childWorkerTaskResult.

private Optional<WorkerTaskResult> childWorkerTaskResult(Flow flow, Execution execution, TaskRun parentTaskRun) throws InternalException {
    Task parent = flow.findTaskByTaskId(parentTaskRun.getTaskId());
    if (parent instanceof FlowableTask) {
        FlowableTask<?> flowableParent = (FlowableTask<?>) parent;
        RunContext runContext = runContextFactory.of(flow, parent, execution, parentTaskRun);
        // first find the normal ended child tasks and send result
        Optional<WorkerTaskResult> endedTask = childWorkerTaskTypeToWorkerTask(flowableParent.resolveState(runContext, execution, parentTaskRun), parent, parentTaskRun);
        if (endedTask.isPresent()) {
            return endedTask;
        }
        // after if the execution is KILLING, we find if all already started tasks if finished
        if (execution.getState().getCurrent() == State.Type.KILLING) {
            // first notified the parent taskRun of killing to avoid new creation of tasks
            if (parentTaskRun.getState().getCurrent() != State.Type.KILLING) {
                return childWorkerTaskTypeToWorkerTask(Optional.of(State.Type.KILLING), parent, parentTaskRun);
            }
            // Then wait for completion (KILLED or whatever) on child taks to KILLED the parennt one.
            List<ResolvedTask> currentTasks = execution.findTaskDependingFlowState(flowableParent.childTasks(runContext, parentTaskRun), FlowableUtils.resolveTasks(flowableParent.getErrors(), parentTaskRun));
            List<TaskRun> taskRunByTasks = execution.findTaskRunByTasks(currentTasks, parentTaskRun);
            if (taskRunByTasks.stream().filter(t -> t.getState().isTerninated()).count() == taskRunByTasks.size()) {
                return childWorkerTaskTypeToWorkerTask(Optional.of(State.Type.KILLED), parent, parentTaskRun);
            }
        }
    }
    return Optional.empty();
}
Also used : FlowableTask(io.kestra.core.models.tasks.FlowableTask) TaskRun(io.kestra.core.models.executions.TaskRun) NextTaskRun(io.kestra.core.models.executions.NextTaskRun) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) Task(io.kestra.core.models.tasks.Task) FlowableTask(io.kestra.core.models.tasks.FlowableTask) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask)

Example 3 with ResolvedTask

use of io.kestra.core.models.tasks.ResolvedTask in project kestra by kestra-io.

the class FlowableUtils method resolveEachTasks.

public static List<ResolvedTask> resolveEachTasks(RunContext runContext, TaskRun parentTaskRun, List<Task> tasks, String value) throws IllegalVariableEvaluationException {
    String renderValue = runContext.render(value);
    List<Object> values;
    try {
        values = MAPPER.readValue(renderValue, TYPE_REFERENCE);
    } catch (JsonProcessingException e) {
        throw new IllegalVariableEvaluationException(e);
    }
    List<Object> distinctValue = values.stream().distinct().collect(Collectors.toList());
    long nullCount = distinctValue.stream().filter(Objects::isNull).count();
    if (nullCount > 0) {
        throw new IllegalVariableEvaluationException("Found '" + nullCount + "' null values on Each, " + "with values=" + Arrays.toString(values.toArray()));
    }
    ArrayList<ResolvedTask> result = new ArrayList<>();
    for (Object current : distinctValue) {
        for (Task task : tasks) {
            try {
                String resolvedValue = current instanceof String ? (String) current : MAPPER.writeValueAsString(current);
                result.add(ResolvedTask.builder().task(task).value(resolvedValue).parentId(parentTaskRun.getId()).build());
            } catch (JsonProcessingException e) {
                throw new IllegalVariableEvaluationException(e);
            }
        }
    }
    return result;
}
Also used : IllegalVariableEvaluationException(io.kestra.core.exceptions.IllegalVariableEvaluationException) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) Task(io.kestra.core.models.tasks.Task) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with ResolvedTask

use of io.kestra.core.models.tasks.ResolvedTask in project kestra by kestra-io.

the class WorkerTest method workerTask.

private WorkerTask workerTask(String sleep) {
    Bash bash = Bash.builder().type(Bash.class.getName()).id("unit-test").commands(new String[] { "for i in $(seq 1 " + sleep + "); do echo $i; sleep 1; done" }).build();
    Flow flow = Flow.builder().id(IdUtils.create()).namespace("io.kestra.unit-test").tasks(Collections.singletonList(bash)).build();
    Execution execution = TestsUtils.mockExecution(flow, ImmutableMap.of());
    ResolvedTask resolvedTask = ResolvedTask.of(bash);
    return WorkerTask.builder().runContext(runContextFactory.of(ImmutableMap.of("key", "value"))).task(bash).taskRun(TaskRun.of(execution, resolvedTask)).build();
}
Also used : Execution(io.kestra.core.models.executions.Execution) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) Bash(io.kestra.core.tasks.scripts.Bash) Flow(io.kestra.core.models.flows.Flow)

Aggregations

ResolvedTask (io.kestra.core.models.tasks.ResolvedTask)4 Execution (io.kestra.core.models.executions.Execution)2 Task (io.kestra.core.models.tasks.Task)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IllegalVariableEvaluationException (io.kestra.core.exceptions.IllegalVariableEvaluationException)1 NextTaskRun (io.kestra.core.models.executions.NextTaskRun)1 TaskRun (io.kestra.core.models.executions.TaskRun)1 Flow (io.kestra.core.models.flows.Flow)1 FlowableTask (io.kestra.core.models.tasks.FlowableTask)1 Bash (io.kestra.core.tasks.scripts.Bash)1