1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-22 19:52:46 +01:00
immich/server/src/main.ts

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-03-21 15:08:29 +01:00
import { CommandFactory } from 'nest-commander';
import { ChildProcess, fork } from 'node:child_process';
import { Worker } from 'node:worker_threads';
import { ImmichAdminModule } from 'src/app.module';
2024-10-03 21:28:36 +02:00
import { ImmichWorker, LogLevel } from 'src/enum';
import { ConfigRepository } from 'src/repositories/config.repository';
2024-10-03 21:28:36 +02:00
const immichApp = process.argv[2];
if (immichApp) {
process.argv.splice(2, 1);
}
let apiProcess: ChildProcess | undefined;
2024-10-03 21:28:36 +02:00
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}`);
});
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);
}
});
}
function bootstrap() {
2024-10-03 21:28:36 +02:00
if (immichApp === 'immich-admin') {
process.title = 'immich_admin_cli';
process.env.IMMICH_LOG_LEVEL = LogLevel.WARN;
return CommandFactory.run(ImmichAdminModule);
}
2024-10-03 21:28:36 +02:00
if (immichApp === 'immich' || immichApp === 'microservices') {
console.error(
`Using "start.sh ${immichApp}" has been deprecated. See https://github.com/immich-app/immich/releases/tag/v1.118.0 for more information.`,
);
process.exit(1);
}
if (immichApp) {
console.error(`Unknown command: "${immichApp}"`);
process.exit(1);
}
process.title = 'immich';
2024-10-03 21:28:36 +02:00
const { workers } = new ConfigRepository().getEnv();
for (const worker of workers) {
bootstrapWorker(worker);
}
}
2024-03-21 15:08:29 +01:00
void bootstrap();