Search in sources :

Example 1 with FlowService

use of io.kestra.core.services.FlowService 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 FlowService

use of io.kestra.core.services.FlowService in project kestra by kestra-io.

the class ExecutorFlowTrigger method topology.

public StreamsBuilder topology() {
    StreamsBuilder builder = new KafkaStreamsBuilder();
    // trigger
    builder.addStateStore(Stores.keyValueStoreBuilder(Stores.persistentKeyValueStore(TRIGGER_MULTIPLE_STATE_STORE_NAME), Serdes.String(), JsonSerde.of(MultipleConditionWindow.class)));
    KStream<String, io.kestra.runner.kafka.streams.ExecutorFlowTrigger> stream = builder.stream(kafkaAdminService.getTopicName(io.kestra.runner.kafka.streams.ExecutorFlowTrigger.class), Consumed.with(Serdes.String(), JsonSerde.of(io.kestra.runner.kafka.streams.ExecutorFlowTrigger.class)).withName("KStream.ExecutorFlowTrigger")).filter((key, value) -> value != null, Named.as("ExecutorFlowTrigger.filterNotNull"));
    stream.transformValues(() -> new FlowTriggerWithExecutionTransformer(TRIGGER_MULTIPLE_STATE_STORE_NAME, kafkaFlowExecutor, flowService), Named.as("ExecutorFlowTrigger.transformToExecutionList"), TRIGGER_MULTIPLE_STATE_STORE_NAME).flatMap((key, value) -> value.stream().map(execution -> new KeyValue<>(execution.getId(), execution)).collect(Collectors.toList()), Named.as("ExecutorFlowTrigger.flapMapToExecution")).to(kafkaAdminService.getTopicName(Execution.class), Produced.with(Serdes.String(), JsonSerde.of(Execution.class)).withName("ExecutorFlowTrigger.toExecution"));
    stream.mapValues((readOnlyKey, value) -> (io.kestra.runner.kafka.streams.ExecutorFlowTrigger) null, Named.as("ExecutorFlowTrigger.executorFlowTriggerToNull")).to(kafkaAdminService.getTopicName(io.kestra.runner.kafka.streams.ExecutorFlowTrigger.class), Produced.with(Serdes.String(), JsonSerde.of(io.kestra.runner.kafka.streams.ExecutorFlowTrigger.class)).withName("ExecutorFlowTrigger.toExecutorFlowTrigger"));
    return builder;
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaAdminService(io.kestra.runner.kafka.services.KafkaAdminService) Produced(org.apache.kafka.streams.kstream.Produced) Consumed(org.apache.kafka.streams.kstream.Consumed) KeyValue(org.apache.kafka.streams.KeyValue) Stores(org.apache.kafka.streams.state.Stores) Singleton(jakarta.inject.Singleton) KStream(org.apache.kafka.streams.kstream.KStream) Execution(io.kestra.core.models.executions.Execution) KafkaFlowExecutor(io.kestra.runner.kafka.KafkaFlowExecutor) JsonSerde(io.kestra.runner.kafka.serializers.JsonSerde) Collectors(java.util.stream.Collectors) MultipleConditionWindow(io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow) FlowService(io.kestra.core.services.FlowService) FlowTriggerWithExecutionTransformer(io.kestra.runner.kafka.streams.FlowTriggerWithExecutionTransformer) Named(org.apache.kafka.streams.kstream.Named) Serdes(org.apache.kafka.common.serialization.Serdes) Inject(jakarta.inject.Inject) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder) KafkaQueueEnabled(io.kestra.runner.kafka.KafkaQueueEnabled) KeyValue(org.apache.kafka.streams.KeyValue) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder) Execution(io.kestra.core.models.executions.Execution) FlowTriggerWithExecutionTransformer(io.kestra.runner.kafka.streams.FlowTriggerWithExecutionTransformer) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder)

Aggregations

Execution (io.kestra.core.models.executions.Execution)2 MultipleConditionWindow (io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow)2 FlowService (io.kestra.core.services.FlowService)2 Collectors (java.util.stream.Collectors)2 InternalException (io.kestra.core.exceptions.InternalException)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 Flow (io.kestra.core.models.flows.Flow)1 MultipleConditionStorageInterface (io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface)1 KafkaFlowExecutor (io.kestra.runner.kafka.KafkaFlowExecutor)1 KafkaQueueEnabled (io.kestra.runner.kafka.KafkaQueueEnabled)1 JsonSerde (io.kestra.runner.kafka.serializers.JsonSerde)1 KafkaAdminService (io.kestra.runner.kafka.services.KafkaAdminService)1 KafkaStreamsBuilder (io.kestra.runner.kafka.services.KafkaStreamsBuilder)1 FlowTriggerWithExecutionTransformer (io.kestra.runner.kafka.streams.FlowTriggerWithExecutionTransformer)1 Schema (io.swagger.v3.oas.annotations.media.Schema)1 Inject (jakarta.inject.Inject)1