Develop a Trigger​Develop a ​Trigger

Here is how you can develop a Trigger.

The Trigger example below will create an execution randomly

You need to extend PollingTriggerInterface and implement the Optional<Execution> evaluate(ConditionContext conditionContext, TriggerContext context) method.

You can have any properties that you want, like for any task (validation, documentation, etc.), and everything works the same way.

The evaluate method will receive these arguments:

  • ConditionContext conditionContext: a ConditionContext which includes various properties such as the RunContext in order to render your properties.
  • TriggerContext context: to have the context of this call (flow, execution, trigger, date).

In this method, you can add any logic that you want: connect to a database, connect to remote file systems, ... You don't have to take care of resources, Kestra will run this method in its own thread.

This method must return an Optional<Execution> with:

  • Optional.empty(): if the condition is not validated.
  • Optional.of(execution): with the execution created if the condition is validated.

You have to provide an Output for any output needed (result of query, result of file system listing, etc.) that will be available for the flow tasks within the {{ trigger.* }} variables.

Realtime Triggers

To create a Realtime Trigger, your plugin class must:

  1. Implement the Interface: Extend the RealtimeTriggerInterface. This interface requires the implementation of the evaluate method.
  2. Overwrite the evaluate Method: Implement the following method signature:
java
// Method required by RealtimeTriggerInterface
Publisher<Execution> evaluate(ConditionContext conditionContext, TriggerContext triggerContext) throws Exception;

Parameters:

  • The method accepts two essential context objects:
    ConditionContext conditionContext: Provides access to the flow's runtime environment, including the RunContext (used for logging, accessing configuration, and accessing storage).
    TriggerContext triggerContext: Provides metadata about the flow, such as the flow ID, namespace and tenant ID.

Return Type and Purpose:

  • The return type, Publisher<Execution>, is a reactive stream (from Reactive Streams) that allows the trigger to continuously listen for events.
    Purpose: Unlike Polling Triggers, this method maintains a connection or subscription to the external system (e.g., a Kafka topic). Every time the publisher emits a new Execution object, the flow is instantly triggered.
    Implementation Guidance: Your implementation should focus on the logic required to subscribe to the external system and emit a new Execution object whenever a relevant event is received.

Documentation

Remember to document your triggers. For this, we provide a set of annotations explained in the Document each plugin section.

Was this page helpful?