11import { WorkloadHttpClient } from "@trigger.dev/core/v3/runEngineWorker" ;
2- import { RunLogger } from "./logger.js" ;
2+ import { RunLogger , SendDebugLogOptions } from "./logger.js" ;
33import { IntervalService , RunExecutionData } from "@trigger.dev/core/v3" ;
44
55export type RunExecutionSnapshotPollerOptions = {
@@ -14,89 +14,65 @@ export type RunExecutionSnapshotPollerOptions = {
1414export class RunExecutionSnapshotPoller {
1515 private runFriendlyId : string ;
1616 private snapshotFriendlyId : string ;
17+ private enabled : boolean ;
1718
1819 private readonly httpClient : WorkloadHttpClient ;
1920 private readonly logger : RunLogger ;
20- private readonly snapshotPollIntervalMs : number ;
2121 private readonly handleSnapshotChange : ( runData : RunExecutionData ) => Promise < void > ;
2222 private readonly poller : IntervalService ;
2323
2424 constructor ( opts : RunExecutionSnapshotPollerOptions ) {
25+ this . enabled = false ;
26+
2527 this . runFriendlyId = opts . runFriendlyId ;
2628 this . snapshotFriendlyId = opts . snapshotFriendlyId ;
2729 this . httpClient = opts . httpClient ;
2830 this . logger = opts . logger ;
29- this . snapshotPollIntervalMs = opts . snapshotPollIntervalSeconds * 1000 ;
3031 this . handleSnapshotChange = opts . handleSnapshotChange ;
3132
32- this . logger . sendDebugLog ( {
33- runId : this . runFriendlyId ,
34- message : "RunExecutionSnapshotPoller" ,
35- properties : {
36- runFriendlyId : this . runFriendlyId ,
37- snapshotFriendlyId : this . snapshotFriendlyId ,
38- snapshotPollIntervalSeconds : opts . snapshotPollIntervalSeconds ,
39- } ,
40- } ) ;
33+ const intervalMs = opts . snapshotPollIntervalSeconds * 1000 ;
4134
4235 this . poller = new IntervalService ( {
4336 onInterval : async ( ) => {
44- if ( ! this . runFriendlyId ) {
45- this . logger . sendDebugLog ( {
46- runId : this . runFriendlyId ,
47- message : "Skipping snapshot poll, no run ID" ,
48- } ) ;
49- return ;
50- }
51-
52- this . logger . sendDebugLog ( {
53- runId : this . runFriendlyId ,
54- message : "Polling for latest snapshot" ,
55- } ) ;
56-
57- this . logger . sendDebugLog ( {
58- runId : this . runFriendlyId ,
59- message : `snapshot poll: started` ,
60- properties : {
61- snapshotId : this . snapshotFriendlyId ,
62- } ,
63- } ) ;
37+ this . sendDebugLog ( "polling for latest snapshot" ) ;
6438
6539 const response = await this . httpClient . getRunExecutionData ( this . runFriendlyId ) ;
6640
6741 if ( ! response . success ) {
68- this . logger . sendDebugLog ( {
69- runId : this . runFriendlyId ,
70- message : "Snapshot poll failed" ,
71- properties : {
72- error : response . error ,
73- } ,
74- } ) ;
75-
76- this . logger . sendDebugLog ( {
77- runId : this . runFriendlyId ,
78- message : `snapshot poll: failed` ,
79- properties : {
80- snapshotId : this . snapshotFriendlyId ,
81- error : response . error ,
82- } ,
83- } ) ;
42+ this . sendDebugLog ( "failed to get run execution data" , { error : response . error } ) ;
43+ return ;
44+ }
8445
46+ if ( ! this . enabled ) {
47+ this . sendDebugLog ( "poller disabled, skipping snapshot change handler" ) ;
8548 return ;
8649 }
8750
8851 await this . handleSnapshotChange ( response . data . execution ) ;
8952 } ,
90- intervalMs : this . snapshotPollIntervalMs ,
53+ intervalMs,
9154 leadingEdge : false ,
9255 onError : async ( error ) => {
93- this . logger . sendDebugLog ( {
94- runId : this . runFriendlyId ,
95- message : "Failed to poll for snapshot" ,
96- properties : { error : error instanceof Error ? error . message : String ( error ) } ,
56+ this . sendDebugLog ( "failed to poll for snapshot" , {
57+ error : error instanceof Error ? error . message : String ( error ) ,
9758 } ) ;
9859 } ,
9960 } ) ;
61+
62+ this . sendDebugLog ( "created" ) ;
63+ }
64+
65+ private sendDebugLog ( message : string , properties ?: SendDebugLogOptions [ "properties" ] ) {
66+ this . logger . sendDebugLog ( {
67+ runId : this . runFriendlyId ,
68+ message : `[poller] ${ message } ` ,
69+ properties : {
70+ ...properties ,
71+ runId : this . runFriendlyId ,
72+ snapshotId : this . snapshotFriendlyId ,
73+ pollIntervalMs : this . poller . intervalMs ,
74+ } ,
75+ } ) ;
10076 }
10177
10278 resetCurrentInterval ( ) {
@@ -112,10 +88,22 @@ export class RunExecutionSnapshotPoller {
11288 }
11389
11490 start ( ) {
91+ if ( this . enabled ) {
92+ this . sendDebugLog ( "already started" ) ;
93+ return ;
94+ }
95+
96+ this . enabled = true ;
11597 this . poller . start ( ) ;
11698 }
11799
118100 stop ( ) {
101+ if ( ! this . enabled ) {
102+ this . sendDebugLog ( "already stopped" ) ;
103+ return ;
104+ }
105+
106+ this . enabled = false ;
119107 this . poller . stop ( ) ;
120108 }
121109}
0 commit comments