@quatrain/queue-amqp 1.2.10 → 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.d.ts +2 -2
- package/lib/AmqpQueueAdapter.js +25 -39
- package/package.json +1 -1
|
@@ -2,8 +2,8 @@ import { AbstractQueueAdapter } from '@quatrain/queue';
|
|
|
2
2
|
import amqplib, { ChannelModel } from 'amqplib';
|
|
3
3
|
export declare class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
4
4
|
protected _client: ChannelModel | undefined;
|
|
5
|
-
protected _connect(): Promise<
|
|
5
|
+
protected _connect(): Promise<ChannelModel>;
|
|
6
6
|
protected _disconnect(): Promise<void>;
|
|
7
7
|
send(data: any, topic: string): Promise<string>;
|
|
8
|
-
listen(topic: string | undefined, messageHandler: Function, params?: any): Promise<amqplib.Replies.Consume
|
|
8
|
+
listen(topic: string | undefined, messageHandler: Function, params?: any): Promise<amqplib.Replies.Consume>;
|
|
9
9
|
}
|
package/lib/AmqpQueueAdapter.js
CHANGED
|
@@ -18,11 +18,11 @@ const amqplib_1 = __importDefault(require("amqplib"));
|
|
|
18
18
|
class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
19
19
|
_connect() {
|
|
20
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
if (this._client) {
|
|
22
|
-
|
|
21
|
+
if (!this._client) {
|
|
22
|
+
const { host = 'localhost', user = 'guest', password = 'guest', port = 5672, } = this._params.config || {};
|
|
23
|
+
this._client = yield amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
this._client = yield amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
|
|
25
|
+
return this._client;
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
_disconnect() {
|
|
@@ -46,7 +46,6 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
listen(topic = this._params.topic, messageHandler, params) {
|
|
49
|
-
var _a;
|
|
50
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
50
|
if (!topic) {
|
|
52
51
|
throw new Error(`No topic provided for listening.`);
|
|
@@ -54,49 +53,36 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
54
53
|
const concurrency = (params === null || params === void 0 ? void 0 : params.concurrency) || 0;
|
|
55
54
|
queue_1.Queue.info(`Starting to listen to topic ${topic} with max concurrency set to ${concurrency}`);
|
|
56
55
|
let concurrents = 0;
|
|
57
|
-
yield this._connect();
|
|
58
|
-
const channel = yield
|
|
59
|
-
yield (
|
|
60
|
-
yield (
|
|
61
|
-
return channel
|
|
62
|
-
if (msg === null) {
|
|
56
|
+
const client = yield this._connect();
|
|
57
|
+
const channel = yield client.createChannel();
|
|
58
|
+
yield channel.assertQueue(topic, { durable: true, autoDelete: false });
|
|
59
|
+
yield channel.prefetch(concurrency); // only get one message at a time
|
|
60
|
+
return channel.consume(topic, (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
if (msg === null || !channel) {
|
|
63
62
|
return;
|
|
64
63
|
}
|
|
65
|
-
channel === null || channel === void 0 ? void 0 : channel.ack(msg);
|
|
66
|
-
yield queue_1.Queue.sleep(); // sleep for one second
|
|
67
64
|
concurrents++;
|
|
68
|
-
queue_1.Queue.info(`
|
|
69
|
-
if (concurrency > 0 && concurrents >= concurrency) {
|
|
70
|
-
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
|
|
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
|
-
});
|
|
84
|
-
}
|
|
65
|
+
queue_1.Queue.info(`Job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} started`);
|
|
85
66
|
try {
|
|
67
|
+
// 1. Process the message synchronously (wait for it to finish)
|
|
86
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
|
+
}
|
|
87
78
|
}
|
|
88
79
|
catch (err) {
|
|
89
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);
|
|
83
|
+
process.exit(1);
|
|
90
84
|
}
|
|
91
|
-
|
|
92
|
-
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, exiting gracefully`);
|
|
93
|
-
// Queue.info(
|
|
94
|
-
// 'Sleeping 5 seconds to allow asynchronous calls to finish'
|
|
95
|
-
// )
|
|
96
|
-
// await Queue.sleep(5)
|
|
97
|
-
process.exit();
|
|
98
|
-
}
|
|
99
|
-
}), { noAck: true } // auto-hack
|
|
85
|
+
}), { noAck: false } // disable auto-hack
|
|
100
86
|
);
|
|
101
87
|
});
|
|
102
88
|
}
|