1
0
mirror of https://github.com/immich-app/immich.git synced 2025-07-08 06:23:00 +02:00

refactor: server emit events (#11780)

This commit is contained in:
Jason Rasmussen
2024-08-15 16:12:41 -04:00
committed by GitHub
parent 32c05ea950
commit 433c7ab01d
27 changed files with 222 additions and 182 deletions

View File

@ -5,6 +5,7 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE, ModuleRef } from '@ne
import { EventEmitterModule } from '@nestjs/event-emitter';
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';
@ -13,6 +14,7 @@ import { controllers } from 'src/controllers';
import { databaseConfig } from 'src/database.config';
import { entities } from 'src/entities';
import { IEventRepository } from 'src/interfaces/event.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { AuthGuard } from 'src/middleware/auth.guard';
import { ErrorInterceptor } from 'src/middleware/error.interceptor';
import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor';
@ -54,15 +56,25 @@ export class ApiModule implements OnModuleInit, OnModuleDestroy {
constructor(
private moduleRef: ModuleRef,
@Inject(IEventRepository) private eventRepository: IEventRepository,
@Inject(ILoggerRepository) private logger: ILoggerRepository,
) {}
async onModuleInit() {
setupEventHandlers(this.moduleRef);
await this.eventRepository.emit('onBootstrapEvent', 'api');
const items = setupEventHandlers(this.moduleRef);
await this.eventRepository.emit('onBootstrap', '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('onShutdownEvent');
await this.eventRepository.emit('onShutdown');
}
}
@ -78,11 +90,11 @@ export class MicroservicesModule implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
setupEventHandlers(this.moduleRef);
await this.eventRepository.emit('onBootstrapEvent', 'microservices');
await this.eventRepository.emit('onBootstrap', 'microservices');
}
async onModuleDestroy() {
await this.eventRepository.emit('onShutdownEvent');
await this.eventRepository.emit('onShutdown');
}
}