@quatrain/queue-amqp 1.2.2 → 1.2.4

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, params?: any): Promise<amqplib.Replies.Consume | undefined>;
6
9
  }
@@ -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,53 @@ 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(); // 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, params) {
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
+ const concurrency = (params === null || params === void 0 ? void 0 : params.concurrency) || 0;
56
+ queue_1.Queue.info(`Starting to listen to topic ${topic} with max concurrency set to ${concurrency}`);
57
+ let concurrents = 1;
60
58
  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
- // )
59
+ const channel = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
60
+ yield (channel === null || channel === void 0 ? void 0 : channel.assertQueue(topic));
61
+ return channel === null || channel === void 0 ? void 0 : channel.consume(topic, (message) => __awaiter(this, void 0, void 0, function* () {
62
+ // execute workflow
63
+ if (concurrency > 0) {
64
+ queue_1.Queue.info(`Concurrent job #${concurrents + 1} of ${concurrency} triggered`);
65
+ }
66
+ yield messageHandler(message.content.toString(), params);
67
+ channel === null || channel === void 0 ? void 0 : channel.ack(message);
68
+ concurrents++;
69
+ if (concurrency > 0 && concurrents > concurrency) {
70
+ queue_1.Queue.info(`Reached max concurrencu of ${concurrency}, exiting gracefully`);
71
+ yield this._disconnect();
72
+ process.exit();
73
+ }
74
+ }));
72
75
  });
73
76
  }
74
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/queue-amqp",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
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.18",
31
31
  "amqplib": "^0.10.9"
32
32
  },
33
33
  "scripts": {