1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-28 22:51:59 +00:00

fix: shutdown api process when another worker exits unexpectedly (#13802)

This commit is contained in:
Zack Pollard 2024-10-29 14:46:04 +00:00 committed by GitHub
parent 02819dc079
commit e74ddca6c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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);
}
});