|
| 1 | +import { |
| 2 | + MachinePresetResources, |
| 3 | + ServerBackgroundWorker, |
| 4 | + WorkerManifest, |
| 5 | +} from "@trigger.dev/core/v3"; |
| 6 | +import { TaskRunProcess } from "../executions/taskRunProcess.js"; |
| 7 | +import { logger } from "../utilities/logger.js"; |
| 8 | + |
| 9 | +export type TaskRunProcessPoolOptions = { |
| 10 | + env: Record<string, string>; |
| 11 | + cwd: string; |
| 12 | + enableProcessReuse: boolean; |
| 13 | + maxPoolSize?: number; |
| 14 | + maxExecutionsPerProcess?: number; |
| 15 | +}; |
| 16 | + |
| 17 | +export class TaskRunProcessPool { |
| 18 | + private availableProcesses: TaskRunProcess[] = []; |
| 19 | + private busyProcesses: Set<TaskRunProcess> = new Set(); |
| 20 | + private readonly options: TaskRunProcessPoolOptions; |
| 21 | + private readonly maxPoolSize: number; |
| 22 | + private readonly maxExecutionsPerProcess: number; |
| 23 | + |
| 24 | + constructor(options: TaskRunProcessPoolOptions) { |
| 25 | + this.options = options; |
| 26 | + this.maxPoolSize = options.maxPoolSize ?? 3; |
| 27 | + this.maxExecutionsPerProcess = options.maxExecutionsPerProcess ?? 50; |
| 28 | + } |
| 29 | + |
| 30 | + async getProcess( |
| 31 | + workerManifest: WorkerManifest, |
| 32 | + serverWorker: ServerBackgroundWorker, |
| 33 | + machineResources: MachinePresetResources, |
| 34 | + env?: Record<string, string> |
| 35 | + ): Promise<TaskRunProcess> { |
| 36 | + // Try to reuse an existing process if enabled |
| 37 | + if (this.options.enableProcessReuse) { |
| 38 | + const reusableProcess = this.findReusableProcess(); |
| 39 | + if (reusableProcess) { |
| 40 | + logger.debug("[TaskRunProcessPool] Reusing existing process", { |
| 41 | + availableCount: this.availableProcesses.length, |
| 42 | + busyCount: this.busyProcesses.size, |
| 43 | + }); |
| 44 | + |
| 45 | + this.availableProcesses = this.availableProcesses.filter((p) => p !== reusableProcess); |
| 46 | + this.busyProcesses.add(reusableProcess); |
| 47 | + return reusableProcess; |
| 48 | + } else { |
| 49 | + logger.debug("[TaskRunProcessPool] No reusable process found", { |
| 50 | + availableCount: this.availableProcesses.length, |
| 51 | + busyCount: this.busyProcesses.size, |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Create new process |
| 57 | + logger.debug("[TaskRunProcessPool] Creating new process", { |
| 58 | + availableCount: this.availableProcesses.length, |
| 59 | + busyCount: this.busyProcesses.size, |
| 60 | + }); |
| 61 | + |
| 62 | + const newProcess = new TaskRunProcess({ |
| 63 | + workerManifest, |
| 64 | + env: { |
| 65 | + ...this.options.env, |
| 66 | + ...env, |
| 67 | + }, |
| 68 | + serverWorker, |
| 69 | + machineResources, |
| 70 | + cwd: this.options.cwd, |
| 71 | + }).initialize(); |
| 72 | + |
| 73 | + this.busyProcesses.add(newProcess); |
| 74 | + return newProcess; |
| 75 | + } |
| 76 | + |
| 77 | + async returnProcess(process: TaskRunProcess): Promise<void> { |
| 78 | + this.busyProcesses.delete(process); |
| 79 | + |
| 80 | + if (this.shouldReuseProcess(process)) { |
| 81 | + logger.debug("[TaskRunProcessPool] Returning process to pool", { |
| 82 | + availableCount: this.availableProcesses.length, |
| 83 | + busyCount: this.busyProcesses.size, |
| 84 | + }); |
| 85 | + |
| 86 | + // Clean up but don't kill the process |
| 87 | + try { |
| 88 | + await process.cleanup(false); |
| 89 | + this.availableProcesses.push(process); |
| 90 | + } catch (error) { |
| 91 | + logger.debug("[TaskRunProcessPool] Failed to cleanup process for reuse, killing it", { |
| 92 | + error, |
| 93 | + }); |
| 94 | + await this.killProcess(process); |
| 95 | + } |
| 96 | + } else { |
| 97 | + logger.debug("[TaskRunProcessPool] Killing process", { |
| 98 | + availableCount: this.availableProcesses.length, |
| 99 | + busyCount: this.busyProcesses.size, |
| 100 | + }); |
| 101 | + await this.killProcess(process); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private findReusableProcess(): TaskRunProcess | undefined { |
| 106 | + return this.availableProcesses.find((process) => this.isProcessHealthy(process)); |
| 107 | + } |
| 108 | + |
| 109 | + private shouldReuseProcess(process: TaskRunProcess): boolean { |
| 110 | + const isHealthy = this.isProcessHealthy(process); |
| 111 | + const isBeingKilled = process.isBeingKilled; |
| 112 | + const pid = process.pid; |
| 113 | + |
| 114 | + logger.debug("[TaskRunProcessPool] Checking if process should be reused", { |
| 115 | + isHealthy, |
| 116 | + isBeingKilled, |
| 117 | + pid, |
| 118 | + availableCount: this.availableProcesses.length, |
| 119 | + busyCount: this.busyProcesses.size, |
| 120 | + maxPoolSize: this.maxPoolSize, |
| 121 | + }); |
| 122 | + |
| 123 | + return ( |
| 124 | + this.options.enableProcessReuse && |
| 125 | + this.isProcessHealthy(process) && |
| 126 | + this.availableProcesses.length < this.maxPoolSize |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + private isProcessHealthy(process: TaskRunProcess): boolean { |
| 131 | + // Basic health checks - we can expand this later |
| 132 | + return !process.isBeingKilled && process.pid !== undefined; |
| 133 | + } |
| 134 | + |
| 135 | + private async killProcess(process: TaskRunProcess): Promise<void> { |
| 136 | + try { |
| 137 | + await process.cleanup(true); |
| 138 | + } catch (error) { |
| 139 | + logger.debug("[TaskRunProcessPool] Error killing process", { error }); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + async shutdown(): Promise<void> { |
| 144 | + logger.debug("[TaskRunProcessPool] Shutting down pool", { |
| 145 | + availableCount: this.availableProcesses.length, |
| 146 | + busyCount: this.busyProcesses.size, |
| 147 | + }); |
| 148 | + |
| 149 | + // Kill all available processes |
| 150 | + await Promise.all(this.availableProcesses.map((process) => this.killProcess(process))); |
| 151 | + this.availableProcesses = []; |
| 152 | + |
| 153 | + // Kill all busy processes |
| 154 | + await Promise.all(Array.from(this.busyProcesses).map((process) => this.killProcess(process))); |
| 155 | + this.busyProcesses.clear(); |
| 156 | + } |
| 157 | + |
| 158 | + getStats() { |
| 159 | + return { |
| 160 | + availableCount: this.availableProcesses.length, |
| 161 | + busyCount: this.busyProcesses.size, |
| 162 | + totalCount: this.availableProcesses.length + this.busyProcesses.size, |
| 163 | + }; |
| 164 | + } |
| 165 | +} |
0 commit comments