@@ -14,7 +14,6 @@ import { RunLogger, SendDebugLogOptions } from "./logger.js";
14
14
import { RunnerEnv } from "./env.js" ;
15
15
import { WorkloadHttpClient } from "@trigger.dev/core/v3/workers" ;
16
16
import { setTimeout as sleep } from "timers/promises" ;
17
- import { RunExecutionHeartbeat } from "./heartbeat.js" ;
18
17
import { RunExecutionSnapshotPoller } from "./poller.js" ;
19
18
import { assertExhaustive , tryCatch } from "@trigger.dev/core/utils" ;
20
19
import { MetadataClient } from "./overrides.js" ;
@@ -63,7 +62,6 @@ export class RunExecution {
63
62
private restoreCount : number ;
64
63
65
64
private taskRunProcess ?: TaskRunProcess ;
66
- private runHeartbeat ?: RunExecutionHeartbeat ;
67
65
private snapshotPoller ?: RunExecutionSnapshotPoller ;
68
66
69
67
constructor ( opts : RunExecutionOptions ) {
@@ -105,7 +103,7 @@ export class RunExecution {
105
103
envVars : Record < string , string > ;
106
104
isWarmStart ?: boolean ;
107
105
} ) {
108
- return new TaskRunProcess ( {
106
+ const taskRunProcess = new TaskRunProcess ( {
109
107
workerManifest : this . workerManifest ,
110
108
env : {
111
109
...envVars ,
@@ -123,6 +121,29 @@ export class RunExecution {
123
121
} ,
124
122
isWarmStart,
125
123
} ) . initialize ( ) ;
124
+
125
+ taskRunProcess . onTaskRunHeartbeat . attach ( async ( runId ) => {
126
+ if ( ! this . runFriendlyId ) {
127
+ this . sendDebugLog ( "onTaskRunHeartbeat: missing run ID" , { heartbeatRunId : runId } ) ;
128
+ return ;
129
+ }
130
+
131
+ if ( runId !== this . runFriendlyId ) {
132
+ this . sendDebugLog ( "onTaskRunHeartbeat: mismatched run ID" , {
133
+ heartbeatRunId : runId ,
134
+ expectedRunId : this . runFriendlyId ,
135
+ } ) ;
136
+ return ;
137
+ }
138
+
139
+ const [ error ] = await tryCatch ( this . onHeartbeat ( ) ) ;
140
+
141
+ if ( error ) {
142
+ this . sendDebugLog ( "onTaskRunHeartbeat: failed" , { error : error . message } ) ;
143
+ }
144
+ } ) ;
145
+
146
+ return taskRunProcess ;
126
147
}
127
148
128
149
/**
@@ -229,7 +250,6 @@ export class RunExecution {
229
250
this . currentSnapshotId = snapshot . friendlyId ;
230
251
231
252
// Update services
232
- this . runHeartbeat ?. updateSnapshotId ( snapshot . friendlyId ) ;
233
253
this . snapshotPoller ?. updateSnapshotId ( snapshot . friendlyId ) ;
234
254
235
255
switch ( snapshot . executionStatus ) {
@@ -450,13 +470,6 @@ export class RunExecution {
450
470
this . podScheduledAt = runOpts . podScheduledAt ;
451
471
452
472
// Create and start services
453
- this . runHeartbeat = new RunExecutionHeartbeat ( {
454
- runFriendlyId : this . runFriendlyId ,
455
- snapshotFriendlyId : this . currentSnapshotId ,
456
- httpClient : this . httpClient ,
457
- logger : this . logger ,
458
- heartbeatIntervalSeconds : this . env . TRIGGER_HEARTBEAT_INTERVAL_SECONDS ,
459
- } ) ;
460
473
this . snapshotPoller = new RunExecutionSnapshotPoller ( {
461
474
runFriendlyId : this . runFriendlyId ,
462
475
snapshotFriendlyId : this . currentSnapshotId ,
@@ -466,7 +479,6 @@ export class RunExecution {
466
479
handleSnapshotChange : this . handleSnapshotChange . bind ( this ) ,
467
480
} ) ;
468
481
469
- this . runHeartbeat . start ( ) ;
470
482
this . snapshotPoller . start ( ) ;
471
483
472
484
const [ startError , start ] = await tryCatch (
@@ -839,9 +851,6 @@ export class RunExecution {
839
851
this . env . override ( overrides ) ;
840
852
841
853
// Update services with new values
842
- if ( overrides . TRIGGER_HEARTBEAT_INTERVAL_SECONDS ) {
843
- this . runHeartbeat ?. updateInterval ( this . env . TRIGGER_HEARTBEAT_INTERVAL_SECONDS * 1000 ) ;
844
- }
845
854
if ( overrides . TRIGGER_SNAPSHOT_POLL_INTERVAL_SECONDS ) {
846
855
this . snapshotPoller ?. updateInterval ( this . env . TRIGGER_SNAPSHOT_POLL_INTERVAL_SECONDS * 1000 ) ;
847
856
}
@@ -857,6 +866,26 @@ export class RunExecution {
857
866
}
858
867
}
859
868
869
+ private async onHeartbeat ( ) {
870
+ if ( ! this . runFriendlyId ) {
871
+ this . sendDebugLog ( "Heartbeat: missing run ID" ) ;
872
+ return ;
873
+ }
874
+
875
+ if ( ! this . currentSnapshotId ) {
876
+ this . sendDebugLog ( "Heartbeat: missing snapshot ID" ) ;
877
+ return ;
878
+ }
879
+
880
+ this . sendDebugLog ( "Heartbeat: started" ) ;
881
+
882
+ const response = await this . httpClient . heartbeatRun ( this . runFriendlyId , this . currentSnapshotId ) ;
883
+
884
+ if ( ! response . success ) {
885
+ this . sendDebugLog ( "Heartbeat: failed" , { error : response . error } ) ;
886
+ }
887
+ }
888
+
860
889
sendDebugLog (
861
890
message : string ,
862
891
properties ?: SendDebugLogOptions [ "properties" ] ,
@@ -917,7 +946,6 @@ export class RunExecution {
917
946
}
918
947
919
948
private stopServices ( ) {
920
- this . runHeartbeat ?. stop ( ) ;
921
949
this . snapshotPoller ?. stop ( ) ;
922
950
}
923
951
}
0 commit comments