Skip to content

Commit

Permalink
fix: shutdown api process when another worker exits unexpectedly
Browse files Browse the repository at this point in the history
  • Loading branch information
zackpollard committed Oct 29, 2024
1 parent 00dd941 commit 4b2d966
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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}`);
Expand All @@ -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);
}
});
Expand Down

0 comments on commit 4b2d966

Please sign in to comment.