mirror of
https://github.com/immich-app/immich.git
synced 2024-11-24 08:52:28 +02:00
refactor(server): worker cleanup (#13805)
This commit is contained in:
parent
143ee0bc34
commit
4b9e460db5
@ -12,29 +12,39 @@ if (immichApp) {
|
||||
|
||||
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 === ImmichWorker.API
|
||||
? (apiProcess = fork(`./dist/workers/${name}.js`, [], { execArgv }))
|
||||
: new Worker(`./dist/workers/${name}.js`);
|
||||
|
||||
worker.on('error', (error) => {
|
||||
const onError = (name: string, error: Error) => {
|
||||
console.error(`${name} worker error: ${error}`);
|
||||
});
|
||||
};
|
||||
|
||||
worker.on('exit', (exitCode) => {
|
||||
const onExit = (name: string, exitCode: number | null) => {
|
||||
if (exitCode !== 0) {
|
||||
console.error(`${name} worker exited with code ${exitCode}`);
|
||||
|
||||
if (apiProcess && name !== ImmichWorker.API) {
|
||||
console.error('Killing api process');
|
||||
apiProcess.kill('SIGTERM');
|
||||
apiProcess = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
process.exit(exitCode);
|
||||
}
|
||||
};
|
||||
|
||||
function bootstrapWorker(name: ImmichWorker) {
|
||||
console.log(`Starting ${name} worker`);
|
||||
|
||||
let worker: Worker | ChildProcess;
|
||||
if (name === ImmichWorker.API) {
|
||||
worker = fork(`./dist/workers/${name}.js`, [], {
|
||||
execArgv: process.execArgv.map((arg) => (arg.startsWith('--inspect') ? '--inspect=0.0.0.0:9231' : arg)),
|
||||
});
|
||||
apiProcess = worker;
|
||||
} else {
|
||||
worker = new Worker(`./dist/workers/${name}.js`);
|
||||
}
|
||||
|
||||
worker.on('error', (error) => onError(name, error));
|
||||
worker.on('exit', (exitCode) => onExit(name, exitCode));
|
||||
}
|
||||
|
||||
function bootstrap() {
|
||||
|
Loading…
Reference in New Issue
Block a user