Skip to content

Commit

Permalink
feat: add a trigger example
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Aug 9, 2024
1 parent 99e8e82 commit 18a5bad
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/main/java/io/kestra/plugin/templates/Trigger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.kestra.plugin.templates;

import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.conditions.ConditionContext;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.triggers.*;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.time.Duration;
import java.util.Optional;

@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Plugin
@Schema(
title = "Trigger an execution randomly",
description ="Trigger an execution randomly"
)
public class Trigger extends AbstractTrigger implements PollingTriggerInterface, TriggerOutput<Trigger.Output> {
@Builder.Default
private final Duration interval = Duration.ofSeconds(60);

protected Double min = 0.5;

@Override
public Optional<Execution> evaluate(ConditionContext conditionContext, TriggerContext context) {
double random = Math.random();
if (random < this.min) {
return Optional.empty();
}

RunContext runContext = conditionContext.getRunContext();
runContext.logger().info("Will create an execution");
Execution execution = TriggerService.generateExecution(
this,
conditionContext,
context,
Output.builder().random(random).build()
);

return Optional.of(execution);
}

@Builder
@Getter
public static class Output implements io.kestra.core.models.tasks.Output {
private Double random;
}
}

0 comments on commit 18a5bad

Please sign in to comment.