@@ -18,7 +18,7 @@ type ManagedRunControllerOptions = {
1818 env : EnvObject ;
1919} ;
2020
21- type SupervisorSocket = Socket < WorkloadServerToClientEvents , WorkloadClientToServerEvents > ;
21+ export type SupervisorSocket = Socket < WorkloadServerToClientEvents , WorkloadClientToServerEvents > ;
2222
2323export class ManagedRunController {
2424 private readonly env : RunnerEnv ;
@@ -31,6 +31,9 @@ export class ManagedRunController {
3131 private warmStartCount = 0 ;
3232 private restoreCount = 0 ;
3333
34+ private notificationCount = 0 ;
35+ private lastNotificationAt : Date | null = null ;
36+
3437 private currentExecution : RunExecution | null = null ;
3538
3639 constructor ( opts : ManagedRunControllerOptions ) {
@@ -91,6 +94,8 @@ export class ManagedRunController {
9194 return {
9295 warmStartCount : this . warmStartCount ,
9396 restoreCount : this . restoreCount ,
97+ notificationCount : this . notificationCount ,
98+ lastNotificationAt : this . lastNotificationAt ,
9499 } ;
95100 }
96101
@@ -188,12 +193,16 @@ export class ManagedRunController {
188193 this . currentExecution = null ;
189194 }
190195
196+ // Remove all run notification listeners just to be safe
197+ this . socket . removeAllListeners ( "run:notify" ) ;
198+
191199 if ( ! this . currentExecution || ! this . currentExecution . canExecute ) {
192200 this . currentExecution = new RunExecution ( {
193201 workerManifest : this . workerManifest ,
194202 env : this . env ,
195203 httpClient : this . httpClient ,
196204 logger : this . logger ,
205+ supervisorSocket : this . socket ,
197206 } ) ;
198207 }
199208
@@ -224,8 +233,8 @@ export class ManagedRunController {
224233
225234 const metrics = this . currentExecution ?. metrics ;
226235
227- if ( metrics ?. restoreCount ) {
228- this . restoreCount += metrics . restoreCount ;
236+ if ( metrics ?. execution ?. restoreCount ) {
237+ this . restoreCount += metrics . execution . restoreCount ;
229238 }
230239
231240 this . lockedRunExecution = null ;
@@ -288,6 +297,7 @@ export class ManagedRunController {
288297 env : this . env ,
289298 httpClient : this . httpClient ,
290299 logger : this . logger ,
300+ supervisorSocket : this . socket ,
291301 } ) . prepareForExecution ( {
292302 taskRunEnv : previousTaskRunEnv ,
293303 } ) ;
@@ -392,116 +402,12 @@ export class ManagedRunController {
392402 createSupervisorSocket ( ) : SupervisorSocket {
393403 const wsUrl = new URL ( "/workload" , this . workerApiUrl ) ;
394404
395- const socket = io ( wsUrl . href , {
405+ const socket : SupervisorSocket = io ( wsUrl . href , {
396406 transports : [ "websocket" ] ,
397407 extraHeaders : {
398408 [ WORKLOAD_HEADERS . DEPLOYMENT_ID ] : this . env . TRIGGER_DEPLOYMENT_ID ,
399409 [ WORKLOAD_HEADERS . RUNNER_ID ] : this . env . TRIGGER_RUNNER_ID ,
400410 } ,
401- } ) satisfies SupervisorSocket ;
402-
403- socket . on ( "run:notify" , async ( { version, run } ) => {
404- // Generate a unique ID for the notification
405- const notificationId = Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ;
406-
407- // Use this to track the notification incl. any processing
408- const notification = {
409- id : notificationId ,
410- runId : run . friendlyId ,
411- version,
412- } ;
413-
414- // Lock this to the current run and snapshot IDs
415- const controller = {
416- runFriendlyId : this . runFriendlyId ,
417- snapshotFriendlyId : this . snapshotFriendlyId ,
418- } ;
419-
420- this . sendDebugLog ( {
421- runId : run . friendlyId ,
422- message : "run:notify received by runner" ,
423- properties : {
424- notification,
425- controller,
426- } ,
427- } ) ;
428-
429- if ( ! controller . runFriendlyId ) {
430- this . sendDebugLog ( {
431- runId : run . friendlyId ,
432- message : "run:notify: ignoring notification, no run ID" ,
433- properties : {
434- notification,
435- controller,
436- } ,
437- } ) ;
438- return ;
439- }
440-
441- if ( ! controller . snapshotFriendlyId ) {
442- this . sendDebugLog ( {
443- runId : run . friendlyId ,
444- message : "run:notify: ignoring notification, no snapshot ID" ,
445- properties : { notification, controller } ,
446- } ) ;
447- }
448-
449- if ( run . friendlyId !== controller . runFriendlyId ) {
450- this . sendDebugLog ( {
451- runId : run . friendlyId ,
452- message : "run:notify: ignoring notification for different run" ,
453- properties : {
454- notification,
455- controller,
456- } ,
457- } ) ;
458- return ;
459- }
460-
461- const latestSnapshot = await this . httpClient . getRunExecutionData ( controller . runFriendlyId ) ;
462-
463- if ( ! latestSnapshot . success ) {
464- this . sendDebugLog ( {
465- runId : run . friendlyId ,
466- message : "run:notify: failed to get latest snapshot data" ,
467- properties : {
468- notification,
469- controller,
470- error : latestSnapshot . error ,
471- } ,
472- } ) ;
473- return ;
474- }
475-
476- const runExecutionData = latestSnapshot . data . execution ;
477-
478- if ( ! this . currentExecution ) {
479- this . sendDebugLog ( {
480- runId : run . friendlyId ,
481- message : "run:notify: no current execution" ,
482- properties : {
483- notification,
484- controller,
485- } ,
486- } ) ;
487- return ;
488- }
489-
490- const [ error ] = await tryCatch (
491- this . currentExecution . enqueueSnapshotChangeAndWait ( runExecutionData )
492- ) ;
493-
494- if ( error ) {
495- this . sendDebugLog ( {
496- runId : run . friendlyId ,
497- message : "run:notify: unexpected error" ,
498- properties : {
499- notification,
500- controller,
501- error : error . message ,
502- } ,
503- } ) ;
504- }
505411 } ) ;
506412
507413 socket . on ( "connect" , ( ) => {
0 commit comments