refactor(server): bootstrap code (#2682)

* refactor(server): bootstrap code

* Add service name

---------

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
Jason Rasmussen 2023-06-07 10:56:08 -04:00 committed by GitHub
parent eb1225a0a5
commit d08535e7f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 46 deletions

View file

@ -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'}`);
}
}

View file

@ -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(port);
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);
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();

View file

@ -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();
}
}

View file

@ -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);
await app.get(AppService).init();
await app.listen(port);
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.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();