Search in sources :

Example 1 with TriggerContext

use of io.kestra.core.models.triggers.TriggerContext in project kestra by kestra-io.

the class AbstractScheduler method handle.

private void handle() {
    if (!this.isReady) {
        log.warn("Scheduler is not ready, waiting");
    }
    metricRegistry.counter(MetricRegistry.SCHEDULER_LOOP_COUNT).increment();
    ZonedDateTime now = now();
    synchronized (this) {
        if (log.isDebugEnabled()) {
            log.debug("Scheduler next iteration for {} with {} schedulables of {} flows", now, schedulable.size(), this.flowListeners.flows().size());
        }
        // get all that is ready from evaluation
        List<FlowWithPollingTriggerNextDate> readyForEvaluate = schedulable.stream().filter(f -> conditionService.isValid(f.getFlow(), f.getTrigger(), f.getConditionContext())).map(flowWithTrigger -> FlowWithPollingTrigger.builder().flow(flowWithTrigger.getFlow()).trigger(flowWithTrigger.getTrigger()).pollingTrigger((PollingTriggerInterface) flowWithTrigger.getTrigger()).conditionContext(flowWithTrigger.getConditionContext()).triggerContext(TriggerContext.builder().namespace(flowWithTrigger.getFlow().getNamespace()).flowId(flowWithTrigger.getFlow().getId()).flowRevision(flowWithTrigger.getFlow().getRevision()).triggerId(flowWithTrigger.getTrigger().getId()).date(now()).build()).build()).filter(f -> this.isEvaluationInterval(f, now)).filter(f -> this.isExecutionNotRunning(f, now)).map(f -> {
            synchronized (this) {
                Trigger lastTrigger = this.getLastTrigger(f, now);
                return FlowWithPollingTriggerNextDate.of(f, f.getPollingTrigger().nextEvaluationDate(f.getConditionContext(), Optional.of(lastTrigger)));
            }
        }).filter(Objects::nonNull).collect(Collectors.toList());
        if (log.isDebugEnabled()) {
            log.debug("Scheduler will evaluate for {} with {} readyForEvaluate of {} schedulables", now, readyForEvaluate.size(), schedulable.size());
        }
        metricRegistry.counter(MetricRegistry.SCHEDULER_EVALUATE_COUNT).increment(readyForEvaluate.size());
        // submit ready one to cached thread pool
        readyForEvaluate.forEach(f -> {
            schedulableNextDate.put(f.getTriggerContext().uid(), f);
            if (f.getPollingTrigger().getInterval() == null) {
                try {
                    this.handleEvaluatePollingTriggerResult(this.evaluatePollingTrigger(f));
                } catch (Exception e) {
                    AbstractScheduler.logError(f, e);
                }
            } else {
                this.addToRunning(f.getTriggerContext(), now);
                ListenableFuture<SchedulerExecutionWithTrigger> result = cachedExecutor.submit(() -> this.evaluatePollingTrigger(f));
                Futures.addCallback(result, new EvaluateFuture(this, f), cachedExecutor);
            }
        });
    }
}
Also used : java.util(java.util) RunContextFactory(io.kestra.core.runners.RunContextFactory) Getter(lombok.Getter) QueueInterface(io.kestra.core.queues.QueueInterface) ZonedDateTime(java.time.ZonedDateTime) AbstractTrigger(io.kestra.core.models.triggers.AbstractTrigger) com.google.common.util.concurrent(com.google.common.util.concurrent) ConditionContext(io.kestra.core.models.conditions.ConditionContext) PollingTriggerInterface(io.kestra.core.models.triggers.PollingTriggerInterface) ApplicationContext(io.micronaut.context.ApplicationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) TriggerContext(io.kestra.core.models.triggers.TriggerContext) Logger(org.slf4j.Logger) QueueFactoryInterface(io.kestra.core.queues.QueueFactoryInterface) TaskDefaultService(io.kestra.core.services.TaskDefaultService) ConditionService(io.kestra.core.services.ConditionService) java.util.concurrent(java.util.concurrent) SuperBuilder(lombok.experimental.SuperBuilder) Throwables(com.google.common.base.Throwables) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) Trigger(io.kestra.core.models.triggers.Trigger) Instant(java.time.Instant) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) InternalException(io.kestra.core.exceptions.InternalException) Slf4j(lombok.extern.slf4j.Slf4j) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) Await(io.kestra.core.utils.Await) ExecutorsUtils(io.kestra.core.utils.ExecutorsUtils) RunContext(io.kestra.core.runners.RunContext) Flow(io.kestra.core.models.flows.Flow) FlowListenersInterface(io.kestra.core.services.FlowListenersInterface) MetricRegistry(io.kestra.core.metrics.MetricRegistry) AllArgsConstructor(lombok.AllArgsConstructor) Inject(jakarta.inject.Inject) NoArgsConstructor(lombok.NoArgsConstructor) PollingTriggerInterface(io.kestra.core.models.triggers.PollingTriggerInterface) AbstractTrigger(io.kestra.core.models.triggers.AbstractTrigger) Trigger(io.kestra.core.models.triggers.Trigger) ZonedDateTime(java.time.ZonedDateTime) InternalException(io.kestra.core.exceptions.InternalException)

Example 2 with TriggerContext

use of io.kestra.core.models.triggers.TriggerContext in project kestra by kestra-io.

the class TestsUtils method mockTrigger.

public static Map.Entry<ConditionContext, TriggerContext> mockTrigger(RunContextFactory runContextFactory, AbstractTrigger trigger) {
    StackTraceElement caller = Thread.currentThread().getStackTrace()[2];
    Flow flow = TestsUtils.mockFlow(caller);
    TriggerContext triggerContext = TriggerContext.builder().triggerId(trigger.getId()).flowId(flow.getId()).namespace(flow.getNamespace()).flowRevision(flow.getRevision()).date(ZonedDateTime.now()).build();
    return new AbstractMap.SimpleEntry<>(ConditionContext.builder().runContext(runContextFactory.of(flow, trigger)).flow(flow).build(), triggerContext);
}
Also used : TriggerContext(io.kestra.core.models.triggers.TriggerContext) Flow(io.kestra.core.models.flows.Flow)

Aggregations

Flow (io.kestra.core.models.flows.Flow)2 TriggerContext (io.kestra.core.models.triggers.TriggerContext)2 Throwables (com.google.common.base.Throwables)1 com.google.common.util.concurrent (com.google.common.util.concurrent)1 InternalException (io.kestra.core.exceptions.InternalException)1 MetricRegistry (io.kestra.core.metrics.MetricRegistry)1 ConditionContext (io.kestra.core.models.conditions.ConditionContext)1 Execution (io.kestra.core.models.executions.Execution)1 AbstractTrigger (io.kestra.core.models.triggers.AbstractTrigger)1 PollingTriggerInterface (io.kestra.core.models.triggers.PollingTriggerInterface)1 Trigger (io.kestra.core.models.triggers.Trigger)1 QueueFactoryInterface (io.kestra.core.queues.QueueFactoryInterface)1 QueueInterface (io.kestra.core.queues.QueueInterface)1 RunContext (io.kestra.core.runners.RunContext)1 RunContextFactory (io.kestra.core.runners.RunContextFactory)1 ConditionService (io.kestra.core.services.ConditionService)1 FlowListenersInterface (io.kestra.core.services.FlowListenersInterface)1 TaskDefaultService (io.kestra.core.services.TaskDefaultService)1 Await (io.kestra.core.utils.Await)1 ExecutorsUtils (io.kestra.core.utils.ExecutorsUtils)1