@modernlock/common 1.0.63 → 1.0.64

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.
@@ -8,8 +8,11 @@ export declare abstract class Consumer {
8
8
  abstract exchangeType: ExchangeTypes;
9
9
  abstract routingKey: RoutingKeys;
10
10
  abstract queue: Queues;
11
- abstract onMessage(msg: amqp.Message, data: any, channel: amqp.Channel): void;
11
+ abstract onMessage(msg: amqp.Message, data: any, channel?: amqp.Channel): Promise<void>;
12
12
  private channel;
13
+ private dlx;
14
+ private dlxType;
15
+ private dlRoutingKey;
13
16
  constructor(channel: amqp.Channel);
14
17
  listen(): Promise<void>;
15
18
  parseMessage(msg: amqp.Message): any;
@@ -12,21 +12,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Consumer = void 0;
13
13
  class Consumer {
14
14
  constructor(channel) {
15
+ // We define standard names for our Dead letter queu
16
+ this.dlx = 'dead_letter_exchange';
17
+ this.dlxType = 'direct';
18
+ this.dlRoutingKey = 'dead_letter';
15
19
  this.channel = channel;
16
20
  }
17
21
  ;
18
22
  listen() {
19
23
  return __awaiter(this, void 0, void 0, function* () {
20
24
  try {
25
+ // Assert the Dead Letter Exchange
26
+ yield this.channel.assertExchange(this.dlx, this.dlxType);
27
+ // Assert the Dead Letter Queue
28
+ const dlQueueName = `${this.queue}.dlq`; // e.g., "update-calendar.dlq"
29
+ yield this.channel.assertQueue(dlQueueName, { durable: true });
30
+ // Bind them so failed messages go into the DLQ
31
+ yield this.channel.bindQueue(dlQueueName, this.dlx, this.dlRoutingKey);
21
32
  yield this.channel.assertExchange(this.exchange, this.exchangeType);
22
- const q = yield this.channel.assertQueue(this.queue, { durable: true });
33
+ // Assert the Main Queue with special arguments linking to DLQ
34
+ const q = yield this.channel.assertQueue(this.queue, {
35
+ durable: true,
36
+ arguments: {
37
+ // If message is Nack'ed (rejected), send to this exchange:
38
+ 'x-dead-letter-exchange': this.dlx,
39
+ // With this routing key:
40
+ 'x-dead-letter-routing-key': this.dlRoutingKey
41
+ // Optional: Time to Live (TTL) before moving to DLQ
42
+ // 'x-message-ttl': 60000
43
+ }
44
+ });
23
45
  yield this.channel.bindQueue(q.queue, this.exchange, this.routingKey);
24
- this.channel.consume(q.queue, (msg) => {
46
+ yield this.channel.prefetch(1);
47
+ this.channel.consume(q.queue, (msg) => __awaiter(this, void 0, void 0, function* () {
25
48
  if (!msg)
26
- throw new Error('Message is required');
27
- const data = this.parseMessage(msg);
28
- this.onMessage(msg, data, this.channel);
29
- });
49
+ return;
50
+ try {
51
+ const data = this.parseMessage(msg);
52
+ yield this.onMessage(data, msg);
53
+ this.channel.ack(msg);
54
+ }
55
+ catch (error) {
56
+ console.error(`Error processing message in ${this.queue}:`, error);
57
+ this.channel.nack(msg, false, false);
58
+ }
59
+ }));
30
60
  }
31
61
  catch (error) {
32
62
  console.error('Failed to publish message:', error);
@@ -10,5 +10,6 @@ export declare class RabbitmqWrapper {
10
10
  connect(url: string): Promise<void>;
11
11
  connectWithRetries(url: string): Promise<void>;
12
12
  private reconnect;
13
+ close(): Promise<void>;
13
14
  }
14
15
  export declare const rabbitmqWrapper: RabbitmqWrapper;
@@ -115,6 +115,19 @@ class RabbitmqWrapper {
115
115
  }
116
116
  });
117
117
  }
118
+ close() {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ try {
121
+ if (this.channel)
122
+ yield this.channel.close();
123
+ if (this.connection)
124
+ yield this.connection.close();
125
+ }
126
+ catch (err) {
127
+ console.error("Error closing RabbitMQ connection", err);
128
+ }
129
+ });
130
+ }
118
131
  }
119
132
  exports.RabbitmqWrapper = RabbitmqWrapper;
120
133
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernlock/common",
3
- "version": "1.0.63",
3
+ "version": "1.0.64",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [