|
| 1 | +package io.kestra.plugin.airflow.dags; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import io.kestra.core.models.annotations.Example; |
| 5 | +import io.kestra.core.models.annotations.Plugin; |
| 6 | +import io.kestra.core.models.annotations.PluginProperty; |
| 7 | +import io.kestra.core.models.tasks.RunnableTask; |
| 8 | +import io.kestra.core.runners.RunContext; |
| 9 | +import io.kestra.core.utils.Await; |
| 10 | +import io.kestra.plugin.airflow.AirflowConnection; |
| 11 | +import io.kestra.plugin.airflow.model.DagRunResponse; |
| 12 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 13 | +import jakarta.validation.constraints.NotNull; |
| 14 | +import lombok.*; |
| 15 | +import lombok.experimental.SuperBuilder; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.time.LocalDateTime; |
| 20 | +import java.time.ZonedDateTime; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +import static io.kestra.core.utils.Rethrow.throwSupplier; |
| 25 | + |
| 26 | +@SuperBuilder |
| 27 | +@ToString |
| 28 | +@EqualsAndHashCode |
| 29 | +@Getter |
| 30 | +@NoArgsConstructor |
| 31 | +@Slf4j |
| 32 | +@Schema( |
| 33 | + title = "Trigger Airflow DAG", |
| 34 | + description = "Trigger an Airflow DAG run and wait for its completion." |
| 35 | +) |
| 36 | +@Plugin( |
| 37 | + examples = { |
| 38 | + @Example( |
| 39 | + title = "Basic authorization", |
| 40 | + code = { |
| 41 | + " - id: trigger_dag", |
| 42 | + " type: io.kestra.plugin.airflow.TriggerDagRun", |
| 43 | + " baseUrl: http://airflow.example.com", |
| 44 | + " dagId: example_dag", |
| 45 | + " checkFrequency: PT30S", |
| 46 | + " interval: PT30S", |
| 47 | + " maxIterations: 100", |
| 48 | + " maxDuration: PT1H", |
| 49 | + " options:", |
| 50 | + " basicAuthUser: myusername", |
| 51 | + " basicAuthPassword: mypassword" |
| 52 | + } |
| 53 | + ), |
| 54 | + @Example( |
| 55 | + title = "Bearer authorization", |
| 56 | + code = { |
| 57 | + " - id: trigger_dag", |
| 58 | + " type: io.kestra.plugin.airflow.TriggerDagRun", |
| 59 | + " baseUrl: http://airflow.example.com", |
| 60 | + " dagId: example_dag", |
| 61 | + " checkFrequency: PT30S", |
| 62 | + " interval: PT30S", |
| 63 | + " headers:", |
| 64 | + " authorization: 'Bearer {{ TOKEN }}'" |
| 65 | + } |
| 66 | + ), |
| 67 | + @Example( |
| 68 | + title = "Basic authorization. Custom body", |
| 69 | + code = { |
| 70 | + " - id: trigger_dag", |
| 71 | + " type: io.kestra.plugin.airflow.TriggerDagRun", |
| 72 | + " baseUrl: http://airflow.example.com", |
| 73 | + " dagId: example_dag", |
| 74 | + " checkFrequency: PT30S", |
| 75 | + " interval: PT30S", |
| 76 | + " options:", |
| 77 | + " basicAuthUser: myusername", |
| 78 | + " basicAuthPassword: mypassword", |
| 79 | + " body: |", |
| 80 | + " {", |
| 81 | + " \"conf\": {", |
| 82 | + " \"source\": \"kestra\",", |
| 83 | + " \"flow\": \"{{ flow.id }}\",", |
| 84 | + " \"namespace\": \"{{ flow.namespace }}\",", |
| 85 | + " \"task\": \"{{ task.id }}\",", |
| 86 | + " \"execution\": \"{{ execution.id }}\"", |
| 87 | + " }", |
| 88 | + " }" |
| 89 | + } |
| 90 | + ), |
| 91 | + } |
| 92 | +) |
| 93 | +public class TriggerDagRun extends AirflowConnection implements RunnableTask<TriggerDagRun.Output> { |
| 94 | + |
| 95 | + @Schema( |
| 96 | + title = "The ID of the DAG to trigger" |
| 97 | + ) |
| 98 | + @NotNull |
| 99 | + @PluginProperty(dynamic = true) |
| 100 | + private String dagId; |
| 101 | + |
| 102 | + @Schema( |
| 103 | + title = "The job ID to check status for." |
| 104 | + ) |
| 105 | + @PluginProperty(dynamic = true) |
| 106 | + private String jobId; |
| 107 | + |
| 108 | + @Schema( |
| 109 | + title = "The maximum total wait duration." |
| 110 | + ) |
| 111 | + @PluginProperty |
| 112 | + @Builder.Default |
| 113 | + Duration maxDuration = Duration.ofMinutes(60); |
| 114 | + |
| 115 | + @Schema( |
| 116 | + title = "Specify how often the task should poll for the DAG run status." |
| 117 | + ) |
| 118 | + @PluginProperty |
| 119 | + @Builder.Default |
| 120 | + Duration pollFrequency = Duration.ofSeconds(1); |
| 121 | + |
| 122 | + @Schema( |
| 123 | + title = "Whether task should wait for the DAG to run to completion", |
| 124 | + description = "Default value is false" |
| 125 | + ) |
| 126 | + @PluginProperty |
| 127 | + @Builder.Default |
| 128 | + private Boolean wait = Boolean.FALSE; |
| 129 | + |
| 130 | + @Schema( |
| 131 | + title = "Overrides the default configuration payload" |
| 132 | + ) |
| 133 | + @PluginProperty |
| 134 | + private Map<String, Object> body; |
| 135 | + |
| 136 | + @Override |
| 137 | + public Output run(RunContext runContext) throws Exception { |
| 138 | + String dagId = runContext.render(this.dagId); |
| 139 | + |
| 140 | + DagRunResponse triggerResult = triggerDag(runContext, dagId, buildBody(runContext)); |
| 141 | + String dagRunId = triggerResult.getDagRunId(); |
| 142 | + |
| 143 | + Output.OutputBuilder outputBuilder = Output.builder() |
| 144 | + .dagId(dagId) |
| 145 | + .dagRunId(dagRunId) |
| 146 | + .state(triggerResult.getState()); |
| 147 | + |
| 148 | + if (this.wait.equals(Boolean.FALSE)) { |
| 149 | + return outputBuilder.build(); |
| 150 | + } |
| 151 | + |
| 152 | + DagRunResponse statusResult = Await.until( |
| 153 | + throwSupplier(() -> { |
| 154 | + DagRunResponse result = getDagStatus(runContext, dagId, dagRunId); |
| 155 | + String state = result.getState(); |
| 156 | + |
| 157 | + if ("success".equalsIgnoreCase(state) || "failed".equalsIgnoreCase(state)) { |
| 158 | + return result; |
| 159 | + } |
| 160 | + |
| 161 | + return null; |
| 162 | + }), |
| 163 | + this.pollFrequency, |
| 164 | + this.maxDuration |
| 165 | + ); |
| 166 | + |
| 167 | + if (statusResult == null) { |
| 168 | + throw new IllegalStateException("DAG run did not complete within the specified timeout"); |
| 169 | + } |
| 170 | + |
| 171 | + return outputBuilder |
| 172 | + .state(statusResult.getState()) |
| 173 | + .started(statusResult.getStartDate().toLocalDateTime()) |
| 174 | + .ended(statusResult.getEndDate().toLocalDateTime()) |
| 175 | + .build(); |
| 176 | + } |
| 177 | + |
| 178 | + private String buildBody(RunContext runContext) throws JsonProcessingException { |
| 179 | + RunContext.FlowInfo flowInfo = runContext.flowInfo(); |
| 180 | + |
| 181 | + Map<String, Object> conf = this.body; |
| 182 | + |
| 183 | + if (this.body == null) { |
| 184 | + conf = Map.of( |
| 185 | + "source", "kestra", |
| 186 | + "flow", flowInfo.id(), |
| 187 | + "namespace", flowInfo.namespace(), |
| 188 | + "task", this.id, |
| 189 | + "execution", runContext.getTriggerExecutionId() |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + return objectMapper.writeValueAsString(conf); |
| 194 | + } |
| 195 | + |
| 196 | + @Getter |
| 197 | + @Builder |
| 198 | + public static class Output implements io.kestra.core.models.tasks.Output { |
| 199 | + @Schema( |
| 200 | + title = "DAG ID" |
| 201 | + ) |
| 202 | + private String dagId; |
| 203 | + |
| 204 | + @Schema( |
| 205 | + title = "DAG run ID" |
| 206 | + ) |
| 207 | + private String dagRunId; |
| 208 | + |
| 209 | + @Schema( |
| 210 | + title = "State" |
| 211 | + ) |
| 212 | + private String state; |
| 213 | + |
| 214 | + @Schema( |
| 215 | + title = "DAG run started date" |
| 216 | + ) |
| 217 | + private LocalDateTime started; |
| 218 | + |
| 219 | + @Schema( |
| 220 | + title = "DAG run completed date" |
| 221 | + ) |
| 222 | + private LocalDateTime ended; |
| 223 | + } |
| 224 | + |
| 225 | +} |
0 commit comments