mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor(server): app module (#13193)
This commit is contained in:
parent
7ee0221c8e
commit
5d0a4bb1a5
18 changed files with 126 additions and 134 deletions
|
|
@ -4,7 +4,6 @@ import { ConfigModule } from '@nestjs/config';
|
|||
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE, ModuleRef } from '@nestjs/core';
|
||||
import { ScheduleModule, SchedulerRegistry } from '@nestjs/schedule';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import _ from 'lodash';
|
||||
import { ClsModule } from 'nestjs-cls';
|
||||
import { OpenTelemetryModule } from 'nestjs-otel';
|
||||
import { commands } from 'src/commands';
|
||||
|
|
@ -12,6 +11,7 @@ import { bullConfig, bullQueues, clsConfig, immichAppConfig } from 'src/config';
|
|||
import { controllers } from 'src/controllers';
|
||||
import { databaseConfig } from 'src/database.config';
|
||||
import { entities } from 'src/entities';
|
||||
import { ImmichWorker } from 'src/enum';
|
||||
import { IEventRepository } from 'src/interfaces/event.interface';
|
||||
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||
import { AuthGuard } from 'src/middleware/auth.guard';
|
||||
|
|
@ -22,7 +22,6 @@ import { LoggingInterceptor } from 'src/middleware/logging.interceptor';
|
|||
import { repositories } from 'src/repositories';
|
||||
import { services } from 'src/services';
|
||||
import { DatabaseService } from 'src/services/database.service';
|
||||
import { setupEventHandlers } from 'src/utils/events';
|
||||
import { otelConfig } from 'src/utils/instrumentation';
|
||||
|
||||
const common = [...services, ...repositories];
|
||||
|
|
@ -56,36 +55,38 @@ const imports = [
|
|||
TypeOrmModule.forFeature(entities),
|
||||
];
|
||||
|
||||
abstract class BaseModule implements OnModuleInit, OnModuleDestroy {
|
||||
private get worker() {
|
||||
return this.getWorker();
|
||||
}
|
||||
|
||||
constructor(
|
||||
@Inject(ILoggerRepository) logger: ILoggerRepository,
|
||||
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
||||
) {
|
||||
logger.setAppName(this.worker);
|
||||
}
|
||||
|
||||
abstract getWorker(): ImmichWorker;
|
||||
|
||||
async onModuleInit() {
|
||||
this.eventRepository.setup({ services });
|
||||
await this.eventRepository.emit('app.bootstrap', this.worker);
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.eventRepository.emit('app.shutdown', this.worker);
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [...imports, ScheduleModule.forRoot()],
|
||||
controllers: [...controllers],
|
||||
providers: [...common, ...middleware],
|
||||
})
|
||||
export class ApiModule implements OnModuleInit, OnModuleDestroy {
|
||||
constructor(
|
||||
private moduleRef: ModuleRef,
|
||||
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
||||
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
||||
) {
|
||||
logger.setAppName('Api');
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
const items = setupEventHandlers(this.moduleRef);
|
||||
|
||||
await this.eventRepository.emit('app.bootstrap', 'api');
|
||||
|
||||
this.logger.setContext('EventLoader');
|
||||
const eventMap = _.groupBy(items, 'event');
|
||||
for (const [event, handlers] of Object.entries(eventMap)) {
|
||||
for (const { priority, label } of handlers) {
|
||||
this.logger.verbose(`Added ${event} {${label}${priority ? '' : ', ' + priority}} event`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.eventRepository.emit('app.shutdown');
|
||||
export class ApiModule extends BaseModule {
|
||||
getWorker() {
|
||||
return ImmichWorker.API;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -93,22 +94,9 @@ export class ApiModule implements OnModuleInit, OnModuleDestroy {
|
|||
imports: [...imports],
|
||||
providers: [...common, SchedulerRegistry],
|
||||
})
|
||||
export class MicroservicesModule implements OnModuleInit, OnModuleDestroy {
|
||||
constructor(
|
||||
private moduleRef: ModuleRef,
|
||||
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
||||
@Inject(ILoggerRepository) logger: ILoggerRepository,
|
||||
) {
|
||||
logger.setAppName('Microservices');
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
setupEventHandlers(this.moduleRef);
|
||||
await this.eventRepository.emit('app.bootstrap', 'microservices');
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.eventRepository.emit('app.shutdown');
|
||||
export class MicroservicesModule extends BaseModule {
|
||||
getWorker() {
|
||||
return ImmichWorker.MICROSERVICES;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue