From 4b2d9664627a3b8562ce9336f5d002421e64746f Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Mon, 28 Oct 2024 15:20:54 +0000 Subject: [PATCH] fix: shutdown api process when another worker exits unexpectedly --- server/src/main.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/src/main.ts b/server/src/main.ts index 11cc44ec10b92..90b7a7c6ca747 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,5 +1,5 @@ import { CommandFactory } from 'nest-commander'; -import { fork } from 'node:child_process'; +import { ChildProcess, fork } from 'node:child_process'; import { Worker } from 'node:worker_threads'; import { ImmichAdminModule } from 'src/app.module'; import { ImmichWorker, LogLevel } from 'src/enum'; @@ -10,12 +10,16 @@ if (immichApp) { process.argv.splice(2, 1); } +let apiProcess: ChildProcess | undefined; + function bootstrapWorker(name: ImmichWorker) { console.log(`Starting ${name} worker`); const execArgv = process.execArgv.map((arg) => (arg.startsWith('--inspect') ? '--inspect=0.0.0.0:9231' : arg)); const worker = - name === 'api' ? fork(`./dist/workers/${name}.js`, [], { execArgv }) : new Worker(`./dist/workers/${name}.js`); + name === ImmichWorker.API + ? (apiProcess = fork(`./dist/workers/${name}.js`, [], { execArgv })) + : new Worker(`./dist/workers/${name}.js`); worker.on('error', (error) => { console.error(`${name} worker error: ${error}`); @@ -24,6 +28,10 @@ function bootstrapWorker(name: ImmichWorker) { worker.on('exit', (exitCode) => { if (exitCode !== 0) { console.error(`${name} worker exited with code ${exitCode}`); + if (apiProcess && name !== ImmichWorker.API) { + console.error('Killing api process'); + apiProcess.kill('SIGTERM'); + } process.exit(exitCode); } });