diff --git a/server/src/main.ts b/server/src/main.ts index 11cc44ec10..90b7a7c6ca 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); } });