|
7 | 7 | import java.util.HashMap; |
8 | 8 | import java.util.List; |
9 | 9 | import java.util.Map; |
| 10 | +import java.util.Objects; |
10 | 11 | import java.util.Optional; |
11 | 12 |
|
12 | 13 | import org.slf4j.Logger; |
|
60 | 61 | * |
61 | 62 | */ |
62 | 63 | public final class TableTriggerReconciler implements Reconciler { |
63 | | - private final static Logger log = LoggerFactory.getLogger(TableTriggerReconciler.class); |
64 | | - private final static String TRIGGER_KEY = "trigger"; |
65 | | - private final static String TRIGGER_TIMESTAMP_KEY = "triggerTimestamp"; |
| 64 | + private static final Logger log = LoggerFactory.getLogger(TableTriggerReconciler.class); |
| 65 | + static final String TRIGGER_KEY = "trigger"; |
| 66 | + static final String TRIGGER_TIMESTAMP_KEY = "triggerTimestamp"; |
66 | 67 |
|
67 | 68 | private final K8sApi<V1alpha1TableTrigger, V1alpha1TableTriggerList> tableTriggerApi; |
68 | 69 | private final K8sApi<V1Job, V1JobList> jobApi; |
@@ -146,6 +147,7 @@ public Result reconcile(Request request) { |
146 | 147 | jobApi.delete(job); |
147 | 148 | return new Result(true); // retry |
148 | 149 | } else { |
| 150 | + maybeUpdateJobAnnotation(job, status.getTimestamp()); |
149 | 151 | log.info("Job for TableTrigger {} still running from a previous trigger event.", name); |
150 | 152 | return new Result(true, pendingRetryDuration()); // retry later |
151 | 153 | } |
@@ -195,5 +197,18 @@ public static Controller controller(K8sContext context) { |
195 | 197 | .watch(x -> ControllerBuilder.controllerWatchBuilder(V1alpha1TableTrigger.class, x).build()) |
196 | 198 | .build(); |
197 | 199 | } |
| 200 | + |
| 201 | + void maybeUpdateJobAnnotation(V1Job job, OffsetDateTime timestamp) throws SQLException { |
| 202 | + Map<String, String> annotations = Objects.requireNonNull(job.getMetadata()).getAnnotations(); |
| 203 | + if (annotations != null) { |
| 204 | + String existing = annotations.get(TRIGGER_TIMESTAMP_KEY); |
| 205 | + if (existing != null && timestamp.isAfter(OffsetDateTime.parse(existing))) { |
| 206 | + annotations.put(TRIGGER_TIMESTAMP_KEY, timestamp.toString()); |
| 207 | + job.getMetadata().setAnnotations(annotations); |
| 208 | + jobApi.update(job); |
| 209 | + log.info("Updated {} in Job {} annotation to {}", TRIGGER_TIMESTAMP_KEY, job.getMetadata().getName(), timestamp); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
198 | 213 | } |
199 | 214 |
|
0 commit comments