@@ -25,6 +25,8 @@ import {
2525import pLimit from "p-limit" ;
2626import { resolveLocalEnvVars } from "../utilities/localEnvVars.js" ;
2727import type { Metafile } from "esbuild" ;
28+ import { TaskRunProcessPool } from "./taskRunProcessPool.js" ;
29+ import { tryCatch } from "@trigger.dev/core/utils" ;
2830
2931export type WorkerRuntimeOptions = {
3032 name : string | undefined ;
@@ -67,6 +69,7 @@ class DevSupervisor implements WorkerRuntime {
6769 private socketConnections = new Set < string > ( ) ;
6870
6971 private runLimiter ?: ReturnType < typeof pLimit > ;
72+ private taskRunProcessPool ?: TaskRunProcessPool ;
7073
7174 constructor ( public readonly options : WorkerRuntimeOptions ) { }
7275
@@ -95,6 +98,42 @@ class DevSupervisor implements WorkerRuntime {
9598
9699 this . runLimiter = pLimit ( maxConcurrentRuns ) ;
97100
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+
98137 this . socket = this . #createSocket( ) ;
99138
100139 //start an SSE connection for presence
@@ -111,6 +150,17 @@ class DevSupervisor implements WorkerRuntime {
111150 } catch ( error ) {
112151 logger . debug ( "[DevSupervisor] shutdown, socket failed to close" , { error } ) ;
113152 }
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+ }
114164 }
115165
116166 async initializeWorker (
@@ -293,12 +343,21 @@ class DevSupervisor implements WorkerRuntime {
293343 continue ;
294344 }
295345
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+
296354 //new run
297355 runController = new DevRunController ( {
298356 runFriendlyId : message . run . friendlyId ,
299357 worker : worker ,
300358 httpClient : this . options . client ,
301359 logLevel : this . options . args . logLevel ,
360+ taskRunProcessPool : this . taskRunProcessPool ,
302361 onFinished : ( ) => {
303362 logger . debug ( "[DevSupervisor] Run finished" , { runId : message . run . friendlyId } ) ;
304363
@@ -574,6 +633,10 @@ class DevSupervisor implements WorkerRuntime {
574633 return ;
575634 }
576635
636+ if ( worker . serverWorker ?. version ) {
637+ this . taskRunProcessPool ?. deprecateVersion ( worker . serverWorker ?. version ) ;
638+ }
639+
577640 if ( this . #workerHasInProgressRuns( friendlyId ) ) {
578641 return ;
579642 }
0 commit comments