Skip to content

Commit 0e2251c

Browse files
jrasm91yosit
authored andcommitted
refactor(server): worker cleanup (immich-app#13805)
1 parent 2d42a8b commit 0e2251c

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

server/src/main.ts

+29-19
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,39 @@ if (immichApp) {
1212

1313
let apiProcess: ChildProcess | undefined;
1414

15+
const onError = (name: string, error: Error) => {
16+
console.error(`${name} worker error: ${error}`);
17+
};
18+
19+
const onExit = (name: string, exitCode: number | null) => {
20+
if (exitCode !== 0) {
21+
console.error(`${name} worker exited with code ${exitCode}`);
22+
23+
if (apiProcess && name !== ImmichWorker.API) {
24+
console.error('Killing api process');
25+
apiProcess.kill('SIGTERM');
26+
apiProcess = undefined;
27+
}
28+
}
29+
30+
process.exit(exitCode);
31+
};
32+
1533
function bootstrapWorker(name: ImmichWorker) {
1634
console.log(`Starting ${name} worker`);
1735

18-
const execArgv = process.execArgv.map((arg) => (arg.startsWith('--inspect') ? '--inspect=0.0.0.0:9231' : arg));
19-
const worker =
20-
name === ImmichWorker.API
21-
? (apiProcess = fork(`./dist/workers/${name}.js`, [], { execArgv }))
22-
: new Worker(`./dist/workers/${name}.js`);
23-
24-
worker.on('error', (error) => {
25-
console.error(`${name} worker error: ${error}`);
26-
});
36+
let worker: Worker | ChildProcess;
37+
if (name === ImmichWorker.API) {
38+
worker = fork(`./dist/workers/${name}.js`, [], {
39+
execArgv: process.execArgv.map((arg) => (arg.startsWith('--inspect') ? '--inspect=0.0.0.0:9231' : arg)),
40+
});
41+
apiProcess = worker;
42+
} else {
43+
worker = new Worker(`./dist/workers/${name}.js`);
44+
}
2745

28-
worker.on('exit', (exitCode) => {
29-
if (exitCode !== 0) {
30-
console.error(`${name} worker exited with code ${exitCode}`);
31-
if (apiProcess && name !== ImmichWorker.API) {
32-
console.error('Killing api process');
33-
apiProcess.kill('SIGTERM');
34-
}
35-
process.exit(exitCode);
36-
}
37-
});
46+
worker.on('error', (error) => onError(name, error));
47+
worker.on('exit', (exitCode) => onExit(name, exitCode));
3848
}
3949

4050
function bootstrap() {

0 commit comments

Comments
 (0)