|
| 1 | +import { HeartbeatService, RunExecutionData } from "@trigger.dev/core/v3"; |
| 2 | +import { WorkloadHttpClient } from "@trigger.dev/core/v3/runEngineWorker"; |
| 3 | +import { RunLogger } from "./logger.js"; |
| 4 | + |
| 5 | +export type RunExecutionHeartbeatOptions = { |
| 6 | + runFriendlyId: string; |
| 7 | + snapshotFriendlyId: string; |
| 8 | + httpClient: WorkloadHttpClient; |
| 9 | + logger: RunLogger; |
| 10 | + heartbeatIntervalSeconds: number; |
| 11 | +}; |
| 12 | + |
| 13 | +export class RunExecutionHeartbeat { |
| 14 | + private readonly logger: RunLogger; |
| 15 | + private readonly heartbeat: HeartbeatService; |
| 16 | + private readonly httpClient: WorkloadHttpClient; |
| 17 | + |
| 18 | + private readonly runFriendlyId: string; |
| 19 | + private snapshotFriendlyId: string; |
| 20 | + |
| 21 | + constructor(opts: RunExecutionHeartbeatOptions) { |
| 22 | + this.logger = opts.logger; |
| 23 | + this.httpClient = opts.httpClient; |
| 24 | + |
| 25 | + this.runFriendlyId = opts.runFriendlyId; |
| 26 | + this.snapshotFriendlyId = opts.snapshotFriendlyId; |
| 27 | + |
| 28 | + this.heartbeat = new HeartbeatService({ |
| 29 | + heartbeat: async () => { |
| 30 | + this.logger.sendDebugLog({ |
| 31 | + runId: this.runFriendlyId, |
| 32 | + message: "heartbeat: started", |
| 33 | + }); |
| 34 | + |
| 35 | + const response = await this.httpClient.heartbeatRun( |
| 36 | + this.runFriendlyId, |
| 37 | + this.snapshotFriendlyId |
| 38 | + ); |
| 39 | + |
| 40 | + if (!response.success) { |
| 41 | + this.logger.sendDebugLog({ |
| 42 | + runId: this.runFriendlyId, |
| 43 | + message: "heartbeat: failed", |
| 44 | + properties: { |
| 45 | + error: response.error, |
| 46 | + }, |
| 47 | + }); |
| 48 | + } |
| 49 | + }, |
| 50 | + intervalMs: opts.heartbeatIntervalSeconds * 1000, |
| 51 | + leadingEdge: false, |
| 52 | + onError: async (error) => { |
| 53 | + this.logger.sendDebugLog({ |
| 54 | + runId: this.runFriendlyId, |
| 55 | + message: "Failed to send heartbeat", |
| 56 | + properties: { error: error instanceof Error ? error.message : String(error) }, |
| 57 | + }); |
| 58 | + }, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + resetCurrentInterval() { |
| 63 | + this.heartbeat.resetCurrentInterval(); |
| 64 | + } |
| 65 | + |
| 66 | + updateSnapshotId(snapshotId: string) { |
| 67 | + this.snapshotFriendlyId = snapshotId; |
| 68 | + } |
| 69 | + |
| 70 | + updateInterval(intervalMs: number) { |
| 71 | + this.heartbeat.updateInterval(intervalMs); |
| 72 | + } |
| 73 | + |
| 74 | + start() { |
| 75 | + this.heartbeat.start(); |
| 76 | + } |
| 77 | + |
| 78 | + stop() { |
| 79 | + this.heartbeat.stop(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +type RunExecutionSnapshotPollerOptions = { |
| 84 | + runFriendlyId: string; |
| 85 | + snapshotFriendlyId: string; |
| 86 | + httpClient: WorkloadHttpClient; |
| 87 | + logger: RunLogger; |
| 88 | + snapshotPollIntervalSeconds: number; |
| 89 | + handleSnapshotChange: (execution: RunExecutionData) => Promise<void>; |
| 90 | +}; |
| 91 | + |
| 92 | +class RunExecutionSnapshotPoller { |
| 93 | + private readonly logger: RunLogger; |
| 94 | + private readonly poller: HeartbeatService; |
| 95 | + private readonly httpClient: WorkloadHttpClient; |
| 96 | + |
| 97 | + private readonly runFriendlyId: string; |
| 98 | + private readonly snapshotFriendlyId: string; |
| 99 | + private readonly snapshotPollIntervalSeconds: number; |
| 100 | + |
| 101 | + private readonly handleSnapshotChange: (execution: RunExecutionData) => Promise<void>; |
| 102 | + |
| 103 | + constructor(opts: RunExecutionSnapshotPollerOptions) { |
| 104 | + this.logger = opts.logger; |
| 105 | + this.httpClient = opts.httpClient; |
| 106 | + |
| 107 | + this.runFriendlyId = opts.runFriendlyId; |
| 108 | + this.snapshotFriendlyId = opts.snapshotFriendlyId; |
| 109 | + |
| 110 | + this.handleSnapshotChange = opts.handleSnapshotChange; |
| 111 | + |
| 112 | + this.poller = new HeartbeatService({ |
| 113 | + heartbeat: async () => { |
| 114 | + if (!this.runFriendlyId) { |
| 115 | + this.logger.sendDebugLog({ |
| 116 | + runId: this.runFriendlyId, |
| 117 | + message: "Skipping snapshot poll, no run ID", |
| 118 | + }); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + this.logger.sendDebugLog({ |
| 123 | + runId: this.runFriendlyId, |
| 124 | + message: "Polling for latest snapshot", |
| 125 | + }); |
| 126 | + |
| 127 | + this.logger.sendDebugLog({ |
| 128 | + runId: this.runFriendlyId, |
| 129 | + message: `snapshot poll: started`, |
| 130 | + properties: { |
| 131 | + snapshotId: this.snapshotFriendlyId, |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + const response = await this.httpClient.getRunExecutionData(this.runFriendlyId); |
| 136 | + |
| 137 | + if (!response.success) { |
| 138 | + this.logger.sendDebugLog({ |
| 139 | + runId: this.runFriendlyId, |
| 140 | + message: "Snapshot poll failed", |
| 141 | + properties: { |
| 142 | + error: response.error, |
| 143 | + }, |
| 144 | + }); |
| 145 | + |
| 146 | + this.logger.sendDebugLog({ |
| 147 | + runId: this.runFriendlyId, |
| 148 | + message: `snapshot poll: failed`, |
| 149 | + properties: { |
| 150 | + snapshotId: this.snapshotFriendlyId, |
| 151 | + error: response.error, |
| 152 | + }, |
| 153 | + }); |
| 154 | + |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + await this.handleSnapshotChange(response.data.execution); |
| 159 | + }, |
| 160 | + intervalMs: this.snapshotPollIntervalSeconds * 1000, |
| 161 | + leadingEdge: false, |
| 162 | + onError: async (error) => { |
| 163 | + this.logger.sendDebugLog({ |
| 164 | + runId: this.runFriendlyId, |
| 165 | + message: "Failed to poll for snapshot", |
| 166 | + properties: { error: error instanceof Error ? error.message : String(error) }, |
| 167 | + }); |
| 168 | + }, |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + updateInterval(intervalMs: number) { |
| 173 | + this.poller.updateInterval(intervalMs); |
| 174 | + } |
| 175 | + |
| 176 | + start() { |
| 177 | + this.poller.start(); |
| 178 | + } |
| 179 | + |
| 180 | + stop() { |
| 181 | + this.poller.stop(); |
| 182 | + } |
| 183 | +} |
0 commit comments