2023-06-01 06:32:51 -04:00
|
|
|
import { BullModule } from '@nestjs/bullmq';
|
2024-06-27 15:54:20 -04:00
|
|
|
import { Inject, Module, OnModuleDestroy, OnModuleInit, ValidationPipe } from '@nestjs/common';
|
|
|
|
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE, ModuleRef } from '@nestjs/core';
|
2023-10-31 23:40:35 -05:00
|
|
|
import { ScheduleModule, SchedulerRegistry } from '@nestjs/schedule';
|
2023-01-11 21:34:36 -05:00
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
2024-04-15 19:39:06 -04:00
|
|
|
import { ClsModule } from 'nestjs-cls';
|
2024-03-12 01:19:12 -04:00
|
|
|
import { OpenTelemetryModule } from 'nestjs-otel';
|
2024-03-30 00:16:06 -04:00
|
|
|
import { commands } from 'src/commands';
|
|
|
|
import { controllers } from 'src/controllers';
|
|
|
|
import { entities } from 'src/entities';
|
2024-10-04 16:57:34 -04:00
|
|
|
import { ImmichWorker } from 'src/enum';
|
2024-06-27 15:54:20 -04:00
|
|
|
import { IEventRepository } from 'src/interfaces/event.interface';
|
2024-10-31 13:42:58 -04:00
|
|
|
import { IJobRepository } from 'src/interfaces/job.interface';
|
2024-08-15 16:12:41 -04:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-10-21 19:52:30 -04:00
|
|
|
import { ITelemetryRepository } from 'src/interfaces/telemetry.interface';
|
2024-03-21 09:08:29 -05:00
|
|
|
import { AuthGuard } from 'src/middleware/auth.guard';
|
|
|
|
import { ErrorInterceptor } from 'src/middleware/error.interceptor';
|
|
|
|
import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor';
|
2024-09-04 13:32:43 -04:00
|
|
|
import { GlobalExceptionFilter } from 'src/middleware/global-exception.filter';
|
2024-04-16 19:21:57 -04:00
|
|
|
import { LoggingInterceptor } from 'src/middleware/logging.interceptor';
|
2024-03-30 00:16:06 -04:00
|
|
|
import { repositories } from 'src/repositories';
|
2024-10-17 10:50:54 -04:00
|
|
|
import { ConfigRepository } from 'src/repositories/config.repository';
|
2024-10-21 19:52:30 -04:00
|
|
|
import { teardownTelemetry } from 'src/repositories/telemetry.repository';
|
2024-03-30 00:16:06 -04:00
|
|
|
import { services } from 'src/services';
|
2024-09-04 13:32:43 -04:00
|
|
|
import { DatabaseService } from 'src/services/database.service';
|
2023-01-11 21:34:36 -05:00
|
|
|
|
2024-04-15 19:39:06 -04:00
|
|
|
const common = [...services, ...repositories];
|
2023-01-11 21:34:36 -05:00
|
|
|
|
2024-03-21 09:08:29 -05:00
|
|
|
const middleware = [
|
|
|
|
FileUploadInterceptor,
|
2024-09-04 13:32:43 -04:00
|
|
|
{ provide: APP_FILTER, useClass: GlobalExceptionFilter },
|
2024-03-21 09:08:29 -05:00
|
|
|
{ provide: APP_PIPE, useValue: new ValidationPipe({ transform: true, whitelist: true }) },
|
2024-04-16 19:21:57 -04:00
|
|
|
{ provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },
|
2024-03-21 09:08:29 -05:00
|
|
|
{ provide: APP_INTERCEPTOR, useClass: ErrorInterceptor },
|
|
|
|
{ provide: APP_GUARD, useClass: AuthGuard },
|
|
|
|
];
|
|
|
|
|
2024-10-17 10:50:54 -04:00
|
|
|
const configRepository = new ConfigRepository();
|
2024-10-29 16:41:47 -04:00
|
|
|
const { bull, cls, database, otel } = configRepository.getEnv();
|
2024-10-17 10:50:54 -04:00
|
|
|
|
2024-03-21 09:08:29 -05:00
|
|
|
const imports = [
|
2024-10-17 10:50:54 -04:00
|
|
|
BullModule.forRoot(bull.config),
|
|
|
|
BullModule.registerQueue(...bull.queues),
|
2024-10-29 16:41:47 -04:00
|
|
|
ClsModule.forRoot(cls.config),
|
2024-10-17 18:04:25 -04:00
|
|
|
OpenTelemetryModule.forRoot(otel),
|
2024-09-04 13:32:43 -04:00
|
|
|
TypeOrmModule.forRootAsync({
|
|
|
|
inject: [ModuleRef],
|
|
|
|
useFactory: (moduleRef: ModuleRef) => {
|
|
|
|
return {
|
2024-10-24 17:12:25 -04:00
|
|
|
...database.config,
|
2024-09-04 13:32:43 -04:00
|
|
|
poolErrorHandler: (error) => {
|
|
|
|
moduleRef.get(DatabaseService, { strict: false }).handleConnectionError(error);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}),
|
2024-03-30 00:16:06 -04:00
|
|
|
TypeOrmModule.forFeature(entities),
|
2024-03-21 09:08:29 -05:00
|
|
|
];
|
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
abstract class BaseModule implements OnModuleInit, OnModuleDestroy {
|
|
|
|
private get worker() {
|
|
|
|
return this.getWorker();
|
|
|
|
}
|
|
|
|
|
2024-06-27 15:54:20 -04:00
|
|
|
constructor(
|
2024-10-04 16:57:34 -04:00
|
|
|
@Inject(ILoggerRepository) logger: ILoggerRepository,
|
2024-06-27 15:54:20 -04:00
|
|
|
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
2024-10-31 13:42:58 -04:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2024-10-21 19:52:30 -04:00
|
|
|
@Inject(ITelemetryRepository) private telemetryRepository: ITelemetryRepository,
|
2024-10-03 17:49:03 -04:00
|
|
|
) {
|
2024-10-04 16:57:34 -04:00
|
|
|
logger.setAppName(this.worker);
|
2024-10-03 17:49:03 -04:00
|
|
|
}
|
2024-03-21 09:08:29 -05:00
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
abstract getWorker(): ImmichWorker;
|
2024-08-15 16:12:41 -04:00
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
async onModuleInit() {
|
2024-10-21 19:52:30 -04:00
|
|
|
this.telemetryRepository.setup({ repositories: repositories.map(({ useClass }) => useClass) });
|
2024-10-31 13:42:58 -04:00
|
|
|
|
|
|
|
this.jobRepository.setup({ services });
|
|
|
|
if (this.worker === ImmichWorker.MICROSERVICES) {
|
|
|
|
this.jobRepository.startWorkers();
|
|
|
|
}
|
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
this.eventRepository.setup({ services });
|
|
|
|
await this.eventRepository.emit('app.bootstrap', this.worker);
|
2024-06-27 15:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async onModuleDestroy() {
|
2024-10-04 16:57:34 -04:00
|
|
|
await this.eventRepository.emit('app.shutdown', this.worker);
|
2024-10-21 19:52:30 -04:00
|
|
|
await teardownTelemetry();
|
2024-03-21 09:08:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Module({
|
2024-10-04 16:57:34 -04:00
|
|
|
imports: [...imports, ScheduleModule.forRoot()],
|
|
|
|
controllers: [...controllers],
|
|
|
|
providers: [...common, ...middleware],
|
2023-01-11 21:34:36 -05:00
|
|
|
})
|
2024-10-04 16:57:34 -04:00
|
|
|
export class ApiModule extends BaseModule {
|
|
|
|
getWorker() {
|
|
|
|
return ImmichWorker.API;
|
2024-06-27 15:54:20 -04:00
|
|
|
}
|
2024-10-04 16:57:34 -04:00
|
|
|
}
|
2024-06-27 15:54:20 -04:00
|
|
|
|
2024-10-04 16:57:34 -04:00
|
|
|
@Module({
|
|
|
|
imports: [...imports],
|
|
|
|
providers: [...common, SchedulerRegistry],
|
|
|
|
})
|
|
|
|
export class MicroservicesModule extends BaseModule {
|
|
|
|
getWorker() {
|
|
|
|
return ImmichWorker.MICROSERVICES;
|
2024-03-21 09:08:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Module({
|
|
|
|
imports: [...imports],
|
2024-03-30 00:16:06 -04:00
|
|
|
providers: [...common, ...commands, SchedulerRegistry],
|
2024-03-21 09:08:29 -05:00
|
|
|
})
|
|
|
|
export class ImmichAdminModule {}
|