Skip to content

Commit

Permalink
refactor(server): worker cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 committed Oct 29, 2024
1 parent e74ddca commit c1c0d6c
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,39 @@ if (immichApp) {

let apiProcess: ChildProcess | undefined;

const onError = (error: Error) => console.error(`${name} worker error: ${error}`);

const onExit = (exitCode: number) => {
if (exitCode !== 0) {
console.error(`${name} worker exited with code ${exitCode}`);
process.exit(exitCode);
}
};

const onWorkerExit = (exitCode: number) => {
if (exitCode !== 0 && apiProcess) {
console.error('Killing api process');
apiProcess.kill('SIGTERM');
apiProcess = undefined;
}
onExit(exitCode);
};

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 === 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}`);
});
if (name === ImmichWorker.API) {
apiProcess = fork(`./dist/workers/${name}.js`, [], {
execArgv: process.execArgv.map((arg) => (arg.startsWith('--inspect') ? '--inspect=0.0.0.0:9231' : arg)),
});
apiProcess.on('error', onError);
apiProcess.on('exit', onExit);
return;
}

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);
}
});
const worker = new Worker(`./dist/workers/${name}.js`);
worker.on('error', onError);
worker.on('exit', onWorkerExit);
}

function bootstrap() {
Expand Down

0 comments on commit c1c0d6c

Please sign in to comment.