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