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