mirror of
https://github.com/immich-app/immich.git
synced 2024-11-21 18:16:55 +02:00
fix: shutdown api process when another worker exits unexpectedly (#13802)
This commit is contained in:
parent
02819dc079
commit
e74ddca6c0
@ -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);
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user