@@ -4,14 +4,14 @@ import { ConfigModule } from '@nestjs/config';
4
4
import { APP_FILTER , APP_GUARD , APP_INTERCEPTOR , APP_PIPE , ModuleRef } from '@nestjs/core' ;
5
5
import { ScheduleModule , SchedulerRegistry } from '@nestjs/schedule' ;
6
6
import { TypeOrmModule } from '@nestjs/typeorm' ;
7
- import _ from 'lodash' ;
8
7
import { ClsModule } from 'nestjs-cls' ;
9
8
import { OpenTelemetryModule } from 'nestjs-otel' ;
10
9
import { commands } from 'src/commands' ;
11
10
import { bullConfig , bullQueues , clsConfig , immichAppConfig } from 'src/config' ;
12
11
import { controllers } from 'src/controllers' ;
13
12
import { databaseConfig } from 'src/database.config' ;
14
13
import { entities } from 'src/entities' ;
14
+ import { ImmichWorker } from 'src/enum' ;
15
15
import { IEventRepository } from 'src/interfaces/event.interface' ;
16
16
import { ILoggerRepository } from 'src/interfaces/logger.interface' ;
17
17
import { AuthGuard } from 'src/middleware/auth.guard' ;
@@ -22,7 +22,6 @@ import { LoggingInterceptor } from 'src/middleware/logging.interceptor';
22
22
import { repositories } from 'src/repositories' ;
23
23
import { services } from 'src/services' ;
24
24
import { DatabaseService } from 'src/services/database.service' ;
25
- import { setupEventHandlers } from 'src/utils/events' ;
26
25
import { otelConfig } from 'src/utils/instrumentation' ;
27
26
28
27
const common = [ ...services , ...repositories ] ;
@@ -56,59 +55,48 @@ const imports = [
56
55
TypeOrmModule . forFeature ( entities ) ,
57
56
] ;
58
57
59
- @Module ( {
60
- imports : [ ...imports , ScheduleModule . forRoot ( ) ] ,
61
- controllers : [ ...controllers ] ,
62
- providers : [ ...common , ...middleware ] ,
63
- } )
64
- export class ApiModule implements OnModuleInit , OnModuleDestroy {
58
+ abstract class BaseModule implements OnModuleInit , OnModuleDestroy {
59
+ private get worker ( ) {
60
+ return this . getWorker ( ) ;
61
+ }
62
+
65
63
constructor (
66
- private moduleRef : ModuleRef ,
64
+ @ Inject ( ILoggerRepository ) logger : ILoggerRepository ,
67
65
@Inject ( IEventRepository ) private eventRepository : IEventRepository ,
68
- @Inject ( ILoggerRepository ) private logger : ILoggerRepository ,
69
66
) {
70
- logger . setAppName ( 'Api' ) ;
67
+ logger . setAppName ( this . worker ) ;
71
68
}
72
69
73
- async onModuleInit ( ) {
74
- const items = setupEventHandlers ( this . moduleRef ) ;
75
-
76
- await this . eventRepository . emit ( 'app.bootstrap' , 'api' ) ;
70
+ abstract getWorker ( ) : ImmichWorker ;
77
71
78
- this . logger . setContext ( 'EventLoader' ) ;
79
- const eventMap = _ . groupBy ( items , 'event' ) ;
80
- for ( const [ event , handlers ] of Object . entries ( eventMap ) ) {
81
- for ( const { priority, label } of handlers ) {
82
- this . logger . verbose ( `Added ${ event } {${ label } ${ priority ? '' : ', ' + priority } } event` ) ;
83
- }
84
- }
72
+ async onModuleInit ( ) {
73
+ this . eventRepository . setup ( { services } ) ;
74
+ await this . eventRepository . emit ( 'app.bootstrap' , this . worker ) ;
85
75
}
86
76
87
77
async onModuleDestroy ( ) {
88
- await this . eventRepository . emit ( 'app.shutdown' ) ;
78
+ await this . eventRepository . emit ( 'app.shutdown' , this . worker ) ;
89
79
}
90
80
}
91
81
92
82
@Module ( {
93
- imports : [ ...imports ] ,
94
- providers : [ ...common , SchedulerRegistry ] ,
83
+ imports : [ ...imports , ScheduleModule . forRoot ( ) ] ,
84
+ controllers : [ ...controllers ] ,
85
+ providers : [ ...common , ...middleware ] ,
95
86
} )
96
- export class MicroservicesModule implements OnModuleInit , OnModuleDestroy {
97
- constructor (
98
- private moduleRef : ModuleRef ,
99
- @Inject ( IEventRepository ) private eventRepository : IEventRepository ,
100
- @Inject ( ILoggerRepository ) logger : ILoggerRepository ,
101
- ) {
102
- logger . setAppName ( 'Microservices' ) ;
103
- }
104
-
105
- async onModuleInit ( ) {
106
- setupEventHandlers ( this . moduleRef ) ;
107
- await this . eventRepository . emit ( 'app.bootstrap' , 'microservices' ) ;
87
+ export class ApiModule extends BaseModule {
88
+ getWorker ( ) {
89
+ return ImmichWorker . API ;
108
90
}
91
+ }
109
92
110
- async onModuleDestroy ( ) {
111
- await this . eventRepository . emit ( 'app.shutdown' ) ;
93
+ @Module ( {
94
+ imports : [ ...imports ] ,
95
+ providers : [ ...common , SchedulerRegistry ] ,
96
+ } )
97
+ export class MicroservicesModule extends BaseModule {
98
+ getWorker ( ) {
99
+ return ImmichWorker . MICROSERVICES ;
112
100
}
113
101
}
114
102
0 commit comments