@quatrain/queue-amqp 1.2.9 → 1.2.10
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 +26 -10
- package/package.json +1 -1
package/lib/AmqpQueueAdapter.js
CHANGED
|
@@ -48,7 +48,6 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
48
48
|
listen(topic = this._params.topic, messageHandler, params) {
|
|
49
49
|
var _a;
|
|
50
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
queue_1.Queue.info('listen() params', params);
|
|
52
51
|
if (!topic) {
|
|
53
52
|
throw new Error(`No topic provided for listening.`);
|
|
54
53
|
}
|
|
@@ -58,29 +57,46 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
58
57
|
yield this._connect();
|
|
59
58
|
const channel = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
|
|
60
59
|
yield (channel === null || channel === void 0 ? void 0 : channel.prefetch(1)); // only get one message at a time
|
|
61
|
-
yield (channel === null || channel === void 0 ? void 0 : channel.assertQueue(topic));
|
|
62
|
-
return channel === null || channel === void 0 ? void 0 : channel.consume(topic, (
|
|
60
|
+
yield (channel === null || channel === void 0 ? void 0 : channel.assertQueue(topic, { durable: true, autoDelete: false }));
|
|
61
|
+
return channel === null || channel === void 0 ? void 0 : channel.consume(topic, (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
if (msg === null) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
channel === null || channel === void 0 ? void 0 : channel.ack(msg);
|
|
66
|
+
yield queue_1.Queue.sleep(); // sleep for one second
|
|
63
67
|
concurrents++;
|
|
64
68
|
queue_1.Queue.info(`Concurrent job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} triggered`);
|
|
65
|
-
channel === null || channel === void 0 ? void 0 : channel.ack(message);
|
|
66
69
|
if (concurrency > 0 && concurrents >= concurrency) {
|
|
67
70
|
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
|
|
68
|
-
|
|
71
|
+
channel
|
|
72
|
+
.close()
|
|
73
|
+
.then(() => __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
queue_1.Queue.info('Channel closed.');
|
|
75
|
+
// 4. Then close CONNECTION
|
|
76
|
+
return yield this._disconnect();
|
|
77
|
+
}))
|
|
78
|
+
.then(() => {
|
|
79
|
+
queue_1.Queue.info('Connection closed. Exiting.');
|
|
80
|
+
})
|
|
81
|
+
.catch((err) => {
|
|
82
|
+
queue_1.Queue.error('Error during shutdown:', err);
|
|
83
|
+
});
|
|
69
84
|
}
|
|
70
85
|
try {
|
|
71
|
-
yield messageHandler(
|
|
86
|
+
yield messageHandler(msg.content.toString(), params);
|
|
72
87
|
}
|
|
73
88
|
catch (err) {
|
|
74
89
|
queue_1.Queue.error(`messageHandler function failed with message: ${err.message}`);
|
|
75
90
|
}
|
|
76
91
|
if (concurrency > 0 && concurrents >= concurrency) {
|
|
77
92
|
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, exiting gracefully`);
|
|
78
|
-
|
|
79
|
-
|
|
93
|
+
// Queue.info(
|
|
94
|
+
// 'Sleeping 5 seconds to allow asynchronous calls to finish'
|
|
95
|
+
// )
|
|
96
|
+
// await Queue.sleep(5)
|
|
80
97
|
process.exit();
|
|
81
98
|
}
|
|
82
|
-
})
|
|
83
|
-
// { noAck: true } // auto-hack
|
|
99
|
+
}), { noAck: true } // auto-hack
|
|
84
100
|
);
|
|
85
101
|
});
|
|
86
102
|
}
|