Skip to content

High performance self-hosted photo and video management solution.

refactor(server): bootstrap code (#2682)

Jason Rasmussend08535e7f625
diff --git a/server/apps/immich/src/app.service.ts b/server/apps/immich/src/app.service.ts
index c366d88512..aee680b28d 100644
--- a/server/apps/immich/src/app.service.ts
+++ b/server/apps/immich/src/app.service.ts
@@ -1,9 +1,11 @@
-import { JobService, SearchService, StorageService } from '@app/domain';
-import { Injectable } from '@nestjs/common';
+import { JobService, MACHINE_LEARNING_ENABLED, SearchService, StorageService } from '@app/domain';
+import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
@Injectable()
export class AppService {
+ private logger = new Logger(AppService.name);
+
constructor(
private jobService: JobService,
private searchService: SearchService,
@@ -18,5 +20,8 @@ export class AppService {
async init() {
this.storageService.init();
await this.searchService.init();
+
+ this.logger.log(`Machine learning is ${MACHINE_LEARNING_ENABLED ? 'enabled' : 'disabled'}`);
+ this.logger.log(`Search is ${this.searchService.isEnabled() ? 'enabled' : 'disabled'}`);
}
}
diff --git a/server/apps/immich/src/main.ts b/server/apps/immich/src/main.ts
index 5c1779916b..015e51b766 100644
--- a/server/apps/immich/src/main.ts
+++ b/server/apps/immich/src/main.ts
@@ -1,4 +1,4 @@
-import { getLogLevels, MACHINE_LEARNING_ENABLED, SearchService, SERVER_VERSION } from '@app/domain';
+import { getLogLevels, SERVER_VERSION } from '@app/domain';
import { RedisIoAdapter } from '@app/infra';
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
@@ -10,13 +10,12 @@ import { AppService } from './app.service';
import { useSwagger } from './app.utils';
const logger = new Logger('ImmichServer');
+const envName = (process.env.NODE_ENV || 'development').toUpperCase();
+const port = Number(process.env.SERVER_PORT) || 3001;
const isDev = process.env.NODE_ENV === 'development';
-const serverPort = Number(process.env.SERVER_PORT) || 3001;
async function bootstrap() {
- const app = await NestFactory.create<NestExpressApplication>(AppModule, {
- logger: getLogLevels(),
- });
+ const app = await NestFactory.create<NestExpressApplication>(AppModule, { logger: getLogLevels() });
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
app.set('etag', 'strong');
@@ -27,18 +26,10 @@ async function bootstrap() {
}
app.useWebSocketAdapter(new RedisIoAdapter(app));
useSwagger(app, isDev);
- await app.get(AppService).init();
-
- await app.listen(serverPort, () => {
- const envName = (process.env.NODE_ENV || 'development').toUpperCase();
- logger.log(
- `Running Immich Server in ${envName} environment - version ${SERVER_VERSION} - Listening on port: ${serverPort}`,
- );
- });
- const searchService = app.get(SearchService);
+ await app.get(AppService).init();
+ await app.listen(port);
- logger.warn(`Machine learning is ${MACHINE_LEARNING_ENABLED ? 'enabled' : 'disabled'}`);
- logger.warn(`Search is ${searchService.isEnabled() ? 'enabled' : 'disabled'}`);
+ logger.log(`Immich Server is listening on ${port} [v${SERVER_VERSION}] [${envName}] `);
}
bootstrap();
diff --git a/server/apps/microservices/src/app.service.ts b/server/apps/microservices/src/app.service.ts
index b657b1f5b3..92b0f237c6 100644
--- a/server/apps/microservices/src/app.service.ts
+++ b/server/apps/microservices/src/app.service.ts
@@ -13,11 +13,13 @@ import {
SystemConfigService,
UserService,
} from '@app/domain';
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
import { MetadataExtractionProcessor } from './processors/metadata-extraction.processor';
@Injectable()
export class AppService {
+ private logger = new Logger(AppService.name);
+
constructor(
// TODO refactor to domain
private metadataProcessor: MetadataExtractionProcessor,
@@ -71,5 +73,17 @@ export class AppService {
[JobName.SIDECAR_DISCOVERY]: (data) => this.metadataService.handleSidecarDiscovery(data),
[JobName.SIDECAR_SYNC]: () => this.metadataService.handleSidecarSync(),
});
+
+ process.on('uncaughtException', (error: Error | any) => {
+ const isCsvError = error.code === 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH';
+ if (!isCsvError) {
+ throw error;
+ }
+
+ this.logger.warn('Geocoding csv parse error, trying again without cache...');
+ this.metadataProcessor.init(true);
+ });
+
+ await this.metadataProcessor.init();
}
}
diff --git a/server/apps/microservices/src/main.ts b/server/apps/microservices/src/main.ts
index 0dfdecfc81..1a68181868 100644
--- a/server/apps/microservices/src/main.ts
+++ b/server/apps/microservices/src/main.ts
@@ -4,41 +4,20 @@ import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppService } from './app.service';
import { MicroservicesModule } from './microservices.module';
-import { MetadataExtractionProcessor } from './processors/metadata-extraction.processor';
const logger = new Logger('ImmichMicroservice');
+const port = Number(process.env.MICROSERVICES_PORT) || 3002;
+const envName = (process.env.NODE_ENV || 'development').toUpperCase();
async function bootstrap() {
- const app = await NestFactory.create(MicroservicesModule, {
- logger: getLogLevels(),
- });
-
- const listeningPort = Number(process.env.MICROSERVICES_PORT) || 3002;
-
- await app.get(AppService).init();
+ const app = await NestFactory.create(MicroservicesModule, { logger: getLogLevels() });
app.useWebSocketAdapter(new RedisIoAdapter(app));
- const metadataService = app.get(MetadataExtractionProcessor);
-
- process.on('uncaughtException', (error: Error | any) => {
- const isCsvError = error.code === 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH';
- if (!isCsvError) {
- throw error;
- }
-
- logger.warn('Geocoding csv parse error, trying again without cache...');
- metadataService.init(true);
- });
-
- await metadataService.init();
+ await app.get(AppService).init();
+ await app.listen(port);
- await app.listen(listeningPort, () => {
- const envName = (process.env.NODE_ENV || 'development').toUpperCase();
- logger.log(
- `Running Immich Microservices in ${envName} environment - version ${SERVER_VERSION} - Listening on port: ${listeningPort}`,
- );
- });
+ logger.log(`Immich Microservices is listening on ${port} [v${SERVER_VERSION}] [${envName}] `);
}
bootstrap();