Search in sources :

Example 1 with InternalException

use of io.kestra.core.exceptions.InternalException in project kestra by kestra-io.

the class MultipleCondition method test.

/**
 * This conditions will only validate previously calculated value on
 * {@link FlowService#multipleFlowTrigger(Stream, Flow, Execution, MultipleConditionStorageInterface)}} and save on {@link MultipleConditionStorageInterface}
 * by the executor.
 * The real validation is done here.
 */
@Override
public boolean test(ConditionContext conditionContext) throws InternalException {
    Logger logger = conditionContext.getRunContext().logger();
    MultipleConditionStorageInterface multipleConditionStorage = conditionContext.getMultipleConditionStorage();
    Objects.requireNonNull(multipleConditionStorage);
    Optional<MultipleConditionWindow> triggerExecutionWindow = multipleConditionStorage.get(conditionContext.getFlow(), this.getId());
    Map<String, Boolean> results = conditions.keySet().stream().map(condition -> new AbstractMap.SimpleEntry<>(condition, (triggerExecutionWindow.isPresent() && triggerExecutionWindow.get().getResults() != null && triggerExecutionWindow.get().getResults().containsKey(condition) && triggerExecutionWindow.get().getResults().get(condition)))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    long validatedCount = results.entrySet().stream().filter(Map.Entry::getValue).count();
    boolean result = conditions.size() == validatedCount;
    if (result && logger.isDebugEnabled()) {
        logger.debug("[namespace: {}] [flow: {}] Multiple conditions validated !", conditionContext.getFlow().getNamespace(), conditionContext.getFlow().getId());
    } else if (logger.isTraceEnabled()) {
        logger.trace("[namespace: {}] [flow: {}] Multiple conditions failed ({}/{}) with '{}'", conditionContext.getFlow().getNamespace(), conditionContext.getFlow().getId(), validatedCount, conditions.size(), results);
    }
    return result;
}
Also used : NotBlank(javax.validation.constraints.NotBlank) Getter(lombok.Getter) Plugin(io.kestra.core.models.annotations.Plugin) MultipleConditionStorageInterface(io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface) ConditionContext(io.kestra.core.models.conditions.ConditionContext) MultipleConditionWindow(io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow) NotEmpty(javax.validation.constraints.NotEmpty) FlowService(io.kestra.core.services.FlowService) Duration(java.time.Duration) Map(java.util.Map) ToString(lombok.ToString) Schema(io.swagger.v3.oas.annotations.media.Schema) Logger(org.slf4j.Logger) SuperBuilder(lombok.experimental.SuperBuilder) Condition(io.kestra.core.models.conditions.Condition) EqualsAndHashCode(lombok.EqualsAndHashCode) NotNull(javax.validation.constraints.NotNull) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) InternalException(io.kestra.core.exceptions.InternalException) PluginProperty(io.kestra.core.models.annotations.PluginProperty) Objects(java.util.Objects) AbstractMap(java.util.AbstractMap) Stream(java.util.stream.Stream) Pattern(javax.validation.constraints.Pattern) Example(io.kestra.core.models.annotations.Example) Optional(java.util.Optional) Flow(io.kestra.core.models.flows.Flow) NoArgsConstructor(lombok.NoArgsConstructor) MultipleConditionStorageInterface(io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface) MultipleConditionWindow(io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow) ToString(lombok.ToString) Logger(org.slf4j.Logger) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 2 with InternalException

use of io.kestra.core.exceptions.InternalException in project kestra by kestra-io.

the class ExecutorService method handleChildNext.

private Executor handleChildNext(Executor executor) throws InternalException {
    if (executor.getExecution().getTaskRunList() == null) {
        return executor;
    }
    List<TaskRun> running = executor.getExecution().getTaskRunList().stream().filter(taskRun -> taskRun.getState().isRunning()).collect(Collectors.toList());
    // Remove functionnal style to avoid (class io.kestra.core.exceptions.IllegalVariableEvaluationException cannot be cast to class java.lang.RuntimeException'
    ArrayList<TaskRun> result = new ArrayList<>();
    for (TaskRun taskRun : running) {
        result.addAll(this.childNextsTaskRun(executor, taskRun));
    }
    if (result.size() == 0) {
        return executor;
    }
    return executor.withTaskRun(result, "handleChildNext");
}
Also used : TaskRun(io.kestra.core.models.executions.TaskRun) Logger(org.slf4j.Logger) Rethrow.throwFunction(io.kestra.core.utils.Rethrow.throwFunction) ImmutableMap(com.google.common.collect.ImmutableMap) ConditionService(io.kestra.core.services.ConditionService) Singleton(jakarta.inject.Singleton) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) InternalException(io.kestra.core.exceptions.InternalException) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) State(io.kestra.core.models.flows.State) ArrayList(java.util.ArrayList) Task(io.kestra.core.models.tasks.Task) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) NextTaskRun(io.kestra.core.models.executions.NextTaskRun) ApplicationContext(io.micronaut.context.ApplicationContext) Optional(java.util.Optional) Flow(io.kestra.core.models.flows.Flow) MetricRegistry(io.kestra.core.metrics.MetricRegistry) Inject(jakarta.inject.Inject) FlowableTask(io.kestra.core.models.tasks.FlowableTask) TaskRun(io.kestra.core.models.executions.TaskRun) NextTaskRun(io.kestra.core.models.executions.NextTaskRun) ArrayList(java.util.ArrayList)

