-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
rabbitmq.module.ts
160 lines (141 loc) 路 4.57 KB
/
rabbitmq.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { DiscoveryModule, DiscoveryService } from '@golevelup/nestjs-discovery';
import {
createConfigurableDynamicRootModule,
IConfigurableDynamicRootModule,
} from '@golevelup/nestjs-modules';
import {
DynamicModule,
Logger,
Module,
OnModuleInit,
OnModuleDestroy,
} from '@nestjs/common';
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
import { groupBy } from 'lodash';
import { AmqpConnection } from './amqp/connection';
import { RABBIT_CONFIG_TOKEN, RABBIT_HANDLER } from './rabbitmq.constants';
import { RabbitHandlerConfig, RabbitMQConfig } from './rabbitmq.interfaces';
declare const placeholder: IConfigurableDynamicRootModule<
RabbitMQModule,
RabbitMQConfig
>;
@Module({
imports: [DiscoveryModule],
})
export class RabbitMQModule
extends createConfigurableDynamicRootModule<RabbitMQModule, RabbitMQConfig>(
RABBIT_CONFIG_TOKEN,
{
providers: [
{
provide: AmqpConnection,
useFactory: async (
config: RabbitMQConfig
): Promise<AmqpConnection> => {
return RabbitMQModule.AmqpConnectionFactory(config);
},
inject: [RABBIT_CONFIG_TOKEN],
},
],
exports: [AmqpConnection],
}
)
implements OnModuleDestroy, OnModuleInit {
private readonly logger = new Logger(RabbitMQModule.name);
constructor(
private readonly discover: DiscoveryService,
private readonly amqpConnection: AmqpConnection,
private readonly externalContextCreator: ExternalContextCreator
) {
super();
}
static async AmqpConnectionFactory(config: RabbitMQConfig) {
const connection = new AmqpConnection(config);
await connection.init();
const logger = new Logger(RabbitMQModule.name);
logger.log('Successfully connected to RabbitMQ');
return connection;
}
public static build(config: RabbitMQConfig): DynamicModule {
const logger = new Logger(RabbitMQModule.name);
logger.warn(
'build() is deprecated. use forRoot() or forRootAsync() to configure RabbitMQ'
);
return {
module: RabbitMQModule,
providers: [
{
provide: AmqpConnection,
useFactory: async (): Promise<AmqpConnection> => {
return RabbitMQModule.AmqpConnectionFactory(config);
},
},
],
exports: [AmqpConnection],
};
}
public static attach(connection: AmqpConnection): DynamicModule {
return {
module: RabbitMQModule,
providers: [
{
provide: AmqpConnection,
useValue: connection,
},
],
exports: [AmqpConnection],
};
}
async onModuleDestroy() {
this.logger.verbose('Closing AMQP Connection');
await this.amqpConnection.managedConnection.close();
}
public async onModuleInit() {
if (!this.amqpConnection.configuration.registerHandlers) {
this.logger.log(
'Skipping RabbitMQ Handlers due to configuration. This application instance will not receive messages over RabbitMQ'
);
return;
}
this.logger.log('Initializing RabbitMQ Handlers');
const rabbitMeta = await this.discover.providerMethodsWithMetaAtKey<
RabbitHandlerConfig
>(RABBIT_HANDLER);
const grouped = groupBy(
rabbitMeta,
(x) => x.discoveredMethod.parentClass.name
);
const providerKeys = Object.keys(grouped);
for (const key of providerKeys) {
this.logger.log(`Registering rabbitmq handlers from ${key}`);
await Promise.all(
grouped[key].map(async ({ discoveredMethod, meta: config }) => {
const handler = this.externalContextCreator.create(
discoveredMethod.parentClass.instance,
discoveredMethod.handler,
discoveredMethod.methodName
);
const { exchange, routingKey, queue } = config;
const handlerDisplayName = `${discoveredMethod.parentClass.name}.${
discoveredMethod.methodName
} {${config.type}} -> ${exchange}::${routingKey}::${
queue || 'amqpgen'
}`;
if (
config.type === 'rpc' &&
!this.amqpConnection.configuration.enableDirectReplyTo
) {
this.logger.warn(
`Direct Reply-To Functionality is disabled. RPC handler ${handlerDisplayName} will not be registered`
);
return;
}
this.logger.log(handlerDisplayName);
return config.type === 'rpc'
? this.amqpConnection.createRpc(handler, config)
: this.amqpConnection.createSubscriber(handler, config);
})
);
}
}
}