rxjs-rabbitmq 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +416 -65
- package/dist/rabbitmq-client.helper.d.ts +34 -2
- package/dist/rabbitmq-client.helper.js +259 -71
- package/dist/rabbitmq-client.helper.js.map +1 -1
- package/dist/types.d.ts +36 -10
- package/dist/utils.d.ts +4 -16
- package/dist/utils.js +12 -3
- package/dist/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/rabbitmq-client.helper.ts +300 -82
- package/src/types.ts +38 -10
- package/src/utils.ts +13 -4
- package/test/rabbitmq-client.test.js +49 -0
|
@@ -29,7 +29,9 @@ import {
|
|
|
29
29
|
} from "rxjs";
|
|
30
30
|
|
|
31
31
|
import {
|
|
32
|
+
getSerializer,
|
|
32
33
|
mapify,
|
|
34
|
+
registerSerializer,
|
|
33
35
|
SERIALIZERS,
|
|
34
36
|
uniqueValue,
|
|
35
37
|
wait
|
|
@@ -39,6 +41,7 @@ import {
|
|
|
39
41
|
MapType,
|
|
40
42
|
QueueExchangeBindingConfig,
|
|
41
43
|
RabbitMqInitConfig,
|
|
44
|
+
RabbitMqLogger,
|
|
42
45
|
RmqEventHandler,
|
|
43
46
|
RmqEventMessage,
|
|
44
47
|
RmqHandleMessageTypeConfig,
|
|
@@ -60,6 +63,7 @@ export type RmgOnEventHandler = (event: RmqEventMessage, rmqClient?: RabbitMQCli
|
|
|
60
63
|
*/
|
|
61
64
|
export class RabbitMQClient {
|
|
62
65
|
private clientInitConfig: RabbitMqInitConfig;
|
|
66
|
+
private logger: RabbitMqLogger;
|
|
63
67
|
|
|
64
68
|
private connection: Connection;
|
|
65
69
|
private channel: Channel;
|
|
@@ -67,6 +71,8 @@ export class RabbitMQClient {
|
|
|
67
71
|
private isReadyStream: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(this.isReady);
|
|
68
72
|
private connectionErrorStream: Subject<any> = new Subject<any>();
|
|
69
73
|
private connectionCloseStream: Subject<any> = new Subject<any>();
|
|
74
|
+
private reconnectTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
75
|
+
private reconnectAttempts: number = 0;
|
|
70
76
|
|
|
71
77
|
/*
|
|
72
78
|
Handle all messages from all queues on this client
|
|
@@ -80,13 +86,35 @@ export class RabbitMQClient {
|
|
|
80
86
|
|
|
81
87
|
private DEFAULT_LISTENER_TYPE: string = '__default';
|
|
82
88
|
private queueListeners: MapType<Subscription> = {};
|
|
89
|
+
private registeredQueueConsumers: Array<{ queue: string, options?: Options.Consume }> = [];
|
|
90
|
+
private metricsEnabled: boolean;
|
|
91
|
+
private metrics: {
|
|
92
|
+
messagesReceived: number;
|
|
93
|
+
messagesPublished: number;
|
|
94
|
+
messagesAcked: number;
|
|
95
|
+
messagesNacked: number;
|
|
96
|
+
messagesRetried: number;
|
|
97
|
+
rpcRequests: number;
|
|
98
|
+
rpcResponses: number;
|
|
99
|
+
reconnects: number;
|
|
100
|
+
} = {
|
|
101
|
+
messagesReceived: 0,
|
|
102
|
+
messagesPublished: 0,
|
|
103
|
+
messagesAcked: 0,
|
|
104
|
+
messagesNacked: 0,
|
|
105
|
+
messagesRetried: 0,
|
|
106
|
+
rpcRequests: 0,
|
|
107
|
+
rpcResponses: 0,
|
|
108
|
+
reconnects: 0,
|
|
109
|
+
};
|
|
83
110
|
|
|
84
|
-
private queueToEventHandleMapping:
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
private
|
|
88
|
-
|
|
89
|
-
|
|
111
|
+
private queueToEventHandleMapping: Map<string, Map<string, ReplaySubject<RmqEventMessage>>> = new Map();
|
|
112
|
+
private queueToEventCallbackMapping: Map<string, Map<string, RmqMessageTypeHandler>> = new Map();
|
|
113
|
+
|
|
114
|
+
private log(level: keyof RabbitMqLogger, ...args: unknown[]) {
|
|
115
|
+
const handler = this.logger[level] || (() => undefined);
|
|
116
|
+
handler(...args);
|
|
117
|
+
}
|
|
90
118
|
|
|
91
119
|
get onReady(): Observable<boolean> {
|
|
92
120
|
return this.isReadyStream.asObservable().pipe(
|
|
@@ -109,13 +137,65 @@ export class RabbitMQClient {
|
|
|
109
137
|
|
|
110
138
|
constructor(clientInitConfig: RabbitMqInitConfig) {
|
|
111
139
|
this.clientInitConfig = clientInitConfig;
|
|
140
|
+
this.metricsEnabled = !!clientInitConfig.metrics;
|
|
141
|
+
this.logger = {
|
|
142
|
+
debug: clientInitConfig.logger?.debug ?? (() => undefined),
|
|
143
|
+
info: clientInitConfig.logger?.info ?? console.info.bind(console),
|
|
144
|
+
warn: clientInitConfig.logger?.warn ?? console.warn.bind(console),
|
|
145
|
+
error: clientInitConfig.logger?.error ?? console.error.bind(console),
|
|
146
|
+
};
|
|
112
147
|
!clientInitConfig.stopAutoInit && this.init();
|
|
113
148
|
}
|
|
114
149
|
|
|
150
|
+
static registerSerializer(contentType: string, serializer: { serialize: (value: unknown) => Buffer; deserialize: (value: Buffer, encoding?: BufferEncoding) => unknown }) {
|
|
151
|
+
return registerSerializer(contentType, serializer);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
getMetrics() {
|
|
155
|
+
return { ...this.metrics };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private resubscribeToRegisteredQueues() {
|
|
159
|
+
for (const { queue, options } of this.registeredQueueConsumers) {
|
|
160
|
+
if (this.queueListeners[queue]) {
|
|
161
|
+
this.queueListeners[queue].unsubscribe();
|
|
162
|
+
delete this.queueListeners[queue];
|
|
163
|
+
}
|
|
164
|
+
this.listenToQueue(queue, options);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private scheduleReconnect(reason: string) {
|
|
169
|
+
if (this.reconnectTimeout) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const retryAttempts = this.clientInitConfig.retryAttempts || 0;
|
|
174
|
+
if (retryAttempts > 0 && this.reconnectAttempts >= retryAttempts) {
|
|
175
|
+
this.log('error', `RabbitMQ reconnect attempts exhausted after ${retryAttempts} tries.`);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
this.isReady = false;
|
|
180
|
+
this.isReadyStream.next(false);
|
|
181
|
+
this.reconnectAttempts += 1;
|
|
182
|
+
this.metrics.reconnects += 1;
|
|
183
|
+
const retryDelay = this.clientInitConfig.retryDelay || 1000;
|
|
184
|
+
|
|
185
|
+
this.log('warn', `${reason}; reconnecting in ${retryDelay}ms (attempt ${this.reconnectAttempts}/${retryAttempts || '∞'})`);
|
|
186
|
+
|
|
187
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
188
|
+
this.reconnectTimeout = undefined;
|
|
189
|
+
this.init().catch((error) => {
|
|
190
|
+
this.log('error', 'Reconnect failed', error);
|
|
191
|
+
});
|
|
192
|
+
}, retryDelay);
|
|
193
|
+
}
|
|
194
|
+
|
|
115
195
|
init() {
|
|
116
196
|
if (this.isReady) {
|
|
117
|
-
|
|
118
|
-
return;
|
|
197
|
+
this.log('info', 'Already initialized');
|
|
198
|
+
return Promise.resolve();
|
|
119
199
|
}
|
|
120
200
|
const {
|
|
121
201
|
delayStart,
|
|
@@ -133,11 +213,11 @@ export class RabbitMQClient {
|
|
|
133
213
|
|
|
134
214
|
const init: () => Promise<any> = () => {
|
|
135
215
|
// creating connection
|
|
136
|
-
|
|
216
|
+
this.log('info', 'Attempting connection to Rabbit MQ...');
|
|
137
217
|
return connect(connection_url)
|
|
138
218
|
.then((connection) => {
|
|
139
219
|
this.connection = connection;
|
|
140
|
-
|
|
220
|
+
this.log('info', 'Connected to message server');
|
|
141
221
|
return this.connection.createChannel();
|
|
142
222
|
})
|
|
143
223
|
// creating channel
|
|
@@ -146,30 +226,29 @@ export class RabbitMQClient {
|
|
|
146
226
|
if (prefetch) {
|
|
147
227
|
this.channel.prefetch(prefetch || 1);
|
|
148
228
|
}
|
|
149
|
-
|
|
229
|
+
this.log('info', 'Channel created on message server');
|
|
150
230
|
})
|
|
151
231
|
// assert to queues
|
|
152
232
|
.then(() => {
|
|
153
233
|
const promises: Promise<Replies.AssertQueue>[] = [];
|
|
154
234
|
|
|
155
235
|
for (const queueConfig of queues) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const queueCallbacksMap = this.queueToEventCallbackMapping[queueConfig.name];
|
|
236
|
+
const queueListenersMap = new Map<string, ReplaySubject<RmqEventMessage>>();
|
|
237
|
+
const queueCallbacksMap = new Map<string, RmqMessageTypeHandler>();
|
|
238
|
+
this.queueToEventHandleMapping.set(queueConfig.name, queueListenersMap);
|
|
239
|
+
this.queueToEventCallbackMapping.set(queueConfig.name, queueCallbacksMap);
|
|
161
240
|
|
|
162
241
|
// by default, create an observable stream on the queue for unidentified/null routing keys
|
|
163
|
-
queueListenersMap
|
|
242
|
+
queueListenersMap.set(this.DEFAULT_LISTENER_TYPE, new ReplaySubject());
|
|
164
243
|
|
|
165
244
|
const useHandleMessageTypes: RmqHandleMessageTypeConfigs = queueConfig.handleMessageTypes || [];
|
|
166
245
|
for (const messageType of useHandleMessageTypes) {
|
|
167
246
|
if (typeof messageType === 'string') {
|
|
168
|
-
queueListenersMap
|
|
247
|
+
queueListenersMap.set(messageType as string, new ReplaySubject());
|
|
169
248
|
}
|
|
170
249
|
else {
|
|
171
250
|
const config: RmqHandleMessageTypeConfig = messageType;
|
|
172
|
-
queueCallbacksMap
|
|
251
|
+
queueCallbacksMap.set(config.messageName, config.callbackHandler);
|
|
173
252
|
}
|
|
174
253
|
}
|
|
175
254
|
|
|
@@ -178,7 +257,7 @@ export class RabbitMQClient {
|
|
|
178
257
|
|
|
179
258
|
return Promise.all(promises).then((values) => {
|
|
180
259
|
this.queues = mapify(values, 'queue');
|
|
181
|
-
|
|
260
|
+
this.log('info', 'queues created on channel');
|
|
182
261
|
});
|
|
183
262
|
})
|
|
184
263
|
// assert exchanges
|
|
@@ -186,7 +265,7 @@ export class RabbitMQClient {
|
|
|
186
265
|
const promises: Promise<Replies.AssertExchange>[] = exchanges.map((config) => this.channel.assertExchange(config.name, config.type, config.options));
|
|
187
266
|
return Promise.all(promises).then(values => {
|
|
188
267
|
this.exchanges = mapify(values, 'exchange');
|
|
189
|
-
|
|
268
|
+
this.log('info', 'exchanges created on channel');
|
|
190
269
|
});
|
|
191
270
|
})
|
|
192
271
|
// apply bindings
|
|
@@ -195,30 +274,36 @@ export class RabbitMQClient {
|
|
|
195
274
|
// bind the exclusive queue to all the exchanges
|
|
196
275
|
// const exclusiveBindings = bindings.map((config) => this.channel.bindQueue(this.EXCLUSIVE_QUEUE, config.exchange, config.routingKey));
|
|
197
276
|
return Promise.all(promises).then(values => {
|
|
198
|
-
|
|
277
|
+
this.log('info', 'bindings created on channel');
|
|
199
278
|
});
|
|
200
279
|
})
|
|
201
280
|
.then(() => {
|
|
202
|
-
|
|
281
|
+
this.log('info', 'Client initialization complete; waiting for messages/events...');
|
|
203
282
|
|
|
204
283
|
// initialization complete; listen for connection issues to retry again
|
|
205
284
|
retryAttempts = this.clientInitConfig.retryAttempts;
|
|
206
285
|
|
|
207
286
|
this.connection.on("error", (err) => {
|
|
208
287
|
this.connectionErrorStream.next(err);
|
|
288
|
+
this.log('error', 'RabbitMQ connection error', err);
|
|
289
|
+
this.scheduleReconnect('RabbitMQ connection error');
|
|
209
290
|
});
|
|
210
291
|
this.connection.on("close", (err) => {
|
|
211
292
|
this.connectionCloseStream.next(err);
|
|
293
|
+
this.log('warn', 'RabbitMQ connection closed', err);
|
|
294
|
+
this.scheduleReconnect('RabbitMQ connection closed');
|
|
212
295
|
});
|
|
213
296
|
|
|
297
|
+
this.reconnectAttempts = 0;
|
|
214
298
|
this.isReady = true;
|
|
215
299
|
this.isReadyStream.next(true);
|
|
300
|
+
this.resubscribeToRegisteredQueues();
|
|
216
301
|
})
|
|
217
302
|
|
|
218
303
|
.catch((error) => {
|
|
219
|
-
|
|
304
|
+
this.log('error', 'connection error, retryAttempts:', retryAttempts, error);
|
|
220
305
|
if (retryAttempts === 0) {
|
|
221
|
-
|
|
306
|
+
this.log('error', 'all retry attemptys exhaused; exiting...');
|
|
222
307
|
throw error;
|
|
223
308
|
}
|
|
224
309
|
retryAttempts = retryAttempts - 1;
|
|
@@ -228,16 +313,16 @@ export class RabbitMQClient {
|
|
|
228
313
|
|
|
229
314
|
return wait(delayStart || 0)
|
|
230
315
|
.then(() => {
|
|
231
|
-
|
|
316
|
+
this.log('info', 'Running pre init promises...');
|
|
232
317
|
return Promise.all(pre_init_promises?.map(p => p instanceof Promise ? p : p()) || []).then(() => {
|
|
233
|
-
|
|
318
|
+
this.log('info', 'Done running pre init promises.');
|
|
234
319
|
});
|
|
235
320
|
})
|
|
236
321
|
.then(init)
|
|
237
322
|
.then(() => {
|
|
238
|
-
|
|
323
|
+
this.log('info', 'Running post init promises...');
|
|
239
324
|
return Promise.all(post_init_promises?.map(p => p instanceof Promise ? p : p()) || []).then(() => {
|
|
240
|
-
|
|
325
|
+
this.log('info', 'Done running post init promises.');
|
|
241
326
|
});
|
|
242
327
|
});
|
|
243
328
|
}
|
|
@@ -265,7 +350,11 @@ export class RabbitMQClient {
|
|
|
265
350
|
// see if a listener was created for the routing key
|
|
266
351
|
const messageType = message.properties.type;
|
|
267
352
|
const useContentType = message.properties.contentType;
|
|
268
|
-
const
|
|
353
|
+
const serializer = getSerializer(useContentType || 'application/octet-stream');
|
|
354
|
+
const useData = serializer ? serializer.deserialize(message.content) : message.content;
|
|
355
|
+
if (this.metricsEnabled) {
|
|
356
|
+
this.metrics.messagesReceived += 1;
|
|
357
|
+
}
|
|
269
358
|
const messageObj: RmqEventMessage = {
|
|
270
359
|
data: useData,
|
|
271
360
|
message,
|
|
@@ -284,34 +373,40 @@ export class RabbitMQClient {
|
|
|
284
373
|
|
|
285
374
|
// console.log(`Message on queue ${queue}:`, messageObj);
|
|
286
375
|
|
|
376
|
+
const queueListeners = this.queueToEventHandleMapping.get(queue) ?? new Map();
|
|
377
|
+
const queueCallbacks = this.queueToEventCallbackMapping.get(queue) ?? new Map();
|
|
378
|
+
|
|
287
379
|
if (!messageType) {
|
|
288
380
|
// no type key found, push to default stream
|
|
289
|
-
|
|
290
|
-
|
|
381
|
+
this.log('debug', `No message type found; pushing to default handler on queue ${queue}`);
|
|
382
|
+
const defaultListener = queueListeners.get(this.DEFAULT_LISTENER_TYPE);
|
|
383
|
+
defaultListener?.next(messageObj);
|
|
291
384
|
return;
|
|
292
385
|
}
|
|
293
386
|
|
|
294
|
-
|
|
387
|
+
const typeListener = queueListeners.get(messageType);
|
|
388
|
+
if (typeListener) {
|
|
295
389
|
// there is a registered listener for the type, push to that stream
|
|
296
|
-
|
|
297
|
-
|
|
390
|
+
this.log('debug', `message type handler found; pushing to ${messageType} handler on queue ${queue}`);
|
|
391
|
+
typeListener.next(messageObj);
|
|
298
392
|
}
|
|
299
|
-
else if (
|
|
393
|
+
else if (queueCallbacks.has(messageType)) {
|
|
300
394
|
// there is a registered listener for the type, push to that stream
|
|
301
|
-
|
|
302
|
-
const callbackHandler =
|
|
395
|
+
this.log('debug', `message type callback found; pushing to ${messageType} callback on queue ${queue}`);
|
|
396
|
+
const callbackHandler = queueCallbacks.get(messageType)!;
|
|
303
397
|
callbackHandler(messageObj);
|
|
304
398
|
}
|
|
305
399
|
else {
|
|
306
400
|
// no registered listener
|
|
307
401
|
if (this.clientInitConfig.autoAckUnhandledMessageTypes) {
|
|
308
|
-
|
|
402
|
+
this.log('debug', `No handler found for message type ${messageType} on queue ${queue}; auto acknowledging.`);
|
|
309
403
|
this.ack(message);
|
|
310
404
|
return;
|
|
311
405
|
}
|
|
312
406
|
else if (this.clientInitConfig.pushUnhandledMessageTypesToDefaultHandler) {
|
|
313
|
-
|
|
314
|
-
|
|
407
|
+
this.log('debug', `No handler found for message type ${messageType} on queue ${queue}; pushing to ${this.DEFAULT_LISTENER_TYPE} handler.`);
|
|
408
|
+
const defaultListener = queueListeners.get(this.DEFAULT_LISTENER_TYPE);
|
|
409
|
+
defaultListener?.next(messageObj);
|
|
315
410
|
return;
|
|
316
411
|
}
|
|
317
412
|
else {
|
|
@@ -327,11 +422,14 @@ export class RabbitMQClient {
|
|
|
327
422
|
|
|
328
423
|
private listenToQueue(queue: string, options?: Options.Consume) {
|
|
329
424
|
if (this.queueListeners[queue]) {
|
|
330
|
-
|
|
425
|
+
this.log('debug', `Already Registered messages/events listener for queue: ${queue}`);
|
|
331
426
|
return;
|
|
332
427
|
}
|
|
333
428
|
|
|
334
|
-
|
|
429
|
+
this.registeredQueueConsumers = this.registeredQueueConsumers.filter((entry) => entry.queue !== queue);
|
|
430
|
+
this.registeredQueueConsumers.push({ queue, options });
|
|
431
|
+
|
|
432
|
+
this.log('info', `Registering messages/events listener for queue: ${queue}`);
|
|
335
433
|
const startQueueListener = this.getQueueListener(queue, options);
|
|
336
434
|
|
|
337
435
|
this.messagesStreamsByQueue[queue] = new Subject<RmqEventMessage>();
|
|
@@ -342,7 +440,7 @@ export class RabbitMQClient {
|
|
|
342
440
|
)
|
|
343
441
|
.subscribe({
|
|
344
442
|
next: (readyState) => {
|
|
345
|
-
|
|
443
|
+
this.log('info', `Registered: Now listening to messages/events on queue ${queue}...`);
|
|
346
444
|
}
|
|
347
445
|
});
|
|
348
446
|
}
|
|
@@ -354,17 +452,21 @@ export class RabbitMQClient {
|
|
|
354
452
|
const handle = (messageType: string) => {
|
|
355
453
|
return this.onReady.pipe(
|
|
356
454
|
mergeMap((ready: boolean, index: number) => {
|
|
357
|
-
|
|
455
|
+
const queueListeners = this.queueToEventHandleMapping.get(queue);
|
|
456
|
+
if (!queueListeners?.has(messageType)) {
|
|
358
457
|
throw new Error(`The provided routing key was not provided during initialization. Please add routing key "${messageType}" in the list of routing keys for the queue config in the constructor.`);
|
|
359
458
|
}
|
|
360
|
-
return
|
|
459
|
+
return queueListeners.get(messageType)!.asObservable();
|
|
361
460
|
})
|
|
362
461
|
);
|
|
363
462
|
};
|
|
364
463
|
|
|
365
464
|
const handleDefault = () => {
|
|
366
465
|
return this.onReady.pipe(
|
|
367
|
-
mergeMap((ready: boolean, index: number) =>
|
|
466
|
+
mergeMap((ready: boolean, index: number) => {
|
|
467
|
+
const queueListeners = this.queueToEventHandleMapping.get(queue);
|
|
468
|
+
return (queueListeners?.get(this.DEFAULT_LISTENER_TYPE) ?? new ReplaySubject<RmqEventMessage>()).asObservable();
|
|
469
|
+
})
|
|
368
470
|
);
|
|
369
471
|
};
|
|
370
472
|
|
|
@@ -399,26 +501,94 @@ export class RabbitMQClient {
|
|
|
399
501
|
|
|
400
502
|
ack(message: ConsumeMessage) {
|
|
401
503
|
this.channel.ack(message);
|
|
402
|
-
|
|
504
|
+
if (this.metricsEnabled) {
|
|
505
|
+
this.metrics.messagesAcked += 1;
|
|
506
|
+
}
|
|
507
|
+
this.log('debug', 'Acknoledged rmq message.');
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
nack(message: ConsumeMessage, requeue: boolean = true) {
|
|
511
|
+
if (!this.channel) {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
this.channel.nack(message, false, requeue);
|
|
515
|
+
if (this.metricsEnabled) {
|
|
516
|
+
this.metrics.messagesNacked += 1;
|
|
517
|
+
}
|
|
518
|
+
this.log('debug', `Nacknowledged rmq message with requeue=${requeue}.`);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
retryMessage(message: ConsumeMessage, options: { delayMs?: number, requeue?: boolean, queue?: string } = {}) {
|
|
522
|
+
const { delayMs = 0, requeue = true, queue = '' } = options;
|
|
523
|
+
const targetQueue = queue || message.fields.routingKey || '';
|
|
524
|
+
if (delayMs > 0) {
|
|
525
|
+
setTimeout(() => {
|
|
526
|
+
if (targetQueue) {
|
|
527
|
+
this.channel.sendToQueue(targetQueue, message.content, { persistent: true, ...message.properties });
|
|
528
|
+
}
|
|
529
|
+
}, delayMs);
|
|
530
|
+
}
|
|
531
|
+
else if (targetQueue) {
|
|
532
|
+
this.channel.sendToQueue(targetQueue, message.content, { persistent: true, ...message.properties });
|
|
533
|
+
}
|
|
534
|
+
this.nack(message, requeue);
|
|
535
|
+
if (this.metricsEnabled) {
|
|
536
|
+
this.metrics.messagesRetried += 1;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
deadLetter(message: ConsumeMessage, reason: string, queue?: string) {
|
|
541
|
+
const targetQueue = queue || `${message.fields.routingKey || 'dead-letter'}.dlq`;
|
|
542
|
+
const headers = { ...(message.properties.headers || {}), 'x-death-reason': reason };
|
|
543
|
+
this.channel.sendToQueue(targetQueue, message.content, {
|
|
544
|
+
...message.properties,
|
|
545
|
+
headers,
|
|
546
|
+
persistent: true,
|
|
547
|
+
});
|
|
548
|
+
this.nack(message, false);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
async close() {
|
|
552
|
+
if (this.reconnectTimeout) {
|
|
553
|
+
clearTimeout(this.reconnectTimeout);
|
|
554
|
+
this.reconnectTimeout = undefined;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if (this.channel) {
|
|
558
|
+
await this.channel.close();
|
|
559
|
+
}
|
|
560
|
+
if (this.connection) {
|
|
561
|
+
await this.connection.close();
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
this.isReady = false;
|
|
565
|
+
this.isReadyStream.next(false);
|
|
566
|
+
this.log('info', 'RabbitMQ client closed.');
|
|
403
567
|
}
|
|
404
568
|
|
|
405
569
|
sendMessage(options: RmqSendMessageParams) {
|
|
406
570
|
const send = () => {
|
|
407
571
|
const { data, publishOptions, queue } = options;
|
|
408
572
|
const useContentType = publishOptions.contentType || ContentTypes.TEXT;
|
|
409
|
-
const
|
|
573
|
+
const serializer = getSerializer(useContentType) || SERIALIZERS[useContentType];
|
|
574
|
+
const useData: Buffer = serializer
|
|
575
|
+
? (serializer.serialize(data) as Buffer)
|
|
576
|
+
: Buffer.from(typeof data === 'string' ? data : JSON.stringify(data), 'utf8');
|
|
577
|
+
if (this.metricsEnabled) {
|
|
578
|
+
this.metrics.messagesPublished += 1;
|
|
579
|
+
}
|
|
410
580
|
this.channel.sendToQueue(queue, useData, { ...publishOptions, appId: publishOptions.appId });
|
|
411
581
|
};
|
|
412
582
|
|
|
413
583
|
if (!this.isReady) {
|
|
414
|
-
|
|
584
|
+
this.log('debug', 'wait until ready to send message...');
|
|
415
585
|
firstValueFrom(this.onReady).then((readyState) => {
|
|
416
|
-
|
|
586
|
+
this.log('debug', 'now ready to send message', { readyState });
|
|
417
587
|
send();
|
|
418
588
|
});
|
|
419
589
|
}
|
|
420
590
|
else {
|
|
421
|
-
|
|
591
|
+
this.log('debug', 'is ready to send message');
|
|
422
592
|
send();
|
|
423
593
|
}
|
|
424
594
|
}
|
|
@@ -427,36 +597,77 @@ export class RabbitMQClient {
|
|
|
427
597
|
queue: string,
|
|
428
598
|
data: any,
|
|
429
599
|
publishOptions: Options.Publish,
|
|
430
|
-
}, autoAckOnReceiveReply: boolean) {
|
|
600
|
+
}, autoAckOnReceiveReply: boolean, requestTimeoutMs?: number) {
|
|
431
601
|
const start_time = Date.now();
|
|
432
602
|
const consumerTag = uniqueValue();
|
|
433
603
|
const correlationId = options.publishOptions.correlationId || uuidv4();
|
|
604
|
+
const timeoutMs = requestTimeoutMs ?? this.clientInitConfig.rpcRequestTimeoutMs ?? 30000;
|
|
434
605
|
|
|
435
606
|
const send = () => {
|
|
436
607
|
const { data, publishOptions, queue } = options;
|
|
437
608
|
const useContentType = publishOptions.contentType || ContentTypes.TEXT;
|
|
438
|
-
const
|
|
609
|
+
const serializer = getSerializer(useContentType) || SERIALIZERS[useContentType];
|
|
610
|
+
const useData: Buffer = serializer
|
|
611
|
+
? (serializer.serialize(data) as Buffer)
|
|
612
|
+
: Buffer.from(typeof data === 'string' ? data : JSON.stringify(data), 'utf8');
|
|
613
|
+
if (this.metricsEnabled) {
|
|
614
|
+
this.metrics.rpcRequests += 1;
|
|
615
|
+
}
|
|
439
616
|
this.channel.sendToQueue(queue, useData, { ...publishOptions, correlationId, replyTo: temporaryRpcQueue, appId: publishOptions.appId });
|
|
440
617
|
};
|
|
441
618
|
|
|
442
619
|
const watch = <T> () => {
|
|
443
620
|
return new Promise<RmqEventMessage<T>>(async (resolve, reject) => {
|
|
444
|
-
|
|
621
|
+
let settled = false;
|
|
622
|
+
let timeoutHandle: NodeJS.Timeout | undefined;
|
|
623
|
+
|
|
624
|
+
const cleanup = async () => {
|
|
625
|
+
if (timeoutHandle) {
|
|
626
|
+
clearTimeout(timeoutHandle);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
try {
|
|
630
|
+
await this.channel.cancel(consumerTag);
|
|
631
|
+
} catch (error) {
|
|
632
|
+
this.log('debug', 'Ignoring cancel failure for RPC consumer', { consumerTag, error });
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
timeoutHandle = setTimeout(async () => {
|
|
637
|
+
if (settled) {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
settled = true;
|
|
641
|
+
await cleanup();
|
|
642
|
+
reject(new Error(`RabbitMQ request timed out after ${timeoutMs}ms for correlationId ${correlationId}`));
|
|
643
|
+
}, timeoutMs);
|
|
644
|
+
|
|
645
|
+
this.log('debug', 'request/rpc sent; awaiting response...', { correlationId, consumerTag, temporaryRpcQueue });
|
|
445
646
|
|
|
446
647
|
const replyHandler = (message: ConsumeMessage | null) => {
|
|
447
|
-
|
|
448
|
-
|
|
648
|
+
if (!message || settled) {
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
this.log('debug', 'Received reply.', { message });
|
|
653
|
+
const correlationIdMatch = message.properties.correlationId == correlationId;
|
|
449
654
|
const isReplyToRequest: boolean = !!message && correlationIdMatch;
|
|
450
|
-
|
|
655
|
+
this.log('debug', { isReplyToRequest, consumerTag, correlationIdMatch, requestCorrelationId: correlationId, responseCorrelationId: message.properties.correlationId });
|
|
451
656
|
|
|
452
657
|
if (isReplyToRequest) {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
const
|
|
456
|
-
|
|
457
|
-
|
|
658
|
+
settled = true;
|
|
659
|
+
void cleanup();
|
|
660
|
+
const useContentType = message.properties.contentType;
|
|
661
|
+
const serializer = getSerializer(useContentType || 'application/octet-stream') || SERIALIZERS[useContentType || 'application/octet-stream'];
|
|
662
|
+
const useData = serializer ? serializer.deserialize(message.content) : message.content;
|
|
663
|
+
if (this.metricsEnabled) {
|
|
664
|
+
this.metrics.rpcResponses += 1;
|
|
665
|
+
}
|
|
666
|
+
const messageObj: RmqEventMessage<T> = {
|
|
667
|
+
data: useData as T,
|
|
668
|
+
message,
|
|
458
669
|
ack: () => {
|
|
459
|
-
this.ack(message
|
|
670
|
+
this.ack(message);
|
|
460
671
|
},
|
|
461
672
|
sendMessage: this.sendMessage.bind(this),
|
|
462
673
|
sendRequest: this.sendRequest.bind(this),
|
|
@@ -465,9 +676,9 @@ export class RabbitMQClient {
|
|
|
465
676
|
const end_time = Date.now();
|
|
466
677
|
const total_time = (end_time - start_time) / 1000;
|
|
467
678
|
const time_in_seconds = total_time.toFixed();
|
|
468
|
-
|
|
679
|
+
this.log('debug', 'received response from request/rpc:', { start_time, end_time, total_time, time_in_seconds, messageObj, options });
|
|
469
680
|
if (autoAckOnReceiveReply) {
|
|
470
|
-
this.ack(message
|
|
681
|
+
this.ack(message);
|
|
471
682
|
}
|
|
472
683
|
resolve(messageObj);
|
|
473
684
|
}
|
|
@@ -486,38 +697,39 @@ export class RabbitMQClient {
|
|
|
486
697
|
data: any,
|
|
487
698
|
publishOptions: Options.Publish,
|
|
488
699
|
},
|
|
489
|
-
autoAckOnReceiveReply: boolean = true
|
|
700
|
+
autoAckOnReceiveReply: boolean = true,
|
|
701
|
+
timeoutMs?: number
|
|
490
702
|
) {
|
|
491
|
-
|
|
703
|
+
this.log('debug', 'sendRequest:', options);
|
|
492
704
|
|
|
493
705
|
return new Promise<RmqEventMessage<T>>(async (resolve, reject) => {
|
|
494
706
|
|
|
495
707
|
const temporaryRpcQueue = uuidv1();
|
|
496
708
|
|
|
497
709
|
const newQueue = await this.channel.assertQueue(temporaryRpcQueue, { exclusive: true, durable: false, autoDelete: true }).then((response) => {
|
|
498
|
-
|
|
710
|
+
this.log('debug', `Created temp queue ${temporaryRpcQueue} for client.`);
|
|
499
711
|
return response;
|
|
500
712
|
});
|
|
501
713
|
|
|
502
|
-
const rpc = this.getRpcMethods(temporaryRpcQueue, options, autoAckOnReceiveReply);
|
|
714
|
+
const rpc = this.getRpcMethods(temporaryRpcQueue, options, autoAckOnReceiveReply, timeoutMs);
|
|
503
715
|
|
|
504
716
|
if (!this.isReady) {
|
|
505
|
-
|
|
717
|
+
this.log('debug', 'waiting until ready to send request');
|
|
506
718
|
firstValueFrom(this.onReady).then((readyState) => {
|
|
507
|
-
|
|
719
|
+
this.log('debug', 'now ready and watching temp queue', { readyState });
|
|
508
720
|
rpc.watch<T>().then((message: RmqEventMessage<T>) => {
|
|
509
721
|
resolve(message);
|
|
510
|
-
}); // first listen on the reply queue
|
|
511
|
-
|
|
722
|
+
}).catch(reject); // first listen on the reply queue
|
|
723
|
+
this.log('debug', 'sending request');
|
|
512
724
|
rpc.send(); // then send the rpc/rmq message
|
|
513
|
-
});
|
|
725
|
+
}).catch(reject);
|
|
514
726
|
}
|
|
515
727
|
else {
|
|
516
|
-
|
|
728
|
+
this.log('debug', 'now ready and watching temp queue');
|
|
517
729
|
rpc.watch<T>().then((message: RmqEventMessage<T>) => {
|
|
518
730
|
resolve(message);
|
|
519
|
-
}); // first listen on the reply queue
|
|
520
|
-
|
|
731
|
+
}).catch(reject); // first listen on the reply queue
|
|
732
|
+
this.log('debug', 'sending request');
|
|
521
733
|
rpc.send(); // then send the rpc/rmq message
|
|
522
734
|
}
|
|
523
735
|
});
|
|
@@ -527,21 +739,27 @@ export class RabbitMQClient {
|
|
|
527
739
|
const publish = () => {
|
|
528
740
|
const { data, routingKey, publishOptions, exchange } = options;
|
|
529
741
|
const useContentType = publishOptions.contentType || ContentTypes.TEXT;
|
|
530
|
-
const
|
|
742
|
+
const serializer = getSerializer(useContentType) || SERIALIZERS[useContentType];
|
|
743
|
+
const useData: Buffer = serializer
|
|
744
|
+
? (serializer.serialize(data) as Buffer)
|
|
745
|
+
: Buffer.from(typeof data === 'string' ? data : JSON.stringify(data), 'utf8');
|
|
746
|
+
if (this.metricsEnabled) {
|
|
747
|
+
this.metrics.messagesPublished += 1;
|
|
748
|
+
}
|
|
531
749
|
this.channel.publish(exchange, routingKey || '', useData, { ...publishOptions, appId: publishOptions.appId });
|
|
532
750
|
};
|
|
533
751
|
|
|
534
752
|
if (!this.isReady) {
|
|
535
|
-
|
|
753
|
+
this.log('debug', 'wait until ready to publish event');
|
|
536
754
|
this.onReady.subscribe({
|
|
537
755
|
next: () => {
|
|
538
|
-
|
|
756
|
+
this.log('debug', 'now ready to publish event');
|
|
539
757
|
publish();
|
|
540
758
|
}
|
|
541
759
|
});
|
|
542
760
|
}
|
|
543
761
|
else {
|
|
544
|
-
|
|
762
|
+
this.log('debug', 'is ready to publish event');
|
|
545
763
|
publish();
|
|
546
764
|
}
|
|
547
765
|
}
|