|
| 1 | +package com.uid2.optout.vertx; |
| 2 | + |
| 3 | +import io.vertx.core.json.JsonObject; |
| 4 | +import java.time.Instant; |
| 5 | + |
| 6 | +/** |
| 7 | + * Represents the status and result of an async delta production job on a pod. |
| 8 | + * |
| 9 | + * This class tracks the lifecycle of a delta production job including its state |
| 10 | + * (running, completed, failed), timing information, and result or error details. |
| 11 | + * |
| 12 | + */ |
| 13 | +public class DeltaProduceJobStatus { |
| 14 | + private final Instant startTime; |
| 15 | + private volatile JobState state; |
| 16 | + private volatile JsonObject result; |
| 17 | + private volatile String errorMessage; |
| 18 | + private volatile Instant endTime; |
| 19 | + |
| 20 | + public enum JobState { |
| 21 | + RUNNING, |
| 22 | + COMPLETED, |
| 23 | + FAILED |
| 24 | + } |
| 25 | + |
| 26 | + public DeltaProduceJobStatus() { |
| 27 | + this.startTime = Instant.now(); |
| 28 | + this.state = JobState.RUNNING; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Mark the job as completed with the given result. |
| 33 | + * @param result The result details as a JsonObject |
| 34 | + */ |
| 35 | + public void complete(JsonObject result) { |
| 36 | + this.result = result; |
| 37 | + this.state = JobState.COMPLETED; |
| 38 | + this.endTime = Instant.now(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Mark the job as failed with the given error message. |
| 43 | + * @param errorMessage Description of the failure |
| 44 | + */ |
| 45 | + public void fail(String errorMessage) { |
| 46 | + this.errorMessage = errorMessage; |
| 47 | + this.state = JobState.FAILED; |
| 48 | + this.endTime = Instant.now(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the current state of the job. |
| 53 | + * @return The job state |
| 54 | + */ |
| 55 | + public JobState getState() { |
| 56 | + return state; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Convert the job status to a JSON representation for API responses. |
| 61 | + * @return JsonObject with state, timing, and result/error information |
| 62 | + */ |
| 63 | + public JsonObject toJson() { |
| 64 | + JsonObject json = new JsonObject() |
| 65 | + .put("state", state.name().toLowerCase()) |
| 66 | + .put("start_time", startTime.toString()); |
| 67 | + |
| 68 | + if (endTime != null) { |
| 69 | + json.put("end_time", endTime.toString()); |
| 70 | + long durationSeconds = endTime.getEpochSecond() - startTime.getEpochSecond(); |
| 71 | + json.put("duration_seconds", durationSeconds); |
| 72 | + } |
| 73 | + |
| 74 | + if (state == JobState.COMPLETED && result != null) { |
| 75 | + json.put("result", result); |
| 76 | + } |
| 77 | + |
| 78 | + if (state == JobState.FAILED && errorMessage != null) { |
| 79 | + json.put("error", errorMessage); |
| 80 | + } |
| 81 | + |
| 82 | + return json; |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments