@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.
@@ -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<amqplib.ChannelModel | undefined>;
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 | undefined>;
8
+ listen(topic: string | undefined, messageHandler: Function, params?: any): Promise<amqplib.Replies.Consume>;
9
9
  }
@@ -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
- return this._client;
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
- const { host = 'localhost', user = 'guest', password = 'guest', port = 5672, } = this._params.config || {};
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 ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
60
- yield (channel === null || channel === void 0 ? void 0 : channel.prefetch(1)); // only get one message at a time
61
- yield (channel === null || channel === void 0 ? void 0 : channel.assertQueue(topic));
62
- return channel === null || channel === void 0 ? void 0 : channel.consume(topic, (message) => __awaiter(this, void 0, void 0, function* () {
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
- queue_1.Queue.info(`Concurrent job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} triggered`);
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
- yield this._disconnect();
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
- yield messageHandler(message.content.toString(), params);
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
- if (concurrency > 0 && concurrents >= concurrency) {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/queue-amqp",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "description": "Queue adapter for AMQP compatible queue",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",