2025-11-17 17:15:44 +00:00
|
|
|
import { Kysely } from 'kysely';
|
2024-03-21 09:08:29 -05:00
|
|
|
import { CommandFactory } from 'nest-commander';
|
2024-10-29 14:46:04 +00:00
|
|
|
import { ChildProcess, fork } from 'node:child_process';
|
2025-07-18 10:57:49 -04:00
|
|
|
import { dirname, join } from 'node:path';
|
2024-05-14 15:28:20 +01:00
|
|
|
import { Worker } from 'node:worker_threads';
|
2025-11-17 17:15:44 +00:00
|
|
|
import { PostgresError } from 'postgres';
|
2024-05-17 14:44:30 +01:00
|
|
|
import { ImmichAdminModule } from 'src/app.module';
|
2025-11-17 17:15:44 +00:00
|
|
|
import { ExitCode, ImmichWorker, LogLevel, SystemMetadataKey } from 'src/enum';
|
2024-10-03 15:28:36 -04:00
|
|
|
import { ConfigRepository } from 'src/repositories/config.repository';
|
2025-11-17 17:15:44 +00:00
|
|
|
import { SystemMetadataRepository } from 'src/repositories/system-metadata.repository';
|
|
|
|
|
import { type DB } from 'src/schema';
|
|
|
|
|
import { getKyselyConfig } from 'src/utils/database';
|
2023-06-08 11:01:07 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
/**
|
|
|
|
|
* Manages worker lifecycle
|
|
|
|
|
*/
|
|
|
|
|
class Workers {
|
|
|
|
|
/**
|
|
|
|
|
* Currently running workers
|
|
|
|
|
*/
|
|
|
|
|
workers: Partial<Record<ImmichWorker, { kill: (signal: NodeJS.Signals) => Promise<void> | void }>> = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fail-safe in case anything dies during restart
|
|
|
|
|
*/
|
|
|
|
|
restarting = false;
|
2023-06-08 11:01:07 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
/**
|
|
|
|
|
* Boot all enabled workers
|
|
|
|
|
*/
|
|
|
|
|
async bootstrap() {
|
|
|
|
|
const isMaintenanceMode = await this.isMaintenanceMode();
|
|
|
|
|
const { workers } = new ConfigRepository().getEnv();
|
2024-10-29 14:46:04 +00:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
if (isMaintenanceMode) {
|
|
|
|
|
this.startWorker(ImmichWorker.Maintenance);
|
|
|
|
|
} else {
|
|
|
|
|
for (const worker of workers) {
|
|
|
|
|
this.startWorker(worker);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-29 11:47:33 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise a short-lived Nest application to build configuration
|
|
|
|
|
* @returns System configuration
|
|
|
|
|
*/
|
|
|
|
|
private async isMaintenanceMode(): Promise<boolean> {
|
|
|
|
|
const { database } = new ConfigRepository().getEnv();
|
|
|
|
|
const kysely = new Kysely<DB>(getKyselyConfig(database.config));
|
|
|
|
|
const systemMetadataRepository = new SystemMetadataRepository(kysely);
|
2024-10-29 11:47:33 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
try {
|
|
|
|
|
const value = await systemMetadataRepository.get(SystemMetadataKey.MaintenanceMode);
|
|
|
|
|
return value?.isMaintenanceMode || false;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// Table doesn't exist (migrations haven't run yet)
|
|
|
|
|
if (error instanceof PostgresError && error.code === '42P01') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await kysely.destroy();
|
2024-10-29 11:47:33 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
/**
|
|
|
|
|
* Start an individual worker
|
|
|
|
|
* @param name Worker
|
|
|
|
|
*/
|
|
|
|
|
private startWorker(name: ImmichWorker) {
|
|
|
|
|
console.log(`Starting ${name} worker`);
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line unicorn/prefer-module
|
|
|
|
|
const basePath = dirname(__filename);
|
|
|
|
|
const workerFile = join(basePath, 'workers', `${name}.js`);
|
|
|
|
|
|
|
|
|
|
let anyWorker: Worker | ChildProcess;
|
|
|
|
|
let kill: (signal?: NodeJS.Signals) => Promise<void> | void;
|
2024-10-29 11:47:33 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
if (name === ImmichWorker.Api) {
|
|
|
|
|
const worker = fork(workerFile, [], {
|
|
|
|
|
execArgv: process.execArgv.map((arg) => (arg.startsWith('--inspect') ? '--inspect=0.0.0.0:9231' : arg)),
|
|
|
|
|
});
|
2024-09-07 13:21:25 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
kill = (signal) => void worker.kill(signal);
|
|
|
|
|
anyWorker = worker;
|
|
|
|
|
} else {
|
|
|
|
|
const worker = new Worker(workerFile);
|
2025-07-18 10:57:49 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
kill = async () => void (await worker.terminate());
|
|
|
|
|
anyWorker = worker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
anyWorker.on('error', (error) => this.onError(name, error));
|
|
|
|
|
anyWorker.on('exit', (exitCode) => this.onExit(name, exitCode));
|
|
|
|
|
|
|
|
|
|
this.workers[name] = { kill };
|
2024-10-29 11:47:33 -04:00
|
|
|
}
|
2024-09-07 13:21:25 -04:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
onError(name: ImmichWorker, error: Error) {
|
|
|
|
|
console.error(`${name} worker error: ${error}, stack: ${error.stack}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onExit(name: ImmichWorker, exitCode: number | null) {
|
|
|
|
|
// restart immich server
|
|
|
|
|
if (exitCode === ExitCode.AppRestart || this.restarting) {
|
|
|
|
|
this.restarting = true;
|
|
|
|
|
|
|
|
|
|
console.info(`${name} worker shutdown for restart`);
|
|
|
|
|
delete this.workers[name];
|
|
|
|
|
|
|
|
|
|
// once all workers shut down, bootstrap again
|
|
|
|
|
if (Object.keys(this.workers).length === 0) {
|
|
|
|
|
void this.bootstrap();
|
|
|
|
|
this.restarting = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// shutdown the entire process
|
|
|
|
|
delete this.workers[name];
|
|
|
|
|
|
|
|
|
|
if (exitCode !== 0) {
|
|
|
|
|
console.error(`${name} worker exited with code ${exitCode}`);
|
|
|
|
|
|
|
|
|
|
if (this.workers[ImmichWorker.Api] && name !== ImmichWorker.Api) {
|
|
|
|
|
console.error('Killing api process');
|
|
|
|
|
void this.workers[ImmichWorker.Api].kill('SIGTERM');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.exit(exitCode);
|
|
|
|
|
}
|
2024-05-14 15:28:20 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
function main() {
|
|
|
|
|
const immichApp = process.argv[2];
|
|
|
|
|
if (immichApp) {
|
|
|
|
|
process.argv.splice(2, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 15:28:36 -04:00
|
|
|
if (immichApp === 'immich-admin') {
|
|
|
|
|
process.title = 'immich_admin_cli';
|
2025-07-15 14:50:13 -04:00
|
|
|
process.env.IMMICH_LOG_LEVEL = LogLevel.Warn;
|
2025-11-17 17:15:44 +00:00
|
|
|
|
2024-10-03 15:28:36 -04:00
|
|
|
return CommandFactory.run(ImmichAdminModule);
|
2023-06-08 11:01:07 -04:00
|
|
|
}
|
2024-10-03 15:28:36 -04: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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 15:10:57 +01:00
|
|
|
process.title = 'immich';
|
2025-11-17 17:15:44 +00:00
|
|
|
void new Workers().bootstrap();
|
2023-06-08 11:01:07 -04:00
|
|
|
}
|
2024-03-21 09:08:29 -05:00
|
|
|
|
2025-11-17 17:15:44 +00:00
|
|
|
void main();
|