11import {
22 CompletedWaitpoint ,
33 ExecutorToWorkerMessageCatalog ,
4+ MachinePreset ,
45 ServerBackgroundWorker ,
56 TaskRunErrorCodes ,
67 TaskRunExecution ,
@@ -48,10 +49,15 @@ export type TaskRunProcessOptions = {
4849 workerManifest : WorkerManifest ;
4950 serverWorker : ServerBackgroundWorker ;
5051 env : Record < string , string > ;
52+ machine : MachinePreset ;
53+ isWarmStart ?: boolean ;
54+ cwd ?: string ;
55+ } ;
56+
57+ export type TaskRunProcessExecuteParams = {
5158 payload : TaskRunExecutionPayload ;
5259 messageId : string ;
53-
54- cwd ?: string ;
60+ env ?: Record < string , string > ;
5561} ;
5662
5763export class TaskRunProcess {
@@ -79,9 +85,18 @@ export class TaskRunProcess {
7985 public onWaitForBatch : Evt < OnWaitForBatchMessage > = new Evt ( ) ;
8086 public onWait : Evt < OnWaitMessage > = new Evt ( ) ;
8187
82- constructor ( public readonly options : TaskRunProcessOptions ) { }
88+ private _isPreparedForNextRun : boolean = false ;
89+
90+ constructor ( public readonly options : TaskRunProcessOptions ) {
91+ this . _isPreparedForNextRun = true ;
92+ }
93+
94+ get isPreparedForNextRun ( ) {
95+ return this . _isPreparedForNextRun ;
96+ }
8397
8498 async cancel ( ) {
99+ this . _isPreparedForNextRun = false ;
85100 this . _isBeingCancelled = true ;
86101
87102 try {
@@ -94,6 +109,8 @@ export class TaskRunProcess {
94109 }
95110
96111 async cleanup ( kill = true ) {
112+ this . _isPreparedForNextRun = false ;
113+
97114 try {
98115 await this . #flush( ) ;
99116 } catch ( err ) {
@@ -105,25 +122,12 @@ export class TaskRunProcess {
105122 }
106123 }
107124
108- get runId ( ) {
109- return this . options . payload . execution . run . id ;
110- }
125+ initialize ( ) {
126+ const { env : $env , workerManifest, cwd, machine } = this . options ;
111127
112- get isTest ( ) {
113- return this . options . payload . execution . run . isTest ;
114- }
115-
116- get payload ( ) : TaskRunExecutionPayload {
117- return this . options . payload ;
118- }
119-
120- async initialize ( ) {
121- const { env : $env , workerManifest, cwd, messageId, payload } = this . options ;
122-
123- const maxOldSpaceSize = nodeOptionsWithMaxOldSpaceSize ( undefined , payload . execution . machine ) ;
128+ const maxOldSpaceSize = nodeOptionsWithMaxOldSpaceSize ( undefined , machine ) ;
124129
125130 const fullEnv = {
126- ...( this . isTest ? { TRIGGER_LOG_LEVEL : "debug" } : { } ) ,
127131 ...$env ,
128132 OTEL_IMPORT_HOOK_INCLUDES : workerManifest . otelImportHook ?. include ?. join ( "," ) ,
129133 // TODO: this will probably need to use something different for bun (maybe --preload?)
@@ -132,7 +136,7 @@ export class TaskRunProcess {
132136 TRIGGER_PROCESS_FORK_START_TIME : String ( Date . now ( ) ) ,
133137 } ;
134138
135- logger . debug ( `[ ${ this . runId } ] initializing task run process` , {
139+ logger . debug ( `initializing task run process` , {
136140 env : fullEnv ,
137141 path : workerManifest . workerEntryPoint ,
138142 cwd,
@@ -175,13 +179,13 @@ export class TaskRunProcess {
175179
176180 resolver ( result ) ;
177181 } ,
178- READY_TO_DISPOSE : async ( message ) => {
179- logger . debug ( `[ ${ this . runId } ] task run process is ready to dispose` ) ;
182+ READY_TO_DISPOSE : async ( ) => {
183+ logger . debug ( `task run process is ready to dispose` ) ;
180184
181185 this . onReadyToDispose . post ( this ) ;
182186 } ,
183187 TASK_HEARTBEAT : async ( message ) => {
184- this . onTaskRunHeartbeat . post ( messageId ) ;
188+ this . onTaskRunHeartbeat . post ( message . id ) ;
185189 } ,
186190 WAIT_FOR_TASK : async ( message ) => {
187191 this . onWaitForTask . post ( message ) ;
@@ -190,14 +194,16 @@ export class TaskRunProcess {
190194 this . onWaitForBatch . post ( message ) ;
191195 } ,
192196 UNCAUGHT_EXCEPTION : async ( message ) => {
193- logger . debug ( `[ ${ this . runId } ] uncaught exception in task run process` , { ...message } ) ;
197+ logger . debug ( " uncaught exception in task run process" , { ...message } ) ;
194198 } ,
195199 } ,
196200 } ) ;
197201
198202 this . _child . on ( "exit" , this . #handleExit. bind ( this ) ) ;
199203 this . _child . stdout ?. on ( "data" , this . #handleLog. bind ( this ) ) ;
200204 this . _child . stderr ?. on ( "data" , this . #handleStdErr. bind ( this ) ) ;
205+
206+ return this ;
201207 }
202208
203209 async #flush( timeoutInMs : number = 5_000 ) {
@@ -206,7 +212,9 @@ export class TaskRunProcess {
206212 await this . _ipc ?. sendWithAck ( "FLUSH" , { timeoutInMs } , timeoutInMs + 1_000 ) ;
207213 }
208214
209- async execute ( ) : Promise < TaskRunExecutionResult > {
215+ async execute ( params : TaskRunProcessExecuteParams ) : Promise < TaskRunExecutionResult > {
216+ this . _isPreparedForNextRun = false ;
217+
210218 let resolver : ( value : TaskRunExecutionResult ) => void ;
211219 let rejecter : ( err ?: any ) => void ;
212220
@@ -215,19 +223,19 @@ export class TaskRunProcess {
215223 rejecter = reject ;
216224 } ) ;
217225
218- this . _attemptStatuses . set ( this . payload . execution . attempt . id , "PENDING" ) ;
226+ this . _attemptStatuses . set ( params . payload . execution . attempt . id , "PENDING" ) ;
219227
220228 // @ts -expect-error - We know that the resolver and rejecter are defined
221- this . _attemptPromises . set ( this . payload . execution . attempt . id , { resolver, rejecter } ) ;
229+ this . _attemptPromises . set ( params . payload . execution . attempt . id , { resolver, rejecter } ) ;
222230
223- const { execution, traceContext, metrics } = this . payload ;
231+ const { execution, traceContext, metrics } = params . payload ;
224232
225233 this . _currentExecution = execution ;
226234
227235 if ( this . _child ?. connected && ! this . _isBeingKilled && ! this . _child . killed ) {
228236 logger . debug (
229237 `[${ new Date ( ) . toISOString ( ) } ][${
230- this . runId
238+ params . payload . execution . run . id
231239 } ] sending EXECUTE_TASK_RUN message to task run process`,
232240 {
233241 pid : this . pid ,
@@ -239,6 +247,8 @@ export class TaskRunProcess {
239247 traceContext,
240248 metadata : this . options . serverWorker ,
241249 metrics,
250+ env : params . env ,
251+ isWarmStart : this . options . isWarmStart ,
242252 } ) ;
243253 }
244254
@@ -398,7 +408,7 @@ export class TaskRunProcess {
398408 }
399409
400410 async kill ( signal ?: number | NodeJS . Signals , timeoutInMs ?: number ) {
401- logger . debug ( `[ ${ this . runId } ] killing task run process` , {
411+ logger . debug ( `killing task run process` , {
402412 signal,
403413 timeoutInMs,
404414 pid : this . pid ,
@@ -417,6 +427,16 @@ export class TaskRunProcess {
417427 }
418428 }
419429
430+ forceExit ( ) {
431+ try {
432+ this . _isBeingKilled = true ;
433+
434+ this . _child ?. kill ( "SIGKILL" ) ;
435+ } catch ( error ) {
436+ logger . debug ( "forceExit: failed to kill child process" , { error } ) ;
437+ }
438+ }
439+
420440 get isBeingKilled ( ) {
421441 return this . _isBeingKilled || this . _child ?. killed ;
422442 }
0 commit comments