Search in sources :

Example 1 with Condition

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));
}
Also used : ExecutionFlowCondition(io.kestra.core.models.conditions.types.ExecutionFlowCondition) ExecutionNamespaceCondition(io.kestra.core.models.conditions.types.ExecutionNamespaceCondition) Condition(io.kestra.core.models.conditions.Condition) ConditionContext(io.kestra.core.models.conditions.ConditionContext) Execution(io.kestra.core.models.executions.Execution) RunContext(io.kestra.core.runners.RunContext) Flow(io.kestra.core.models.flows.Flow) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 2 with Condition

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));
}
Also used : ExecutionFlowCondition(io.kestra.core.models.conditions.types.ExecutionFlowCondition) ExecutionNamespaceCondition(io.kestra.core.models.conditions.types.ExecutionNamespaceCondition) Condition(io.kestra.core.models.conditions.Condition) ConditionContext(io.kestra.core.models.conditions.ConditionContext) Schedule(io.kestra.core.models.triggers.types.Schedule) ArrayList(java.util.ArrayList) RunContext(io.kestra.core.runners.RunContext) LogEntry(io.kestra.core.models.executions.LogEntry) Flow(io.kestra.core.models.flows.Flow) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 3 with Condition

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;
}
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 4 with Condition

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();
}
Also used : Condition(io.kestra.core.models.conditions.Condition) Task(io.kestra.core.models.tasks.Task) BeanIntrospectionReference(io.micronaut.core.beans.BeanIntrospectionReference) ArrayList(java.util.ArrayList) StorageInterface(io.kestra.core.storages.StorageInterface) AbstractTrigger(io.kestra.core.models.triggers.AbstractTrigger)

Example 5 with Condition

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);
}
Also used : Condition(io.kestra.core.models.conditions.Condition) ScheduleCondition(io.kestra.core.models.conditions.ScheduleCondition) ConditionContext(io.kestra.core.models.conditions.ConditionContext)

Aggregations

Condition (io.kestra.core.models.conditions.Condition)5 ConditionContext (io.kestra.core.models.conditions.ConditionContext)4 Flow (io.kestra.core.models.flows.Flow)3 ExecutionFlowCondition (io.kestra.core.models.conditions.types.ExecutionFlowCondition)2 ExecutionNamespaceCondition (io.kestra.core.models.conditions.types.ExecutionNamespaceCondition)2 Execution (io.kestra.core.models.executions.Execution)2 RunContext (io.kestra.core.runners.RunContext)2 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)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 ScheduleCondition (io.kestra.core.models.conditions.ScheduleCondition)1 LogEntry (io.kestra.core.models.executions.LogEntry)1 Task (io.kestra.core.models.tasks.Task)1 AbstractTrigger (io.kestra.core.models.triggers.AbstractTrigger)1 MultipleConditionStorageInterface (io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface)1 MultipleConditionWindow (io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow)1