Example 3 with InternalException

use of io.kestra.core.exceptions.InternalException in project kestra by kestra-io.

the class FlowController method updateTask.

/**
 * @param namespace flow namespace
 * @param id        flow id to update
 * @param taskId    taskId id to update
 * @return flow updated
 */
@Patch(uri = "{namespace}/{id}/{taskId}", produces = MediaType.TEXT_JSON)
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<Flow> updateTask(String namespace, String id, String taskId, @Valid @Body Task task) throws ConstraintViolationException {
    Optional<Flow> existingFlow = flowRepository.findById(namespace, id);
    if (existingFlow.isEmpty()) {
        return HttpResponse.status(HttpStatus.NOT_FOUND);
    }
    if (!taskId.equals(task.getId())) {
        throw new IllegalArgumentException("Invalid taskId, previous '" + taskId + "' & current '" + task.getId() + "'");
    }
    Flow flow = existingFlow.get();
    try {
        Flow newValue = flow.updateTask(taskId, task);
        return HttpResponse.ok(flowRepository.update(newValue, flow));
    } catch (InternalException e) {
        return HttpResponse.status(HttpStatus.NOT_FOUND);
    }
}
Also used : Flow(io.kestra.core.models.flows.Flow) InternalException(io.kestra.core.exceptions.InternalException) ExecuteOn(io.micronaut.scheduling.annotation.ExecuteOn)

Example 4 with InternalException

use of io.kestra.core.exceptions.InternalException in project kestra by kestra-io.

the class FlowJoinerTransformer method transform.

@Override
public Executor transform(String key, Executor executor) {
    Flow flow;
    try {
        // pooling of new flow can be delayed on ExecutorStore, we maybe need to wait that the flow is updated
        flow = Await.until(() -> flowExecutorInterface.findByExecution(executor.getExecution()).orElse(null), Duration.ofMillis(100), Duration.ofMinutes(5));
    } catch (TimeoutException e) {
        return executor.withException(new Exception("Unable to find flow with namespace: '" + executor.getExecution().getNamespace() + "'" + ", id: '" + executor.getExecution().getFlowId() + "', " + "revision '" + executor.getExecution().getFlowRevision() + "'"), "joinFlow");
    }
    if (!withDefaults) {
        return executor.withFlow(flow);
    }
    try {
        flow = Template.injectTemplate(flow, executor.getExecution(), (namespace, id) -> this.templateExecutorInterface.findById(namespace, id).orElse(null));
    } catch (InternalException e) {
        log.warn("Failed to inject template", e);
    }
    Flow flowWithDefaults = taskDefaultService.injectDefaults(flow, executor.getExecution());
    return executor.withFlow(flowWithDefaults);
}
Also used : ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) FlowExecutorInterface(io.kestra.core.runners.FlowExecutorInterface) Slf4j(lombok.extern.slf4j.Slf4j) Await(io.kestra.core.utils.Await) TaskDefaultService(io.kestra.core.services.TaskDefaultService) Duration(java.time.Duration) Executor(io.kestra.core.runners.Executor) ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) TimeoutException(java.util.concurrent.TimeoutException) Flow(io.kestra.core.models.flows.Flow) Template(io.kestra.core.tasks.flows.Template) InternalException(io.kestra.core.exceptions.InternalException) TimeoutException(java.util.concurrent.TimeoutException) InternalException(io.kestra.core.exceptions.InternalException) Flow(io.kestra.core.models.flows.Flow) TimeoutException(java.util.concurrent.TimeoutException) InternalException(io.kestra.core.exceptions.InternalException)

Aggregations

InternalException (io.kestra.core.exceptions.InternalException)4 Flow (io.kestra.core.models.flows.Flow)4 Execution (io.kestra.core.models.executions.Execution)2 Duration (java.time.Duration)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Logger (org.slf4j.Logger)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 MetricRegistry (io.kestra.core.metrics.MetricRegistry)1 Example (io.kestra.core.models.annotations.Example)1 Plugin (io.kestra.core.models.annotations.Plugin)1 PluginProperty (io.kestra.core.models.annotations.PluginProperty)1 Condition (io.kestra.core.models.conditions.Condition)1 ConditionContext (io.kestra.core.models.conditions.ConditionContext)1 NextTaskRun (io.kestra.core.models.executions.NextTaskRun)1 TaskRun (io.kestra.core.models.executions.TaskRun)1 State (io.kestra.core.models.flows.State)1 FlowableTask (io.kestra.core.models.tasks.FlowableTask)1 ResolvedTask (io.kestra.core.models.tasks.ResolvedTask)1