@quatrain/queue-amqp 1.2.2 → 1.2.3
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 +5 -2
- package/lib/AmqpQueueAdapter.js +29 -29
- package/package.json +3 -3
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { AbstractQueueAdapter } from '@quatrain/queue';
|
|
2
|
+
import amqplib, { ChannelModel } from 'amqplib';
|
|
2
3
|
export declare class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
3
|
-
protected
|
|
4
|
+
protected _client: ChannelModel | undefined;
|
|
5
|
+
protected _connect(): Promise<amqplib.ChannelModel | undefined>;
|
|
6
|
+
protected _disconnect(): Promise<void>;
|
|
4
7
|
send(data: any, topic: string): Promise<string>;
|
|
5
|
-
listen(topic: string | undefined, messageHandler: Function): Promise<
|
|
8
|
+
listen(topic: string | undefined, messageHandler: Function, concurrency?: number): Promise<amqplib.Replies.Consume | undefined>;
|
|
6
9
|
}
|
package/lib/AmqpQueueAdapter.js
CHANGED
|
@@ -16,18 +16,6 @@ exports.AmqpQueueAdapter = void 0;
|
|
|
16
16
|
const queue_1 = require("@quatrain/queue");
|
|
17
17
|
const amqplib_1 = __importDefault(require("amqplib"));
|
|
18
18
|
class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
19
|
-
// constructor(params: QueueParameters) {
|
|
20
|
-
// super(params)
|
|
21
|
-
// const {
|
|
22
|
-
// host = 'localhost',
|
|
23
|
-
// user = 'guest',
|
|
24
|
-
// password = 'guest',
|
|
25
|
-
// port = 5672,
|
|
26
|
-
// } = params.config || {}
|
|
27
|
-
// this._client = new AMQPClient(
|
|
28
|
-
// `amqp://${user}:${password}@${host}:${port}?frameMax=0x80000000`
|
|
29
|
-
// )
|
|
30
|
-
// }
|
|
31
19
|
_connect() {
|
|
32
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
33
21
|
if (this._client) {
|
|
@@ -37,38 +25,50 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
37
25
|
this._client = yield amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
|
|
38
26
|
});
|
|
39
27
|
}
|
|
28
|
+
_disconnect() {
|
|
29
|
+
var _a;
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.close());
|
|
32
|
+
});
|
|
33
|
+
}
|
|
40
34
|
send(data, topic) {
|
|
35
|
+
var _a;
|
|
41
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
-
yield this._connect();
|
|
43
|
-
const
|
|
44
|
-
// const channel: AMQPChannel = await connection.channel()
|
|
45
|
-
// const queue: AMQPQueue = await channel.queue(topic)
|
|
37
|
+
yield this._connect();
|
|
38
|
+
const channel = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
|
|
46
39
|
const dataBuffer = Buffer.from(JSON.stringify(data));
|
|
47
40
|
queue_1.Queue.info(`[AMQP] Sending message to ${topic}`);
|
|
48
|
-
|
|
41
|
+
channel === null || channel === void 0 ? void 0 : channel.sendToQueue(topic, dataBuffer);
|
|
42
|
+
channel === null || channel === void 0 ? void 0 : channel.close();
|
|
49
43
|
// const res = await queue.publish(dataBuffer, { deliveryMode: 1 })
|
|
50
44
|
const id = Date.now(); // fake
|
|
51
45
|
queue_1.Queue.info(`[AMQP] Message send with id ${id}`);
|
|
52
46
|
return String(id);
|
|
53
47
|
});
|
|
54
48
|
}
|
|
55
|
-
listen(topic = this._params.topic, messageHandler) {
|
|
49
|
+
listen(topic = this._params.topic, messageHandler, concurrency = 0) {
|
|
50
|
+
var _a;
|
|
56
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
52
|
if (!topic) {
|
|
58
53
|
throw new Error(`No topic provided for listening.`);
|
|
59
54
|
}
|
|
55
|
+
let concurrents = 1;
|
|
60
56
|
yield this._connect();
|
|
61
|
-
const
|
|
62
|
-
yield
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
57
|
+
const channel = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
|
|
58
|
+
yield (channel === null || channel === void 0 ? void 0 : channel.assertQueue(topic));
|
|
59
|
+
return channel === null || channel === void 0 ? void 0 : channel.consume(topic, (message) => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
// execute workflow
|
|
61
|
+
if (concurrency > 0) {
|
|
62
|
+
queue_1.Queue.info(`Concurrent job #${concurrents + 1} of ${concurrency} triggered`);
|
|
63
|
+
}
|
|
64
|
+
yield messageHandler(message.content.toString());
|
|
65
|
+
channel === null || channel === void 0 ? void 0 : channel.ack(message);
|
|
66
|
+
concurrents++;
|
|
67
|
+
if (concurrency > 0 && concurrents > concurrency) {
|
|
68
|
+
yield this._disconnect();
|
|
69
|
+
process.exit();
|
|
70
|
+
}
|
|
71
|
+
}));
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/queue-amqp",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Queue adapter for AMQP compatible queue",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"@tsconfig/recommended": "^1.0.1",
|
|
15
|
-
"@types/amqplib": "^0",
|
|
15
|
+
"@types/amqplib": "^0.10.8",
|
|
16
16
|
"@types/fs-extra": "^11.0.4",
|
|
17
17
|
"@types/jest": "^27.0.3",
|
|
18
18
|
"@types/node": "^22.10.1",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@cloudamqp/amqp-client": "^3.4.1",
|
|
29
29
|
"@quatrain/core": "^1.1.22",
|
|
30
|
-
"@quatrain/queue": "^1.1.
|
|
30
|
+
"@quatrain/queue": "^1.1.17",
|
|
31
31
|
"amqplib": "^0.10.9"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|