Search in sources :

Example 1 with Trigger

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

the class SchedulerTriggerStateInterfaceTest method all.

@Test
void all() {
    Trigger.TriggerBuilder<?, ?> builder = trigger();
    Optional<Trigger> find = triggerState.findLast(builder.build());
    assertThat(find.isPresent(), is(false));
    Trigger save = triggerState.save(builder.build());
    find = triggerState.findLast(save);
    assertThat(find.isPresent(), is(true));
    assertThat(find.get().getExecutionId(), is(save.getExecutionId()));
    save = triggerState.save(builder.executionId(IdUtils.create()).build());
    find = triggerState.findLast(save);
    assertThat(find.isPresent(), is(true));
    assertThat(find.get().getExecutionId(), is(save.getExecutionId()));
}
Also used : Trigger(io.kestra.core.models.triggers.Trigger) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 2 with Trigger

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

the class ExecutorTriggerCleaner method topology.

public StreamsBuilder topology() {
    StreamsBuilder builder = new KafkaStreamsBuilder();
    KStream<String, Executor> executorKStream = kafkaStreamSourceService.executorKStream(builder);
    KStream<String, Executor> executionWithFlowKStream = kafkaStreamSourceService.executorWithFlow(executorKStream, false);
    GlobalKTable<String, Trigger> triggerGlobalKTable = kafkaStreamSourceService.triggerGlobalKTable(builder);
    executionWithFlowKStream.filter((key, value) -> value.getExecution().getTrigger() != null, Named.as("cleanTrigger-hasTrigger-filter")).filter((key, value) -> conditionService.isTerminatedWithListeners(value.getFlow(), value.getExecution()), Named.as("cleanTrigger-terminated-filter")).join(triggerGlobalKTable, (key, executionWithFlow) -> Trigger.uid(executionWithFlow.getExecution()), (execution, trigger) -> trigger.resetExecution(), Named.as("cleanTrigger-join")).selectKey((key, value) -> queueService.key(value)).to(kafkaAdminService.getTopicName(Trigger.class), Produced.with(Serdes.String(), JsonSerde.of(Trigger.class)));
    return builder;
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaAdminService(io.kestra.runner.kafka.services.KafkaAdminService) Produced(org.apache.kafka.streams.kstream.Produced) ConditionService(io.kestra.core.services.ConditionService) Singleton(jakarta.inject.Singleton) Trigger(io.kestra.core.models.triggers.Trigger) QueueService(io.kestra.core.queues.QueueService) KStream(org.apache.kafka.streams.kstream.KStream) JsonSerde(io.kestra.runner.kafka.serializers.JsonSerde) Slf4j(lombok.extern.slf4j.Slf4j) GlobalKTable(org.apache.kafka.streams.kstream.GlobalKTable) Named(org.apache.kafka.streams.kstream.Named) Executor(io.kestra.core.runners.Executor) Serdes(org.apache.kafka.common.serialization.Serdes) Inject(jakarta.inject.Inject) KafkaStreamSourceService(io.kestra.runner.kafka.services.KafkaStreamSourceService) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder) KafkaQueueEnabled(io.kestra.runner.kafka.KafkaQueueEnabled) Executor(io.kestra.core.runners.Executor) Trigger(io.kestra.core.models.triggers.Trigger) KafkaStreamsBuilder(io.kestra.runner.kafka.services.KafkaStreamsBuilder)

Example 3 with Trigger

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

the class AbstractScheduler method getLastTrigger.

private Trigger getLastTrigger(FlowWithPollingTrigger f, ZonedDateTime now) {
    return triggerState.findLast(f.getTriggerContext()).orElseGet(() -> {
        ZonedDateTime nextDate = f.getPollingTrigger().nextEvaluationDate(f.getConditionContext(), Optional.empty());
        Trigger build = Trigger.builder().date(nextDate.compareTo(now) < 0 ? nextDate : now).flowId(f.getFlow().getId()).flowRevision(f.getFlow().getRevision()).namespace(f.getFlow().getNamespace()).triggerId(f.getTriggerContext().getTriggerId()).updatedDate(Instant.now()).build();
        // but since we could have some changed on the flow in meantime, we wait 1 min before saving them.
        if (triggerStateSaved.containsKey(build.uid())) {
            Trigger cachedTrigger = triggerStateSaved.get(build.uid());
            if (cachedTrigger.getUpdatedDate() != null && Instant.now().isAfter(cachedTrigger.getUpdatedDate().plusSeconds(60))) {
                triggerState.save(build);
                triggerStateSaved.remove(build.uid());
            }
            return cachedTrigger;
        } else {
            triggerStateSaved.put(build.uid(), build);
        }
        return build;
    });
}
Also used : AbstractTrigger(io.kestra.core.models.triggers.AbstractTrigger) Trigger(io.kestra.core.models.triggers.Trigger) ZonedDateTime(java.time.ZonedDateTime)

Example 4 with Trigger

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

the class AbstractScheduler method saveLastTriggerAndEmitExecution.

protected synchronized void saveLastTriggerAndEmitExecution(SchedulerExecutionWithTrigger executionWithTrigger) {
    Trigger trigger = Trigger.of(executionWithTrigger.getTriggerContext(), executionWithTrigger.getExecution());
    this.triggerState.save(trigger);
    this.executionQueue.emit(executionWithTrigger.getExecution());
}
Also used : AbstractTrigger(io.kestra.core.models.triggers.AbstractTrigger) Trigger(io.kestra.core.models.triggers.Trigger)

Example 5 with Trigger

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

the class RestoreQueueService method triggers.

public int triggers(boolean noRecreate) {
    TriggerRepositoryInterface triggerRepository = applicationContext.getBean(TriggerRepositoryInterface.class);
    List<Trigger> triggers = new ArrayList<>(triggerRepository.findAll());
    return this.send(triggers, QueueFactoryInterface.TRIGGER_NAMED, Trigger.class, noRecreate);
}
Also used : TriggerRepositoryInterface(io.kestra.core.repositories.TriggerRepositoryInterface) Trigger(io.kestra.core.models.triggers.Trigger) ArrayList(java.util.ArrayList)

Aggregations

Trigger (io.kestra.core.models.triggers.Trigger)12 AbstractTrigger (io.kestra.core.models.triggers.AbstractTrigger)6 Execution (io.kestra.core.models.executions.Execution)4 Inject (jakarta.inject.Inject)4 Singleton (jakarta.inject.Singleton)4 Slf4j (lombok.extern.slf4j.Slf4j)4 Flow (io.kestra.core.models.flows.Flow)3 ConditionService (io.kestra.core.services.ConditionService)3 ZonedDateTime (java.time.ZonedDateTime)3 Collectors (java.util.stream.Collectors)3 AllArgsConstructor (lombok.AllArgsConstructor)3 Getter (lombok.Getter)3 Throwables (com.google.common.base.Throwables)2 com.google.common.util.concurrent (com.google.common.util.concurrent)2 InternalException (io.kestra.core.exceptions.InternalException)2 MetricRegistry (io.kestra.core.metrics.MetricRegistry)2 ConditionContext (io.kestra.core.models.conditions.ConditionContext)2 PollingTriggerInterface (io.kestra.core.models.triggers.PollingTriggerInterface)2 TriggerContext (io.kestra.core.models.triggers.TriggerContext)2 QueueFactoryInterface (io.kestra.core.queues.QueueFactoryInterface)2