@@ -25,6 +25,8 @@ import {
25
25
import pLimit from "p-limit" ;
26
26
import { resolveLocalEnvVars } from "../utilities/localEnvVars.js" ;
27
27
import type { Metafile } from "esbuild" ;
28
+ import { TaskRunProcessPool } from "./taskRunProcessPool.js" ;
29
+ import { tryCatch } from "@trigger.dev/core/utils" ;
28
30
29
31
export type WorkerRuntimeOptions = {
30
32
name : string | undefined ;
@@ -67,6 +69,7 @@ class DevSupervisor implements WorkerRuntime {
67
69
private socketConnections = new Set < string > ( ) ;
68
70
69
71
private runLimiter ?: ReturnType < typeof pLimit > ;
72
+ private taskRunProcessPool ?: TaskRunProcessPool ;
70
73
71
74
constructor ( public readonly options : WorkerRuntimeOptions ) { }
72
75
@@ -95,6 +98,42 @@ class DevSupervisor implements WorkerRuntime {
95
98
96
99
this . runLimiter = pLimit ( maxConcurrentRuns ) ;
97
100
101
+ // Initialize the task run process pool
102
+ const env = await this . #getEnvVars( ) ;
103
+
104
+ const enableProcessReuse =
105
+ typeof this . options . config . experimental_processKeepAlive === "boolean"
106
+ ? this . options . config . experimental_processKeepAlive
107
+ : typeof this . options . config . experimental_processKeepAlive === "object"
108
+ ? this . options . config . experimental_processKeepAlive . enabled
109
+ : false ;
110
+
111
+ const maxPoolSize =
112
+ typeof this . options . config . experimental_processKeepAlive === "object"
113
+ ? this . options . config . experimental_processKeepAlive . devMaxPoolSize ?? 25
114
+ : 25 ;
115
+
116
+ const maxExecutionsPerProcess =
117
+ typeof this . options . config . experimental_processKeepAlive === "object"
118
+ ? this . options . config . experimental_processKeepAlive . maxExecutionsPerProcess ?? 50
119
+ : 50 ;
120
+
121
+ if ( enableProcessReuse ) {
122
+ logger . debug ( "[DevSupervisor] Enabling process reuse" , {
123
+ enableProcessReuse,
124
+ maxPoolSize,
125
+ maxExecutionsPerProcess,
126
+ } ) ;
127
+ }
128
+
129
+ this . taskRunProcessPool = new TaskRunProcessPool ( {
130
+ env,
131
+ cwd : this . options . config . workingDir ,
132
+ enableProcessReuse,
133
+ maxPoolSize,
134
+ maxExecutionsPerProcess,
135
+ } ) ;
136
+
98
137
this . socket = this . #createSocket( ) ;
99
138
100
139
//start an SSE connection for presence
@@ -111,6 +150,17 @@ class DevSupervisor implements WorkerRuntime {
111
150
} catch ( error ) {
112
151
logger . debug ( "[DevSupervisor] shutdown, socket failed to close" , { error } ) ;
113
152
}
153
+
154
+ // Shutdown the task run process pool
155
+ if ( this . taskRunProcessPool ) {
156
+ const [ shutdownError ] = await tryCatch ( this . taskRunProcessPool . shutdown ( ) ) ;
157
+
158
+ if ( shutdownError ) {
159
+ logger . debug ( "[DevSupervisor] shutdown, task run process pool failed to shutdown" , {
160
+ error : shutdownError ,
161
+ } ) ;
162
+ }
163
+ }
114
164
}
115
165
116
166
async initializeWorker (
@@ -293,12 +343,21 @@ class DevSupervisor implements WorkerRuntime {
293
343
continue ;
294
344
}
295
345
346
+ if ( ! this . taskRunProcessPool ) {
347
+ logger . debug ( `[DevSupervisor] dequeueRuns. No task run process pool` , {
348
+ run : message . run . friendlyId ,
349
+ worker,
350
+ } ) ;
351
+ continue ;
352
+ }
353
+
296
354
//new run
297
355
runController = new DevRunController ( {
298
356
runFriendlyId : message . run . friendlyId ,
299
357
worker : worker ,
300
358
httpClient : this . options . client ,
301
359
logLevel : this . options . args . logLevel ,
360
+ taskRunProcessPool : this . taskRunProcessPool ,
302
361
onFinished : ( ) => {
303
362
logger . debug ( "[DevSupervisor] Run finished" , { runId : message . run . friendlyId } ) ;
304
363
@@ -574,6 +633,10 @@ class DevSupervisor implements WorkerRuntime {
574
633
return ;
575
634
}
576
635
636
+ if ( worker . serverWorker ?. version ) {
637
+ this . taskRunProcessPool ?. deprecateVersion ( worker . serverWorker ?. version ) ;
638
+ }
639
+
577
640
if ( this . #workerHasInProgressRuns( friendlyId ) ) {
578
641
return ;
579
642
}
0 commit comments