@modernlock/common 1.0.52 → 1.0.54
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.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Channel } from 'amqplib';
|
|
2
|
+
import { INotification } from '../@types/notification';
|
|
3
|
+
export declare class NotificationPublisher {
|
|
4
|
+
private channel;
|
|
5
|
+
private readonly exchange;
|
|
6
|
+
private readonly exchangeType;
|
|
7
|
+
constructor(channel: Channel);
|
|
8
|
+
/**
|
|
9
|
+
* Publishes a notification event to the topic exchange.
|
|
10
|
+
* @param routingKey The descriptive routing key (e.g., 'alert.critical.water_leak').
|
|
11
|
+
* @param notification The fully-formed notification object.
|
|
12
|
+
*/
|
|
13
|
+
publish(routingKey: string, notification: Omit<INotification, '_id' | 'createdAt'>): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.NotificationPublisher = void 0;
|
|
13
|
+
class NotificationPublisher {
|
|
14
|
+
constructor(channel) {
|
|
15
|
+
this.exchange = 'notification_exchange'; // The single topic exchange for all notifications
|
|
16
|
+
this.exchangeType = 'topic';
|
|
17
|
+
this.channel = channel;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Publishes a notification event to the topic exchange.
|
|
21
|
+
* @param routingKey The descriptive routing key (e.g., 'alert.critical.water_leak').
|
|
22
|
+
* @param notification The fully-formed notification object.
|
|
23
|
+
*/
|
|
24
|
+
publish(routingKey, notification) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
try {
|
|
27
|
+
yield this.channel.assertExchange(this.exchange, this.exchangeType, { durable: true });
|
|
28
|
+
const message = JSON.stringify(notification);
|
|
29
|
+
this.channel.publish(this.exchange, routingKey, Buffer.from(message));
|
|
30
|
+
console.log(`[NotificationPublisher] Published event with key '${routingKey}'`);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.error('[NotificationPublisher] Failed to publish notification:', error);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.NotificationPublisher = NotificationPublisher;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Channel, Message } from 'amqplib';
|
|
2
|
+
export declare abstract class PriorityConsumer {
|
|
3
|
+
protected channel: Channel;
|
|
4
|
+
abstract exchange: string;
|
|
5
|
+
abstract serviceName: string;
|
|
6
|
+
abstract highPriorityBindingKeys: string[];
|
|
7
|
+
abstract normalPriorityBindingKeys: string[];
|
|
8
|
+
abstract onHighPriorityMessage(data: any, msg: Message): void;
|
|
9
|
+
abstract onNormalPriorityMessage(data: any, msg: Message): void;
|
|
10
|
+
constructor(channel: Channel);
|
|
11
|
+
startListening(): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PriorityConsumer = void 0;
|
|
13
|
+
class PriorityConsumer {
|
|
14
|
+
constructor(channel) {
|
|
15
|
+
this.channel = channel;
|
|
16
|
+
}
|
|
17
|
+
startListening() {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
yield this.channel.assertExchange(this.exchange, 'topic', { durable: true });
|
|
20
|
+
// --- Setup High Priority Queue ---
|
|
21
|
+
const highPriorityQueueName = `${this.serviceName}_high_priority_q`;
|
|
22
|
+
yield this.channel.assertQueue(highPriorityQueueName, { durable: true });
|
|
23
|
+
for (const key of this.highPriorityBindingKeys) {
|
|
24
|
+
yield this.channel.bindQueue(highPriorityQueueName, this.exchange, key);
|
|
25
|
+
}
|
|
26
|
+
// --- Setup Normal Priority Queue ---
|
|
27
|
+
const normalPriorityQueueName = `${this.serviceName}_normal_priority_q`;
|
|
28
|
+
yield this.channel.assertQueue(normalPriorityQueueName, { durable: true });
|
|
29
|
+
for (const key of this.normalPriorityBindingKeys) {
|
|
30
|
+
yield this.channel.bindQueue(normalPriorityQueueName, this.exchange, key);
|
|
31
|
+
}
|
|
32
|
+
console.log(`[${this.serviceName}] Waiting for messages...`);
|
|
33
|
+
// --- Start Consuming (with QoS for prioritization) ---
|
|
34
|
+
// Allow fetching up to 10 high-priority messages at once
|
|
35
|
+
this.channel.prefetch(10, true); // This prefetch count applies to the next consume call
|
|
36
|
+
this.channel.consume(highPriorityQueueName, (msg) => {
|
|
37
|
+
if (msg) {
|
|
38
|
+
this.onHighPriorityMessage(JSON.parse(msg.content.toString()), msg);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
// Only fetch 1 normal-priority message at a time.
|
|
42
|
+
// This encourages the worker to be available for high-priority tasks.
|
|
43
|
+
this.channel.prefetch(1, true);
|
|
44
|
+
this.channel.consume(normalPriorityQueueName, (msg) => {
|
|
45
|
+
if (msg) {
|
|
46
|
+
this.onNormalPriorityMessage(JSON.parse(msg.content.toString()), msg);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.PriorityConsumer = PriorityConsumer;
|
package/build/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export * from "./events/send-notification-publisher";
|
|
|
10
10
|
export * from "./events/chat-notification-publisher";
|
|
11
11
|
export * from "./events/events";
|
|
12
12
|
export * from "./events/mappings";
|
|
13
|
+
export * from "./events/priority-consumer";
|
|
14
|
+
export * from "./events/notification-publisher";
|
|
13
15
|
export * from "./errors/custom-error";
|
|
14
16
|
export * from "./errors/internal-server-error";
|
|
15
17
|
export * from "./errors/not-found-error";
|
package/build/index.js
CHANGED
|
@@ -26,6 +26,8 @@ __exportStar(require("./events/send-notification-publisher"), exports);
|
|
|
26
26
|
__exportStar(require("./events/chat-notification-publisher"), exports);
|
|
27
27
|
__exportStar(require("./events/events"), exports);
|
|
28
28
|
__exportStar(require("./events/mappings"), exports);
|
|
29
|
+
__exportStar(require("./events/priority-consumer"), exports);
|
|
30
|
+
__exportStar(require("./events/notification-publisher"), exports);
|
|
29
31
|
__exportStar(require("./errors/custom-error"), exports);
|
|
30
32
|
__exportStar(require("./errors/internal-server-error"), exports);
|
|
31
33
|
__exportStar(require("./errors/not-found-error"), exports);
|