@quatrain/queue-amqp 1.2.1 → 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.
@@ -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 _connect(): Promise<any>;
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<any>;
8
+ listen(topic: string | undefined, messageHandler: Function, concurrency?: number): Promise<amqplib.Replies.Consume | undefined>;
6
9
  }
@@ -16,59 +16,59 @@ 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) {
34
22
  return this._client;
35
23
  }
36
24
  const { host = 'localhost', user = 'guest', password = 'guest', port = 5672, } = this._params.config || {};
37
- this._client = amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
25
+ this._client = yield amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
26
+ });
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());
38
32
  });
39
33
  }
40
34
  send(data, topic) {
35
+ var _a;
41
36
  return __awaiter(this, void 0, void 0, function* () {
42
- yield this._connect(); // this._client.connect()
43
- const ch = yield this._client.createChannel();
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
- ch.sendToQueue(topic, dataBuffer);
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 ch = yield this._client.createChannel();
62
- yield ch.assertQueue(topic);
63
- return ch.consume(topic, (message) => __awaiter(this, void 0, void 0, function* () { return yield messageHandler(message.content.toString()); }));
64
- // const connection = await this._client.connect()
65
- // const channel = await connection.channel()
66
- // const queue = await channel.queue(topic)
67
- // return queue.subscribe(
68
- // { noAck: true },
69
- // async (message: AMQPMessage) =>
70
- // await messageHandler(message.bodyToString())
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.1",
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.16",
30
+ "@quatrain/queue": "^1.1.17",
31
31
  "amqplib": "^0.10.9"
32
32
  },
33
33
  "scripts": {