@quatrain/queue-amqp 1.2.11 → 1.2.12
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/lib/AmqpQueueAdapter.js +15 -31
- package/package.json +1 -1
package/lib/AmqpQueueAdapter.js
CHANGED
|
@@ -56,46 +56,30 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
56
56
|
const client = yield this._connect();
|
|
57
57
|
const channel = yield client.createChannel();
|
|
58
58
|
yield channel.assertQueue(topic, { durable: true, autoDelete: false });
|
|
59
|
-
yield channel.prefetch(
|
|
59
|
+
yield channel.prefetch(concurrency); // only get one message at a time
|
|
60
60
|
return channel.consume(topic, (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
61
61
|
if (msg === null || !channel) {
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
concurrents++;
|
|
65
|
-
|
|
66
|
-
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
|
|
67
|
-
channel.ack(msg);
|
|
68
|
-
yield channel
|
|
69
|
-
.close()
|
|
70
|
-
.then(() => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
queue_1.Queue.info('Channel closed.');
|
|
72
|
-
// 4. Then close CONNECTION
|
|
73
|
-
return yield client.close(); //._disconnect()
|
|
74
|
-
}))
|
|
75
|
-
.then(() => {
|
|
76
|
-
queue_1.Queue.info('Connection closed. Exiting.');
|
|
77
|
-
})
|
|
78
|
-
.catch((err) => {
|
|
79
|
-
queue_1.Queue.error('Error during shutdown:', err);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
// if (channel) {
|
|
83
|
-
// channel.ack(msg)
|
|
84
|
-
// await Queue.sleep(1) // sleep for one second
|
|
85
|
-
// }
|
|
86
|
-
queue_1.Queue.info(`Concurrent job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} triggered`);
|
|
65
|
+
queue_1.Queue.info(`Job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} started`);
|
|
87
66
|
try {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
67
|
+
// 1. Process the message synchronously (wait for it to finish)
|
|
68
|
+
yield messageHandler(msg.content.toString(), params);
|
|
69
|
+
// 2. Acknowledge the message only after successful processing
|
|
70
|
+
channel.ack(msg);
|
|
71
|
+
// 3. Check if we reached the limit of messages to process
|
|
72
|
+
if (concurrency > 0 && concurrents >= concurrency) {
|
|
73
|
+
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
|
|
74
|
+
yield channel.close();
|
|
75
|
+
yield client.close();
|
|
76
|
+
process.exit(0);
|
|
77
|
+
}
|
|
96
78
|
}
|
|
97
79
|
catch (err) {
|
|
98
80
|
queue_1.Queue.error(`messageHandler function failed with message: ${err.message}`);
|
|
81
|
+
// Negative acknowledge the message so it can be requeued or handled by DLQ
|
|
82
|
+
channel.nack(msg);
|
|
99
83
|
process.exit(1);
|
|
100
84
|
}
|
|
101
85
|
}), { noAck: false } // disable auto-hack
|