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
4 changed files with 35 additions and 46 deletions

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