@quatrain/queue-amqp 1.2.9 → 1.2.11
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 +42 -24
- 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,41 +46,59 @@ 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
|
-
queue_1.Queue.info('listen() params', params);
|
|
52
50
|
if (!topic) {
|
|
53
51
|
throw new Error(`No topic provided for listening.`);
|
|
54
52
|
}
|
|
55
53
|
const concurrency = (params === null || params === void 0 ? void 0 : params.concurrency) || 0;
|
|
56
54
|
queue_1.Queue.info(`Starting to listen to topic ${topic} with max concurrency set to ${concurrency}`);
|
|
57
55
|
let concurrents = 0;
|
|
58
|
-
yield this._connect();
|
|
59
|
-
const channel = yield
|
|
60
|
-
yield (
|
|
61
|
-
yield (
|
|
62
|
-
return channel
|
|
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(1); // 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) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
63
64
|
concurrents++;
|
|
64
|
-
|
|
65
|
-
channel === null || channel === void 0 ? void 0 : channel.ack(message);
|
|
66
|
-
if (concurrency > 0 && concurrents >= concurrency) {
|
|
65
|
+
if (channel && concurrency > 0 && concurrents >= concurrency) {
|
|
67
66
|
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
|
|
68
|
-
|
|
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
|
+
});
|
|
69
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`);
|
|
70
87
|
try {
|
|
71
|
-
|
|
88
|
+
messageHandler(msg.content.toString(), params).finally(() => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (concurrency > 0 && concurrents >= concurrency) {
|
|
90
|
+
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, exiting gracefully`);
|
|
91
|
+
queue_1.Queue.info('Sleeping 3 seconds to allow asynchronous calls to finish');
|
|
92
|
+
yield queue_1.Queue.sleep(3);
|
|
93
|
+
process.exit();
|
|
94
|
+
}
|
|
95
|
+
}));
|
|
72
96
|
}
|
|
73
97
|
catch (err) {
|
|
74
98
|
queue_1.Queue.error(`messageHandler function failed with message: ${err.message}`);
|
|
99
|
+
process.exit(1);
|
|
75
100
|
}
|
|
76
|
-
|
|
77
|
-
queue_1.Queue.info(`Reached max concurrency of ${concurrency}, exiting gracefully`);
|
|
78
|
-
queue_1.Queue.info('Sleeping 5 seconds to allow asynchronous calls to finish');
|
|
79
|
-
yield queue_1.Queue.sleep(5);
|
|
80
|
-
process.exit();
|
|
81
|
-
}
|
|
82
|
-
})
|
|
83
|
-
// { noAck: true } // auto-hack
|
|
101
|
+
}), { noAck: false } // disable auto-hack
|
|
84
102
|
);
|
|
85
103
|
});
|
|
86
104
|
}
|