@quatrain/queue-amqp 1.2.21 → 1.2.23
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/README.md +38 -0
- package/dist/AmqpQueueAdapter.d.ts +2 -0
- package/dist/AmqpQueueAdapter.js +10 -6
- package/package.json +3 -3
- package/src/AmqpQueueAdapter.ts +12 -6
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @quatrain/queue-amqp
|
|
2
|
+
|
|
3
|
+
The AMQP (Advanced Message Queuing Protocol) adapter for `@quatrain/queue`.
|
|
4
|
+
|
|
5
|
+
## Introduction
|
|
6
|
+
|
|
7
|
+
This adapter enables communication with AMQP 0-9-1 brokers such as RabbitMQ. It is ideal for high-throughput, low-latency messaging requirements where you have full control over the broker infrastructure.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @quatrain/queue-amqp amqplib
|
|
13
|
+
# or
|
|
14
|
+
yarn add @quatrain/queue-amqp amqplib
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Configuration
|
|
18
|
+
|
|
19
|
+
Register the adapter by providing the connection URL for your AMQP broker.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { Queue } from '@quatrain/queue'
|
|
23
|
+
import { AmqpQueueAdapter } from '@quatrain/queue-amqp'
|
|
24
|
+
|
|
25
|
+
const amqpAdapter = new AmqpQueueAdapter({
|
|
26
|
+
config: {
|
|
27
|
+
url: 'amqp://user:password@localhost:5672',
|
|
28
|
+
// Optional: specify an exchange if not using default
|
|
29
|
+
exchange: 'quatrain-exchange'
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
Queue.addAdapter('rabbitmq', amqpAdapter, true)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
AGPL-3.0-only
|
|
@@ -2,6 +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 _logger: any;
|
|
6
|
+
constructor(params: any);
|
|
5
7
|
protected _connect(): Promise<ChannelModel>;
|
|
6
8
|
protected _disconnect(): Promise<void>;
|
|
7
9
|
send(data: any, topic: string): Promise<string>;
|
package/dist/AmqpQueueAdapter.js
CHANGED
|
@@ -16,6 +16,10 @@ 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) {
|
|
20
|
+
super(params);
|
|
21
|
+
this._logger = queue_1.Queue.logger.clone('AMQP');
|
|
22
|
+
}
|
|
19
23
|
_connect() {
|
|
20
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
21
25
|
if (!this._client) {
|
|
@@ -37,11 +41,11 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
37
41
|
yield this._connect();
|
|
38
42
|
const channel = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
|
|
39
43
|
const dataBuffer = Buffer.from(JSON.stringify(data));
|
|
40
|
-
|
|
44
|
+
this._logger.info(`Sending message to ${topic}`);
|
|
41
45
|
channel === null || channel === void 0 ? void 0 : channel.sendToQueue(topic, dataBuffer);
|
|
42
46
|
channel === null || channel === void 0 ? void 0 : channel.close();
|
|
43
47
|
const id = Date.now(); // fake
|
|
44
|
-
|
|
48
|
+
this._logger.info(`Message send with id ${id}`);
|
|
45
49
|
return String(id);
|
|
46
50
|
});
|
|
47
51
|
}
|
|
@@ -58,7 +62,7 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
58
62
|
throw new Error(`No topic provided for listening.`);
|
|
59
63
|
}
|
|
60
64
|
const concurrency = (params === null || params === void 0 ? void 0 : params.concurrency) || 0;
|
|
61
|
-
|
|
65
|
+
this._logger.info(`Starting to listen to topic ${topic} with max concurrency set to ${concurrency}`);
|
|
62
66
|
let concurrents = 0;
|
|
63
67
|
const client = yield this._connect();
|
|
64
68
|
const channel = yield client.createChannel();
|
|
@@ -69,7 +73,7 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
69
73
|
return;
|
|
70
74
|
}
|
|
71
75
|
concurrents++;
|
|
72
|
-
|
|
76
|
+
this._logger.info(`Job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} started`);
|
|
73
77
|
try {
|
|
74
78
|
// 1. Process the message synchronously (wait for it to finish)
|
|
75
79
|
const result = yield messageHandler(msg.content.toString(), params);
|
|
@@ -80,14 +84,14 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
80
84
|
channel.ack(msg);
|
|
81
85
|
// 3. Check if we reached the limit of messages to process
|
|
82
86
|
if (concurrency > 0 && concurrents >= concurrency) {
|
|
83
|
-
|
|
87
|
+
this._logger.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
|
|
84
88
|
yield channel.close();
|
|
85
89
|
yield client.close();
|
|
86
90
|
process.exit(0);
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
catch (err) {
|
|
90
|
-
|
|
94
|
+
this._logger.error(`messageHandler function failed with message: ${err.message}`);
|
|
91
95
|
// Negative acknowledge the message so it can be requeued or handled by DLQ
|
|
92
96
|
channel.nack(msg);
|
|
93
97
|
process.exit(1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/queue-amqp",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.23",
|
|
4
4
|
"description": "Queue adapter for AMQP compatible queue",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@cloudamqp/amqp-client": "^3.4.1",
|
|
32
|
-
"@quatrain/core": "^1.1.
|
|
33
|
-
"@quatrain/queue": "^1.1.
|
|
32
|
+
"@quatrain/core": "^1.1.49",
|
|
33
|
+
"@quatrain/queue": "^1.1.29",
|
|
34
34
|
"amqplib": "^0.10.9"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
package/src/AmqpQueueAdapter.ts
CHANGED
|
@@ -3,6 +3,12 @@ import amqplib, { ChannelModel, ConsumeMessage, Message } from 'amqplib'
|
|
|
3
3
|
|
|
4
4
|
export class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
5
5
|
protected _client: ChannelModel | undefined
|
|
6
|
+
protected _logger: any
|
|
7
|
+
|
|
8
|
+
constructor(params: any) {
|
|
9
|
+
super(params)
|
|
10
|
+
this._logger = (Queue as any).logger.clone('AMQP')
|
|
11
|
+
}
|
|
6
12
|
|
|
7
13
|
protected async _connect(): Promise<ChannelModel> {
|
|
8
14
|
if (!this._client) {
|
|
@@ -31,13 +37,13 @@ export class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
|
31
37
|
const channel = await this._client?.createChannel()
|
|
32
38
|
|
|
33
39
|
const dataBuffer = Buffer.from(JSON.stringify(data))
|
|
34
|
-
|
|
40
|
+
this._logger.info(`Sending message to ${topic}`)
|
|
35
41
|
|
|
36
42
|
channel?.sendToQueue(topic, dataBuffer)
|
|
37
43
|
channel?.close()
|
|
38
44
|
|
|
39
45
|
const id = Date.now() // fake
|
|
40
|
-
|
|
46
|
+
this._logger.info(`Message send with id ${id}`)
|
|
41
47
|
|
|
42
48
|
return String(id)
|
|
43
49
|
}
|
|
@@ -60,7 +66,7 @@ export class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
|
60
66
|
|
|
61
67
|
const concurrency = params?.concurrency || 0
|
|
62
68
|
|
|
63
|
-
|
|
69
|
+
this._logger.info(
|
|
64
70
|
`Starting to listen to topic ${topic} with max concurrency set to ${concurrency}`
|
|
65
71
|
)
|
|
66
72
|
|
|
@@ -79,7 +85,7 @@ export class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
concurrents++
|
|
82
|
-
|
|
88
|
+
this._logger.info(
|
|
83
89
|
`Job #${concurrents} of ${
|
|
84
90
|
concurrency > 0 ? concurrency : 'unlimited'
|
|
85
91
|
} started`
|
|
@@ -103,7 +109,7 @@ export class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
|
103
109
|
|
|
104
110
|
// 3. Check if we reached the limit of messages to process
|
|
105
111
|
if (concurrency > 0 && concurrents >= concurrency) {
|
|
106
|
-
|
|
112
|
+
this._logger.info(
|
|
107
113
|
`Reached max concurrency of ${concurrency}, disconnecting from queue`
|
|
108
114
|
)
|
|
109
115
|
await channel.close()
|
|
@@ -111,7 +117,7 @@ export class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
|
111
117
|
process.exit(0)
|
|
112
118
|
}
|
|
113
119
|
} catch (err) {
|
|
114
|
-
|
|
120
|
+
this._logger.error(
|
|
115
121
|
`messageHandler function failed with message: ${
|
|
116
122
|
(err as Error).message
|
|
117
123
|
}`
|