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;
}
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;
}
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);
}
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);
}
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();
}
Aggregations