core-services-sdk 1.3.35 → 1.3.36
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/package.json +1 -1
- package/src/rabbit-mq/rabbit-mq.js +42 -8
package/package.json
CHANGED
|
@@ -46,7 +46,7 @@ export const connectQueueService = async ({ host, log }) => {
|
|
|
46
46
|
* Creates a channel from a RabbitMQ connection.
|
|
47
47
|
*
|
|
48
48
|
* @param {{ host: string, log: import('pino').Logger }} options
|
|
49
|
-
* @returns {Promise<amqp.Channel>}
|
|
49
|
+
* @returns {Promise<{ channel: amqp.Channel, connection: amqp.Connection }>}
|
|
50
50
|
*/
|
|
51
51
|
export const createChannel = async ({ host, log }) => {
|
|
52
52
|
const t0 = Date.now()
|
|
@@ -65,7 +65,7 @@ export const createChannel = async ({ host, log }) => {
|
|
|
65
65
|
ms: Date.now() - t0,
|
|
66
66
|
})
|
|
67
67
|
|
|
68
|
-
return channel
|
|
68
|
+
return { channel, connection }
|
|
69
69
|
} catch (err) {
|
|
70
70
|
logger.error(err, {
|
|
71
71
|
event: 'error',
|
|
@@ -96,7 +96,7 @@ const parseMessage = (msgInfo) => {
|
|
|
96
96
|
* @param {boolean} [options.nackOnError=false] - Whether to nack the message on error (default: false)
|
|
97
97
|
* @param {number} [options.prefetch=1] - Max unacked messages per consumer (default: 1)
|
|
98
98
|
*
|
|
99
|
-
* @returns {Promise<
|
|
99
|
+
* @returns {Promise<string>} Returns the consumer tag for later cancellation
|
|
100
100
|
*/
|
|
101
101
|
export const subscribeToQueue = async ({
|
|
102
102
|
log,
|
|
@@ -112,7 +112,7 @@ export const subscribeToQueue = async ({
|
|
|
112
112
|
await channel.assertQueue(queue, { durable: true })
|
|
113
113
|
!!prefetch && (await channel.prefetch(prefetch))
|
|
114
114
|
|
|
115
|
-
channel.consume(queue, async (msgInfo) => {
|
|
115
|
+
const { consumerTag } = await channel.consume(queue, async (msgInfo) => {
|
|
116
116
|
if (!msgInfo) {
|
|
117
117
|
return
|
|
118
118
|
}
|
|
@@ -143,6 +143,9 @@ export const subscribeToQueue = async ({
|
|
|
143
143
|
return
|
|
144
144
|
}
|
|
145
145
|
})
|
|
146
|
+
|
|
147
|
+
logger.debug({ consumerTag }, 'consumer-started')
|
|
148
|
+
return consumerTag
|
|
146
149
|
} catch (err) {
|
|
147
150
|
logger.error(err, {
|
|
148
151
|
event: 'error',
|
|
@@ -164,12 +167,14 @@ export const subscribeToQueue = async ({
|
|
|
164
167
|
* queue: string,
|
|
165
168
|
* onReceive: (data: any, correlationId?: string) => Promise<void>,
|
|
166
169
|
* nackOnError?: boolean
|
|
167
|
-
* }) => Promise<
|
|
168
|
-
* channel: amqp.Channel
|
|
170
|
+
* }) => Promise<string>,
|
|
171
|
+
* channel: amqp.Channel,
|
|
172
|
+
* connection: amqp.Connection,
|
|
173
|
+
* close: () => Promise<void>
|
|
169
174
|
* }>}
|
|
170
175
|
*/
|
|
171
176
|
export const initializeQueue = async ({ host, log }) => {
|
|
172
|
-
const channel = await createChannel({ host, log })
|
|
177
|
+
const { channel, connection } = await createChannel({ host, log })
|
|
173
178
|
const logger = log.child({ op: 'initializeQueue', host: mask(host) })
|
|
174
179
|
|
|
175
180
|
/**
|
|
@@ -224,16 +229,45 @@ export const initializeQueue = async ({ host, log }) => {
|
|
|
224
229
|
* onReceive: (data: any, correlationId?: string) => Promise<void>,
|
|
225
230
|
* nackOnError?: boolean
|
|
226
231
|
* }} options
|
|
227
|
-
* @returns {Promise<
|
|
232
|
+
* @returns {Promise<string>} Returns the consumer tag for later cancellation
|
|
228
233
|
*/
|
|
229
234
|
const subscribe = async ({ queue, onReceive, nackOnError = false }) => {
|
|
230
235
|
return subscribeToQueue({ channel, queue, onReceive, log, nackOnError })
|
|
231
236
|
}
|
|
232
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Gracefully closes the RabbitMQ channel and connection.
|
|
240
|
+
*
|
|
241
|
+
* @returns {Promise<void>}
|
|
242
|
+
*/
|
|
243
|
+
const close = async () => {
|
|
244
|
+
const t0 = Date.now()
|
|
245
|
+
const logChild = logger.child({ op: 'close' })
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
logChild.debug('closing-channel-and-connection')
|
|
249
|
+
await channel.close()
|
|
250
|
+
await connection.close()
|
|
251
|
+
|
|
252
|
+
logChild.info({
|
|
253
|
+
event: 'ok',
|
|
254
|
+
ms: Date.now() - t0,
|
|
255
|
+
})
|
|
256
|
+
} catch (err) {
|
|
257
|
+
logChild.error(err, {
|
|
258
|
+
event: 'error',
|
|
259
|
+
ms: Date.now() - t0,
|
|
260
|
+
})
|
|
261
|
+
throw err
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
233
265
|
return {
|
|
234
266
|
channel,
|
|
267
|
+
connection,
|
|
235
268
|
publish,
|
|
236
269
|
subscribe,
|
|
270
|
+
close,
|
|
237
271
|
}
|
|
238
272
|
}
|
|
239
273
|
|