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
|
@@ -19,6 +19,10 @@ const enums_1 = require("./enums");
|
|
|
19
19
|
Rabbit MQ - RxJS Powered
|
|
20
20
|
*/
|
|
21
21
|
class RabbitMQClient {
|
|
22
|
+
log(level, ...args) {
|
|
23
|
+
const handler = this.logger[level] || (() => undefined);
|
|
24
|
+
handler(...args);
|
|
25
|
+
}
|
|
22
26
|
get onReady() {
|
|
23
27
|
return this.isReadyStream.asObservable().pipe((0, rxjs_1.filter)((state) => !!state), (0, rxjs_1.take)(1));
|
|
24
28
|
}
|
|
@@ -32,10 +36,12 @@ class RabbitMQClient {
|
|
|
32
36
|
return this.messagesStream.asObservable();
|
|
33
37
|
}
|
|
34
38
|
constructor(clientInitConfig) {
|
|
39
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
35
40
|
this.isReady = false;
|
|
36
41
|
this.isReadyStream = new rxjs_1.BehaviorSubject(this.isReady);
|
|
37
42
|
this.connectionErrorStream = new rxjs_1.Subject();
|
|
38
43
|
this.connectionCloseStream = new rxjs_1.Subject();
|
|
44
|
+
this.reconnectAttempts = 0;
|
|
39
45
|
/*
|
|
40
46
|
Handle all messages from all queues on this client
|
|
41
47
|
*/
|
|
@@ -46,26 +52,81 @@ class RabbitMQClient {
|
|
|
46
52
|
this.bindings = [];
|
|
47
53
|
this.DEFAULT_LISTENER_TYPE = '__default';
|
|
48
54
|
this.queueListeners = {};
|
|
49
|
-
this.
|
|
50
|
-
this.
|
|
55
|
+
this.registeredQueueConsumers = [];
|
|
56
|
+
this.metrics = {
|
|
57
|
+
messagesReceived: 0,
|
|
58
|
+
messagesPublished: 0,
|
|
59
|
+
messagesAcked: 0,
|
|
60
|
+
messagesNacked: 0,
|
|
61
|
+
messagesRetried: 0,
|
|
62
|
+
rpcRequests: 0,
|
|
63
|
+
rpcResponses: 0,
|
|
64
|
+
reconnects: 0,
|
|
65
|
+
};
|
|
66
|
+
this.queueToEventHandleMapping = new Map();
|
|
67
|
+
this.queueToEventCallbackMapping = new Map();
|
|
51
68
|
this.clientInitConfig = clientInitConfig;
|
|
69
|
+
this.metricsEnabled = !!clientInitConfig.metrics;
|
|
70
|
+
this.logger = {
|
|
71
|
+
debug: (_b = (_a = clientInitConfig.logger) === null || _a === void 0 ? void 0 : _a.debug) !== null && _b !== void 0 ? _b : (() => undefined),
|
|
72
|
+
info: (_d = (_c = clientInitConfig.logger) === null || _c === void 0 ? void 0 : _c.info) !== null && _d !== void 0 ? _d : console.info.bind(console),
|
|
73
|
+
warn: (_f = (_e = clientInitConfig.logger) === null || _e === void 0 ? void 0 : _e.warn) !== null && _f !== void 0 ? _f : console.warn.bind(console),
|
|
74
|
+
error: (_h = (_g = clientInitConfig.logger) === null || _g === void 0 ? void 0 : _g.error) !== null && _h !== void 0 ? _h : console.error.bind(console),
|
|
75
|
+
};
|
|
52
76
|
!clientInitConfig.stopAutoInit && this.init();
|
|
53
77
|
}
|
|
78
|
+
static registerSerializer(contentType, serializer) {
|
|
79
|
+
return (0, utils_1.registerSerializer)(contentType, serializer);
|
|
80
|
+
}
|
|
81
|
+
getMetrics() {
|
|
82
|
+
return Object.assign({}, this.metrics);
|
|
83
|
+
}
|
|
84
|
+
resubscribeToRegisteredQueues() {
|
|
85
|
+
for (const { queue, options } of this.registeredQueueConsumers) {
|
|
86
|
+
if (this.queueListeners[queue]) {
|
|
87
|
+
this.queueListeners[queue].unsubscribe();
|
|
88
|
+
delete this.queueListeners[queue];
|
|
89
|
+
}
|
|
90
|
+
this.listenToQueue(queue, options);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
scheduleReconnect(reason) {
|
|
94
|
+
if (this.reconnectTimeout) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const retryAttempts = this.clientInitConfig.retryAttempts || 0;
|
|
98
|
+
if (retryAttempts > 0 && this.reconnectAttempts >= retryAttempts) {
|
|
99
|
+
this.log('error', `RabbitMQ reconnect attempts exhausted after ${retryAttempts} tries.`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.isReady = false;
|
|
103
|
+
this.isReadyStream.next(false);
|
|
104
|
+
this.reconnectAttempts += 1;
|
|
105
|
+
this.metrics.reconnects += 1;
|
|
106
|
+
const retryDelay = this.clientInitConfig.retryDelay || 1000;
|
|
107
|
+
this.log('warn', `${reason}; reconnecting in ${retryDelay}ms (attempt ${this.reconnectAttempts}/${retryAttempts || '∞'})`);
|
|
108
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
109
|
+
this.reconnectTimeout = undefined;
|
|
110
|
+
this.init().catch((error) => {
|
|
111
|
+
this.log('error', 'Reconnect failed', error);
|
|
112
|
+
});
|
|
113
|
+
}, retryDelay);
|
|
114
|
+
}
|
|
54
115
|
init() {
|
|
55
116
|
if (this.isReady) {
|
|
56
|
-
|
|
57
|
-
return;
|
|
117
|
+
this.log('info', 'Already initialized');
|
|
118
|
+
return Promise.resolve();
|
|
58
119
|
}
|
|
59
120
|
const { delayStart, connection_url, prefetch, queues, exchanges, bindings, pre_init_promises, post_init_promises } = this.clientInitConfig;
|
|
60
121
|
let retryAttempts = this.clientInitConfig.retryAttempts || 0;
|
|
61
122
|
const retryDelay = this.clientInitConfig.retryDelay || 0;
|
|
62
123
|
const init = () => {
|
|
63
124
|
// creating connection
|
|
64
|
-
|
|
125
|
+
this.log('info', 'Attempting connection to Rabbit MQ...');
|
|
65
126
|
return (0, amqplib_1.connect)(connection_url)
|
|
66
127
|
.then((connection) => {
|
|
67
128
|
this.connection = connection;
|
|
68
|
-
|
|
129
|
+
this.log('info', 'Connected to message server');
|
|
69
130
|
return this.connection.createChannel();
|
|
70
131
|
})
|
|
71
132
|
// creating channel
|
|
@@ -74,33 +135,33 @@ class RabbitMQClient {
|
|
|
74
135
|
if (prefetch) {
|
|
75
136
|
this.channel.prefetch(prefetch || 1);
|
|
76
137
|
}
|
|
77
|
-
|
|
138
|
+
this.log('info', 'Channel created on message server');
|
|
78
139
|
})
|
|
79
140
|
// assert to queues
|
|
80
141
|
.then(() => {
|
|
81
142
|
const promises = [];
|
|
82
143
|
for (const queueConfig of queues) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
144
|
+
const queueListenersMap = new Map();
|
|
145
|
+
const queueCallbacksMap = new Map();
|
|
146
|
+
this.queueToEventHandleMapping.set(queueConfig.name, queueListenersMap);
|
|
147
|
+
this.queueToEventCallbackMapping.set(queueConfig.name, queueCallbacksMap);
|
|
87
148
|
// by default, create an observable stream on the queue for unidentified/null routing keys
|
|
88
|
-
queueListenersMap
|
|
149
|
+
queueListenersMap.set(this.DEFAULT_LISTENER_TYPE, new rxjs_1.ReplaySubject());
|
|
89
150
|
const useHandleMessageTypes = queueConfig.handleMessageTypes || [];
|
|
90
151
|
for (const messageType of useHandleMessageTypes) {
|
|
91
152
|
if (typeof messageType === 'string') {
|
|
92
|
-
queueListenersMap
|
|
153
|
+
queueListenersMap.set(messageType, new rxjs_1.ReplaySubject());
|
|
93
154
|
}
|
|
94
155
|
else {
|
|
95
156
|
const config = messageType;
|
|
96
|
-
queueCallbacksMap
|
|
157
|
+
queueCallbacksMap.set(config.messageName, config.callbackHandler);
|
|
97
158
|
}
|
|
98
159
|
}
|
|
99
160
|
promises.push(this.channel.assertQueue(queueConfig.name, queueConfig.options));
|
|
100
161
|
}
|
|
101
162
|
return Promise.all(promises).then((values) => {
|
|
102
163
|
this.queues = (0, utils_1.mapify)(values, 'queue');
|
|
103
|
-
|
|
164
|
+
this.log('info', 'queues created on channel');
|
|
104
165
|
});
|
|
105
166
|
})
|
|
106
167
|
// assert exchanges
|
|
@@ -108,7 +169,7 @@ class RabbitMQClient {
|
|
|
108
169
|
const promises = exchanges.map((config) => this.channel.assertExchange(config.name, config.type, config.options));
|
|
109
170
|
return Promise.all(promises).then(values => {
|
|
110
171
|
this.exchanges = (0, utils_1.mapify)(values, 'exchange');
|
|
111
|
-
|
|
172
|
+
this.log('info', 'exchanges created on channel');
|
|
112
173
|
});
|
|
113
174
|
})
|
|
114
175
|
// apply bindings
|
|
@@ -117,26 +178,32 @@ class RabbitMQClient {
|
|
|
117
178
|
// bind the exclusive queue to all the exchanges
|
|
118
179
|
// const exclusiveBindings = bindings.map((config) => this.channel.bindQueue(this.EXCLUSIVE_QUEUE, config.exchange, config.routingKey));
|
|
119
180
|
return Promise.all(promises).then(values => {
|
|
120
|
-
|
|
181
|
+
this.log('info', 'bindings created on channel');
|
|
121
182
|
});
|
|
122
183
|
})
|
|
123
184
|
.then(() => {
|
|
124
|
-
|
|
185
|
+
this.log('info', 'Client initialization complete; waiting for messages/events...');
|
|
125
186
|
// initialization complete; listen for connection issues to retry again
|
|
126
187
|
retryAttempts = this.clientInitConfig.retryAttempts;
|
|
127
188
|
this.connection.on("error", (err) => {
|
|
128
189
|
this.connectionErrorStream.next(err);
|
|
190
|
+
this.log('error', 'RabbitMQ connection error', err);
|
|
191
|
+
this.scheduleReconnect('RabbitMQ connection error');
|
|
129
192
|
});
|
|
130
193
|
this.connection.on("close", (err) => {
|
|
131
194
|
this.connectionCloseStream.next(err);
|
|
195
|
+
this.log('warn', 'RabbitMQ connection closed', err);
|
|
196
|
+
this.scheduleReconnect('RabbitMQ connection closed');
|
|
132
197
|
});
|
|
198
|
+
this.reconnectAttempts = 0;
|
|
133
199
|
this.isReady = true;
|
|
134
200
|
this.isReadyStream.next(true);
|
|
201
|
+
this.resubscribeToRegisteredQueues();
|
|
135
202
|
})
|
|
136
203
|
.catch((error) => {
|
|
137
|
-
|
|
204
|
+
this.log('error', 'connection error, retryAttempts:', retryAttempts, error);
|
|
138
205
|
if (retryAttempts === 0) {
|
|
139
|
-
|
|
206
|
+
this.log('error', 'all retry attemptys exhaused; exiting...');
|
|
140
207
|
throw error;
|
|
141
208
|
}
|
|
142
209
|
retryAttempts = retryAttempts - 1;
|
|
@@ -145,16 +212,16 @@ class RabbitMQClient {
|
|
|
145
212
|
};
|
|
146
213
|
return (0, utils_1.wait)(delayStart || 0)
|
|
147
214
|
.then(() => {
|
|
148
|
-
|
|
215
|
+
this.log('info', 'Running pre init promises...');
|
|
149
216
|
return Promise.all((pre_init_promises === null || pre_init_promises === void 0 ? void 0 : pre_init_promises.map(p => p instanceof Promise ? p : p())) || []).then(() => {
|
|
150
|
-
|
|
217
|
+
this.log('info', 'Done running pre init promises.');
|
|
151
218
|
});
|
|
152
219
|
})
|
|
153
220
|
.then(init)
|
|
154
221
|
.then(() => {
|
|
155
|
-
|
|
222
|
+
this.log('info', 'Running post init promises...');
|
|
156
223
|
return Promise.all((post_init_promises === null || post_init_promises === void 0 ? void 0 : post_init_promises.map(p => p instanceof Promise ? p : p())) || []).then(() => {
|
|
157
|
-
|
|
224
|
+
this.log('info', 'Done running post init promises.');
|
|
158
225
|
});
|
|
159
226
|
});
|
|
160
227
|
}
|
|
@@ -168,6 +235,7 @@ class RabbitMQClient {
|
|
|
168
235
|
return () => {
|
|
169
236
|
// callback
|
|
170
237
|
const handleCallback = (message) => {
|
|
238
|
+
var _a, _b;
|
|
171
239
|
if (!message) {
|
|
172
240
|
// console.log('Consumer cancelled by server');
|
|
173
241
|
// throw new Error('Consumer cancelled by server');
|
|
@@ -177,7 +245,11 @@ class RabbitMQClient {
|
|
|
177
245
|
// see if a listener was created for the routing key
|
|
178
246
|
const messageType = message.properties.type;
|
|
179
247
|
const useContentType = message.properties.contentType;
|
|
180
|
-
const
|
|
248
|
+
const serializer = (0, utils_1.getSerializer)(useContentType || 'application/octet-stream');
|
|
249
|
+
const useData = serializer ? serializer.deserialize(message.content) : message.content;
|
|
250
|
+
if (this.metricsEnabled) {
|
|
251
|
+
this.metrics.messagesReceived += 1;
|
|
252
|
+
}
|
|
181
253
|
const messageObj = {
|
|
182
254
|
data: useData,
|
|
183
255
|
message,
|
|
@@ -193,33 +265,38 @@ class RabbitMQClient {
|
|
|
193
265
|
// send message to queue stream
|
|
194
266
|
!!this.messagesStreamsByQueue[queue] && this.messagesStreamsByQueue[queue].next(messageObj);
|
|
195
267
|
// console.log(`Message on queue ${queue}:`, messageObj);
|
|
268
|
+
const queueListeners = (_a = this.queueToEventHandleMapping.get(queue)) !== null && _a !== void 0 ? _a : new Map();
|
|
269
|
+
const queueCallbacks = (_b = this.queueToEventCallbackMapping.get(queue)) !== null && _b !== void 0 ? _b : new Map();
|
|
196
270
|
if (!messageType) {
|
|
197
271
|
// no type key found, push to default stream
|
|
198
|
-
|
|
199
|
-
|
|
272
|
+
this.log('debug', `No message type found; pushing to default handler on queue ${queue}`);
|
|
273
|
+
const defaultListener = queueListeners.get(this.DEFAULT_LISTENER_TYPE);
|
|
274
|
+
defaultListener === null || defaultListener === void 0 ? void 0 : defaultListener.next(messageObj);
|
|
200
275
|
return;
|
|
201
276
|
}
|
|
202
|
-
|
|
277
|
+
const typeListener = queueListeners.get(messageType);
|
|
278
|
+
if (typeListener) {
|
|
203
279
|
// there is a registered listener for the type, push to that stream
|
|
204
|
-
|
|
205
|
-
|
|
280
|
+
this.log('debug', `message type handler found; pushing to ${messageType} handler on queue ${queue}`);
|
|
281
|
+
typeListener.next(messageObj);
|
|
206
282
|
}
|
|
207
|
-
else if (
|
|
283
|
+
else if (queueCallbacks.has(messageType)) {
|
|
208
284
|
// there is a registered listener for the type, push to that stream
|
|
209
|
-
|
|
210
|
-
const callbackHandler =
|
|
285
|
+
this.log('debug', `message type callback found; pushing to ${messageType} callback on queue ${queue}`);
|
|
286
|
+
const callbackHandler = queueCallbacks.get(messageType);
|
|
211
287
|
callbackHandler(messageObj);
|
|
212
288
|
}
|
|
213
289
|
else {
|
|
214
290
|
// no registered listener
|
|
215
291
|
if (this.clientInitConfig.autoAckUnhandledMessageTypes) {
|
|
216
|
-
|
|
292
|
+
this.log('debug', `No handler found for message type ${messageType} on queue ${queue}; auto acknowledging.`);
|
|
217
293
|
this.ack(message);
|
|
218
294
|
return;
|
|
219
295
|
}
|
|
220
296
|
else if (this.clientInitConfig.pushUnhandledMessageTypesToDefaultHandler) {
|
|
221
|
-
|
|
222
|
-
|
|
297
|
+
this.log('debug', `No handler found for message type ${messageType} on queue ${queue}; pushing to ${this.DEFAULT_LISTENER_TYPE} handler.`);
|
|
298
|
+
const defaultListener = queueListeners.get(this.DEFAULT_LISTENER_TYPE);
|
|
299
|
+
defaultListener === null || defaultListener === void 0 ? void 0 : defaultListener.next(messageObj);
|
|
223
300
|
return;
|
|
224
301
|
}
|
|
225
302
|
else {
|
|
@@ -233,10 +310,12 @@ class RabbitMQClient {
|
|
|
233
310
|
}
|
|
234
311
|
listenToQueue(queue, options) {
|
|
235
312
|
if (this.queueListeners[queue]) {
|
|
236
|
-
|
|
313
|
+
this.log('debug', `Already Registered messages/events listener for queue: ${queue}`);
|
|
237
314
|
return;
|
|
238
315
|
}
|
|
239
|
-
|
|
316
|
+
this.registeredQueueConsumers = this.registeredQueueConsumers.filter((entry) => entry.queue !== queue);
|
|
317
|
+
this.registeredQueueConsumers.push({ queue, options });
|
|
318
|
+
this.log('info', `Registering messages/events listener for queue: ${queue}`);
|
|
240
319
|
const startQueueListener = this.getQueueListener(queue, options);
|
|
241
320
|
this.messagesStreamsByQueue[queue] = new rxjs_1.Subject();
|
|
242
321
|
this.queueListeners[queue] = this.onReady.pipe((0, rxjs_1.tap)((readyState) => {
|
|
@@ -244,7 +323,7 @@ class RabbitMQClient {
|
|
|
244
323
|
}))
|
|
245
324
|
.subscribe({
|
|
246
325
|
next: (readyState) => {
|
|
247
|
-
|
|
326
|
+
this.log('info', `Registered: Now listening to messages/events on queue ${queue}...`);
|
|
248
327
|
}
|
|
249
328
|
});
|
|
250
329
|
}
|
|
@@ -253,14 +332,19 @@ class RabbitMQClient {
|
|
|
253
332
|
this.listenToQueue(queue, options);
|
|
254
333
|
const handle = (messageType) => {
|
|
255
334
|
return this.onReady.pipe((0, rxjs_1.mergeMap)((ready, index) => {
|
|
256
|
-
|
|
335
|
+
const queueListeners = this.queueToEventHandleMapping.get(queue);
|
|
336
|
+
if (!(queueListeners === null || queueListeners === void 0 ? void 0 : queueListeners.has(messageType))) {
|
|
257
337
|
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.`);
|
|
258
338
|
}
|
|
259
|
-
return
|
|
339
|
+
return queueListeners.get(messageType).asObservable();
|
|
260
340
|
}));
|
|
261
341
|
};
|
|
262
342
|
const handleDefault = () => {
|
|
263
|
-
return this.onReady.pipe((0, rxjs_1.mergeMap)((ready, index) =>
|
|
343
|
+
return this.onReady.pipe((0, rxjs_1.mergeMap)((ready, index) => {
|
|
344
|
+
var _a;
|
|
345
|
+
const queueListeners = this.queueToEventHandleMapping.get(queue);
|
|
346
|
+
return ((_a = queueListeners === null || queueListeners === void 0 ? void 0 : queueListeners.get(this.DEFAULT_LISTENER_TYPE)) !== null && _a !== void 0 ? _a : new rxjs_1.ReplaySubject()).asObservable();
|
|
347
|
+
}));
|
|
264
348
|
};
|
|
265
349
|
/**
|
|
266
350
|
Providing callback function approach
|
|
@@ -289,51 +373,193 @@ class RabbitMQClient {
|
|
|
289
373
|
}
|
|
290
374
|
ack(message) {
|
|
291
375
|
this.channel.ack(message);
|
|
292
|
-
|
|
376
|
+
if (this.metricsEnabled) {
|
|
377
|
+
this.metrics.messagesAcked += 1;
|
|
378
|
+
}
|
|
379
|
+
this.log('debug', 'Acknoledged rmq message.');
|
|
380
|
+
}
|
|
381
|
+
nack(message, requeue = true) {
|
|
382
|
+
if (!this.channel) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
this.channel.nack(message, false, requeue);
|
|
386
|
+
if (this.metricsEnabled) {
|
|
387
|
+
this.metrics.messagesNacked += 1;
|
|
388
|
+
}
|
|
389
|
+
this.log('debug', `Nacknowledged rmq message with requeue=${requeue}.`);
|
|
390
|
+
}
|
|
391
|
+
retryMessage(message, options = {}) {
|
|
392
|
+
const { delayMs = 0, requeue = true, queue = '' } = options;
|
|
393
|
+
const targetQueue = queue || message.fields.routingKey || '';
|
|
394
|
+
if (delayMs > 0) {
|
|
395
|
+
setTimeout(() => {
|
|
396
|
+
if (targetQueue) {
|
|
397
|
+
this.channel.sendToQueue(targetQueue, message.content, Object.assign({ persistent: true }, message.properties));
|
|
398
|
+
}
|
|
399
|
+
}, delayMs);
|
|
400
|
+
}
|
|
401
|
+
else if (targetQueue) {
|
|
402
|
+
this.channel.sendToQueue(targetQueue, message.content, Object.assign({ persistent: true }, message.properties));
|
|
403
|
+
}
|
|
404
|
+
this.nack(message, requeue);
|
|
405
|
+
if (this.metricsEnabled) {
|
|
406
|
+
this.metrics.messagesRetried += 1;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
deadLetter(message, reason, queue) {
|
|
410
|
+
const targetQueue = queue || `${message.fields.routingKey || 'dead-letter'}.dlq`;
|
|
411
|
+
const headers = Object.assign(Object.assign({}, (message.properties.headers || {})), { 'x-death-reason': reason });
|
|
412
|
+
this.channel.sendToQueue(targetQueue, message.content, Object.assign(Object.assign({}, message.properties), { headers, persistent: true }));
|
|
413
|
+
this.nack(message, false);
|
|
414
|
+
}
|
|
415
|
+
setupDeadLetterQueue(config) {
|
|
416
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
417
|
+
const { sourceQueue, deadLetterQueue = `${sourceQueue}.dlq`, deadLetterExchange = `${sourceQueue}.dlx`, deadLetterRoutingKey = deadLetterQueue, queueOptions = {}, deadLetterQueueOptions = { durable: true }, maxLength, messageTtlMs, } = config;
|
|
418
|
+
const dlxArguments = Object.assign(Object.assign({}, (maxLength ? { 'x-max-length': maxLength } : {})), (messageTtlMs ? { 'x-message-ttl': messageTtlMs } : {}));
|
|
419
|
+
yield this.channel.assertExchange(deadLetterExchange, 'direct', { durable: true });
|
|
420
|
+
yield this.channel.assertQueue(deadLetterQueue, Object.assign(Object.assign({ durable: true }, deadLetterQueueOptions), { arguments: Object.assign(Object.assign({}, (deadLetterQueueOptions.arguments || {})), dlxArguments) }));
|
|
421
|
+
yield this.channel.bindQueue(deadLetterQueue, deadLetterExchange, deadLetterRoutingKey);
|
|
422
|
+
const sourceQueueArguments = Object.assign(Object.assign({}, (queueOptions.arguments || {})), { 'x-dead-letter-exchange': deadLetterExchange, 'x-dead-letter-routing-key': deadLetterRoutingKey });
|
|
423
|
+
yield this.channel.assertQueue(sourceQueue, Object.assign(Object.assign({}, queueOptions), { arguments: sourceQueueArguments }));
|
|
424
|
+
return {
|
|
425
|
+
sourceQueue,
|
|
426
|
+
deadLetterQueue,
|
|
427
|
+
deadLetterExchange,
|
|
428
|
+
deadLetterRoutingKey,
|
|
429
|
+
};
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
handleMessageError(message, error, options = {}) {
|
|
433
|
+
var _a, _b;
|
|
434
|
+
const { reason = error instanceof Error ? error.message : 'unknown_error', requeue = false, deadLetterQueue, deadLetterExchange, deadLetterRoutingKey, retryQueue, retryDelayMs, loggerMessage = 'RabbitMQ message processing failed', } = options;
|
|
435
|
+
this.log('error', loggerMessage, {
|
|
436
|
+
reason,
|
|
437
|
+
error,
|
|
438
|
+
routingKey: (_a = message === null || message === void 0 ? void 0 : message.fields) === null || _a === void 0 ? void 0 : _a.routingKey,
|
|
439
|
+
queue: (_b = message === null || message === void 0 ? void 0 : message.fields) === null || _b === void 0 ? void 0 : _b.routingKey,
|
|
440
|
+
});
|
|
441
|
+
if (retryQueue && retryDelayMs && retryDelayMs > 0) {
|
|
442
|
+
const targetQueue = retryQueue;
|
|
443
|
+
setTimeout(() => {
|
|
444
|
+
if (this.channel) {
|
|
445
|
+
this.channel.sendToQueue(targetQueue, message.content, Object.assign(Object.assign({}, message.properties), { persistent: true }));
|
|
446
|
+
}
|
|
447
|
+
}, retryDelayMs);
|
|
448
|
+
this.nack(message, false);
|
|
449
|
+
return { action: 'retry', targetQueue, retryDelayMs };
|
|
450
|
+
}
|
|
451
|
+
if (deadLetterQueue || deadLetterExchange || deadLetterRoutingKey) {
|
|
452
|
+
const targetQueue = deadLetterQueue || `${message.fields.routingKey || 'dead-letter'}.dlq`;
|
|
453
|
+
this.deadLetter(message, reason, targetQueue);
|
|
454
|
+
return { action: 'dead-letter', targetQueue };
|
|
455
|
+
}
|
|
456
|
+
this.nack(message, requeue);
|
|
457
|
+
return { action: 'nack', requeue };
|
|
458
|
+
}
|
|
459
|
+
close() {
|
|
460
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
461
|
+
if (this.reconnectTimeout) {
|
|
462
|
+
clearTimeout(this.reconnectTimeout);
|
|
463
|
+
this.reconnectTimeout = undefined;
|
|
464
|
+
}
|
|
465
|
+
if (this.channel) {
|
|
466
|
+
yield this.channel.close();
|
|
467
|
+
}
|
|
468
|
+
if (this.connection) {
|
|
469
|
+
yield this.connection.close();
|
|
470
|
+
}
|
|
471
|
+
this.isReady = false;
|
|
472
|
+
this.isReadyStream.next(false);
|
|
473
|
+
this.log('info', 'RabbitMQ client closed.');
|
|
474
|
+
});
|
|
293
475
|
}
|
|
294
476
|
sendMessage(options) {
|
|
295
477
|
const send = () => {
|
|
296
478
|
const { data, publishOptions, queue } = options;
|
|
297
479
|
const useContentType = publishOptions.contentType || enums_1.ContentTypes.TEXT;
|
|
298
|
-
const
|
|
480
|
+
const serializer = (0, utils_1.getSerializer)(useContentType) || utils_1.SERIALIZERS[useContentType];
|
|
481
|
+
const useData = serializer
|
|
482
|
+
? serializer.serialize(data)
|
|
483
|
+
: Buffer.from(typeof data === 'string' ? data : JSON.stringify(data), 'utf8');
|
|
484
|
+
if (this.metricsEnabled) {
|
|
485
|
+
this.metrics.messagesPublished += 1;
|
|
486
|
+
}
|
|
299
487
|
this.channel.sendToQueue(queue, useData, Object.assign(Object.assign({}, publishOptions), { appId: publishOptions.appId }));
|
|
300
488
|
};
|
|
301
489
|
if (!this.isReady) {
|
|
302
|
-
|
|
490
|
+
this.log('debug', 'wait until ready to send message...');
|
|
303
491
|
(0, rxjs_1.firstValueFrom)(this.onReady).then((readyState) => {
|
|
304
|
-
|
|
492
|
+
this.log('debug', 'now ready to send message', { readyState });
|
|
305
493
|
send();
|
|
306
494
|
});
|
|
307
495
|
}
|
|
308
496
|
else {
|
|
309
|
-
|
|
497
|
+
this.log('debug', 'is ready to send message');
|
|
310
498
|
send();
|
|
311
499
|
}
|
|
312
500
|
}
|
|
313
|
-
getRpcMethods(temporaryRpcQueue, options, autoAckOnReceiveReply) {
|
|
501
|
+
getRpcMethods(temporaryRpcQueue, options, autoAckOnReceiveReply, requestTimeoutMs) {
|
|
502
|
+
var _a;
|
|
314
503
|
const start_time = Date.now();
|
|
315
504
|
const consumerTag = (0, utils_1.uniqueValue)();
|
|
316
505
|
const correlationId = options.publishOptions.correlationId || (0, uuid_1.v4)();
|
|
506
|
+
const timeoutMs = (_a = requestTimeoutMs !== null && requestTimeoutMs !== void 0 ? requestTimeoutMs : this.clientInitConfig.rpcRequestTimeoutMs) !== null && _a !== void 0 ? _a : 30000;
|
|
317
507
|
const send = () => {
|
|
318
508
|
const { data, publishOptions, queue } = options;
|
|
319
509
|
const useContentType = publishOptions.contentType || enums_1.ContentTypes.TEXT;
|
|
320
|
-
const
|
|
510
|
+
const serializer = (0, utils_1.getSerializer)(useContentType) || utils_1.SERIALIZERS[useContentType];
|
|
511
|
+
const useData = serializer
|
|
512
|
+
? serializer.serialize(data)
|
|
513
|
+
: Buffer.from(typeof data === 'string' ? data : JSON.stringify(data), 'utf8');
|
|
514
|
+
if (this.metricsEnabled) {
|
|
515
|
+
this.metrics.rpcRequests += 1;
|
|
516
|
+
}
|
|
321
517
|
this.channel.sendToQueue(queue, useData, Object.assign(Object.assign({}, publishOptions), { correlationId, replyTo: temporaryRpcQueue, appId: publishOptions.appId }));
|
|
322
518
|
};
|
|
323
519
|
const watch = () => {
|
|
324
520
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
325
|
-
|
|
521
|
+
let settled = false;
|
|
522
|
+
let timeoutHandle;
|
|
523
|
+
const cleanup = () => __awaiter(this, void 0, void 0, function* () {
|
|
524
|
+
if (timeoutHandle) {
|
|
525
|
+
clearTimeout(timeoutHandle);
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
yield this.channel.cancel(consumerTag);
|
|
529
|
+
}
|
|
530
|
+
catch (error) {
|
|
531
|
+
this.log('debug', 'Ignoring cancel failure for RPC consumer', { consumerTag, error });
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
timeoutHandle = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
535
|
+
if (settled) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
settled = true;
|
|
539
|
+
yield cleanup();
|
|
540
|
+
reject(new Error(`RabbitMQ request timed out after ${timeoutMs}ms for correlationId ${correlationId}`));
|
|
541
|
+
}), timeoutMs);
|
|
542
|
+
this.log('debug', 'request/rpc sent; awaiting response...', { correlationId, consumerTag, temporaryRpcQueue });
|
|
326
543
|
const replyHandler = (message) => {
|
|
327
|
-
|
|
544
|
+
if (!message || settled) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
this.log('debug', 'Received reply.', { message });
|
|
328
548
|
const correlationIdMatch = message.properties.correlationId == correlationId;
|
|
329
549
|
const isReplyToRequest = !!message && correlationIdMatch;
|
|
330
|
-
|
|
550
|
+
this.log('debug', { isReplyToRequest, consumerTag, correlationIdMatch, requestCorrelationId: correlationId, responseCorrelationId: message.properties.correlationId });
|
|
331
551
|
if (isReplyToRequest) {
|
|
552
|
+
settled = true;
|
|
553
|
+
void cleanup();
|
|
332
554
|
const useContentType = message.properties.contentType;
|
|
333
|
-
const
|
|
555
|
+
const serializer = (0, utils_1.getSerializer)(useContentType || 'application/octet-stream') || utils_1.SERIALIZERS[useContentType || 'application/octet-stream'];
|
|
556
|
+
const useData = serializer ? serializer.deserialize(message.content) : message.content;
|
|
557
|
+
if (this.metricsEnabled) {
|
|
558
|
+
this.metrics.rpcResponses += 1;
|
|
559
|
+
}
|
|
334
560
|
const messageObj = {
|
|
335
561
|
data: useData,
|
|
336
|
-
message
|
|
562
|
+
message,
|
|
337
563
|
ack: () => {
|
|
338
564
|
this.ack(message);
|
|
339
565
|
},
|
|
@@ -344,7 +570,7 @@ class RabbitMQClient {
|
|
|
344
570
|
const end_time = Date.now();
|
|
345
571
|
const total_time = (end_time - start_time) / 1000;
|
|
346
572
|
const time_in_seconds = total_time.toFixed();
|
|
347
|
-
|
|
573
|
+
this.log('debug', 'received response from request/rpc:', { start_time, end_time, total_time, time_in_seconds, messageObj, options });
|
|
348
574
|
if (autoAckOnReceiveReply) {
|
|
349
575
|
this.ack(message);
|
|
350
576
|
}
|
|
@@ -356,32 +582,32 @@ class RabbitMQClient {
|
|
|
356
582
|
};
|
|
357
583
|
return { watch, send };
|
|
358
584
|
}
|
|
359
|
-
sendRequest(options, autoAckOnReceiveReply = true) {
|
|
360
|
-
|
|
585
|
+
sendRequest(options, autoAckOnReceiveReply = true, timeoutMs) {
|
|
586
|
+
this.log('debug', 'sendRequest:', options);
|
|
361
587
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
362
588
|
const temporaryRpcQueue = (0, uuid_1.v1)();
|
|
363
589
|
const newQueue = yield this.channel.assertQueue(temporaryRpcQueue, { exclusive: true, durable: false, autoDelete: true }).then((response) => {
|
|
364
|
-
|
|
590
|
+
this.log('debug', `Created temp queue ${temporaryRpcQueue} for client.`);
|
|
365
591
|
return response;
|
|
366
592
|
});
|
|
367
|
-
const rpc = this.getRpcMethods(temporaryRpcQueue, options, autoAckOnReceiveReply);
|
|
593
|
+
const rpc = this.getRpcMethods(temporaryRpcQueue, options, autoAckOnReceiveReply, timeoutMs);
|
|
368
594
|
if (!this.isReady) {
|
|
369
|
-
|
|
595
|
+
this.log('debug', 'waiting until ready to send request');
|
|
370
596
|
(0, rxjs_1.firstValueFrom)(this.onReady).then((readyState) => {
|
|
371
|
-
|
|
597
|
+
this.log('debug', 'now ready and watching temp queue', { readyState });
|
|
372
598
|
rpc.watch().then((message) => {
|
|
373
599
|
resolve(message);
|
|
374
|
-
}); // first listen on the reply queue
|
|
375
|
-
|
|
600
|
+
}).catch(reject); // first listen on the reply queue
|
|
601
|
+
this.log('debug', 'sending request');
|
|
376
602
|
rpc.send(); // then send the rpc/rmq message
|
|
377
|
-
});
|
|
603
|
+
}).catch(reject);
|
|
378
604
|
}
|
|
379
605
|
else {
|
|
380
|
-
|
|
606
|
+
this.log('debug', 'now ready and watching temp queue');
|
|
381
607
|
rpc.watch().then((message) => {
|
|
382
608
|
resolve(message);
|
|
383
|
-
}); // first listen on the reply queue
|
|
384
|
-
|
|
609
|
+
}).catch(reject); // first listen on the reply queue
|
|
610
|
+
this.log('debug', 'sending request');
|
|
385
611
|
rpc.send(); // then send the rpc/rmq message
|
|
386
612
|
}
|
|
387
613
|
}));
|
|
@@ -390,20 +616,26 @@ class RabbitMQClient {
|
|
|
390
616
|
const publish = () => {
|
|
391
617
|
const { data, routingKey, publishOptions, exchange } = options;
|
|
392
618
|
const useContentType = publishOptions.contentType || enums_1.ContentTypes.TEXT;
|
|
393
|
-
const
|
|
619
|
+
const serializer = (0, utils_1.getSerializer)(useContentType) || utils_1.SERIALIZERS[useContentType];
|
|
620
|
+
const useData = serializer
|
|
621
|
+
? serializer.serialize(data)
|
|
622
|
+
: Buffer.from(typeof data === 'string' ? data : JSON.stringify(data), 'utf8');
|
|
623
|
+
if (this.metricsEnabled) {
|
|
624
|
+
this.metrics.messagesPublished += 1;
|
|
625
|
+
}
|
|
394
626
|
this.channel.publish(exchange, routingKey || '', useData, Object.assign(Object.assign({}, publishOptions), { appId: publishOptions.appId }));
|
|
395
627
|
};
|
|
396
628
|
if (!this.isReady) {
|
|
397
|
-
|
|
629
|
+
this.log('debug', 'wait until ready to publish event');
|
|
398
630
|
this.onReady.subscribe({
|
|
399
631
|
next: () => {
|
|
400
|
-
|
|
632
|
+
this.log('debug', 'now ready to publish event');
|
|
401
633
|
publish();
|
|
402
634
|
}
|
|
403
635
|
});
|
|
404
636
|
}
|
|
405
637
|
else {
|
|
406
|
-
|
|
638
|
+
this.log('debug', 'is ready to publish event');
|
|
407
639
|
publish();
|
|
408
640
|
}
|
|
409
641
|
}
|