use of io.kestra.core.models.conditions.Condition in project kestra by kestra-io.
the class ConditionServiceTest method valid.
@Test
void valid() {
Flow flow = TestsUtils.mockFlow();
Execution execution = TestsUtils.mockExecution(flow, ImmutableMap.of());
RunContext runContext = runContextFactory.of(flow, execution);
ConditionContext conditionContext = conditionService.conditionContext(runContext, flow, execution);
List<Condition> conditions = Arrays.asList(ExecutionFlowCondition.builder().namespace(flow.getNamespace()).flowId(flow.getId()).build(), ExecutionNamespaceCondition.builder().namespace(flow.getNamespace()).build());
boolean valid = conditionService.valid(flow, conditions, conditionContext);
assertThat(valid, is(true));
}
use of io.kestra.core.models.conditions.Condition in project kestra by kestra-io.
the class ConditionServiceTest method exception.
@Test
void exception() throws InterruptedException {
List<LogEntry> logs = new ArrayList<>();
logQueue.receive(logs::add);
Flow flow = TestsUtils.mockFlow();
Schedule schedule = Schedule.builder().id("unit").type(Schedule.class.getName()).cron("0 0 1 * *").build();
RunContext runContext = runContextFactory.of(flow, schedule);
ConditionContext conditionContext = conditionService.conditionContext(runContext, flow, null);
List<Condition> conditions = Collections.singletonList(ExecutionFlowCondition.builder().namespace(flow.getNamespace()).flowId(flow.getId()).build());
conditionService.valid(flow, conditions, conditionContext);
Thread.sleep(250);
assertThat(logs.stream().filter(logEntry -> logEntry.getNamespace().equals("io.kestra.core.services.ConditionServiceTest")).count(), greaterThan(0L));
assertThat(logs.stream().filter(logEntry -> logEntry.getFlowId().equals("exception")).count(), greaterThan(0L));
}
use of io.kestra.core.models.conditions.Condition 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.models.conditions.Condition in project kestra by kestra-io.
the class PluginScanner method scanClassLoader.
@SuppressWarnings({ "unchecked", "rawtypes" })
private RegisteredPlugin scanClassLoader(final ClassLoader classLoader, ExternalPlugin externalPlugin, Manifest manifest) {
List<Class<? extends Task>> tasks = new ArrayList<>();
List<Class<? extends AbstractTrigger>> triggers = new ArrayList<>();
List<Class<? extends Condition>> conditions = new ArrayList<>();
List<Class<? extends StorageInterface>> storages = new ArrayList<>();
List<Class<?>> controllers = new ArrayList<>();
final SoftServiceLoader<BeanIntrospectionReference> definitions = SoftServiceLoader.load(BeanIntrospectionReference.class, classLoader);
if (manifest == null) {
manifest = getManifest(classLoader);
}
for (ServiceDefinition<BeanIntrospectionReference> definition : definitions) {
if (definition.isPresent()) {
final BeanIntrospectionReference ref = definition.load();
Class beanType = ref.getBeanType();
if (Modifier.isAbstract(beanType.getModifiers())) {
continue;
}
if (Task.class.isAssignableFrom(beanType)) {
tasks.add(beanType);
}
if (AbstractTrigger.class.isAssignableFrom(beanType)) {
triggers.add(beanType);
}
if (Condition.class.isAssignableFrom(beanType)) {
conditions.add(beanType);
}
if (StorageInterface.class.isAssignableFrom(beanType)) {
storages.add(beanType);
}
if (beanType.isAnnotationPresent(Controller.class)) {
controllers.add(beanType);
}
}
}
return RegisteredPlugin.builder().externalPlugin(externalPlugin).manifest(manifest).classLoader(classLoader).tasks(tasks).triggers(triggers).conditions(conditions).controllers(controllers).storages(storages).build();
}
use of io.kestra.core.models.conditions.Condition in project kestra by kestra-io.
the class ConditionService method isValid.
public boolean isValid(AbstractTrigger trigger, Flow flow, Execution execution, MultipleConditionStorageInterface multipleConditionStorage) {
assert execution != null;
List<Condition> conditions = trigger.getConditions() == null ? new ArrayList<>() : trigger.getConditions();
ConditionContext conditionContext = this.conditionContext(runContextFactory.of(flow, execution), flow, execution, multipleConditionStorage);
return this.valid(flow, conditions, conditionContext);
}
Aggregations