message-brokers 1.10.0-beta → 1.10.1-beta
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.
|
@@ -3,7 +3,7 @@ const CacheService = require("../services/cache.service");
|
|
|
3
3
|
const DeadQueueService = require("../services/deadqueue.service");
|
|
4
4
|
const mbConfigs = require("../configs/mb.configs");
|
|
5
5
|
const maskPid = require("../utils/maskPid");
|
|
6
|
-
const CentralRabbitMQHelper = require("../
|
|
6
|
+
const CentralRabbitMQHelper = require("../rabbitmq/centralRabbitmq/centralRabbitmq.helper");
|
|
7
7
|
const AmqpController = {};
|
|
8
8
|
|
|
9
9
|
/**
|
package/lib/package-loader.js
CHANGED
|
@@ -4,7 +4,7 @@ const {
|
|
|
4
4
|
const redisDB = require("./helpers/redis.helper");
|
|
5
5
|
const mongoDB = require("./helpers/mongodb.helper");
|
|
6
6
|
const MessageBroker = require("./helpers/message-brokers.helper");
|
|
7
|
-
const CentralRabbitMQHelper = require("./
|
|
7
|
+
const CentralRabbitMQHelper = require("./rabbitmq/centralRabbitmq/centralRabbitmq.helper");
|
|
8
8
|
const mbConfigs = require("./configs/mb.configs");
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const {
|
|
2
|
+
randomUUID
|
|
3
|
+
} = require("crypto");
|
|
4
|
+
const centralRabbitmq = require("./centralRabbitmqConnection");
|
|
5
|
+
const {
|
|
6
|
+
exchangeLogger,
|
|
7
|
+
queueLogger,
|
|
8
|
+
bindingLogger
|
|
9
|
+
} = require("../../utils/log.util");
|
|
10
|
+
const centralMqConfigs = require("../../configs/centralMqConfigs");
|
|
11
|
+
const mbConfigs = require("../../configs/mb.configs");
|
|
12
|
+
const CacheService = require("../../services/cache.service");
|
|
13
|
+
const {
|
|
14
|
+
applyQueueName,
|
|
15
|
+
applyRoutingKey,
|
|
16
|
+
applyExchangeName,
|
|
17
|
+
applyRoutingKeyIsolationForPublish
|
|
18
|
+
} = require("../../utils/rabbitmq.utils");
|
|
19
|
+
|
|
20
|
+
// const offlinePubQueue = [];
|
|
21
|
+
|
|
22
|
+
const CentralRabbitMQHelper = {};
|
|
23
|
+
const CachingEnabled = mbConfigs.cachingEnabled === "true";
|
|
24
|
+
CentralRabbitMQHelper.connect = centralRabbitmq;
|
|
25
|
+
CentralRabbitMQHelper.initializePubSub = async () => {
|
|
26
|
+
try {
|
|
27
|
+
const rabbitmqChannel = await centralRabbitmq();
|
|
28
|
+
const exchangeType = "direct";
|
|
29
|
+
for (const mq of centralMqConfigs) {
|
|
30
|
+
let exchangeName = applyExchangeName(mq.exchangeName);
|
|
31
|
+
await rabbitmqChannel.assertExchange(exchangeName, exchangeType, {
|
|
32
|
+
durable: true
|
|
33
|
+
});
|
|
34
|
+
await exchangeLogger(exchangeName, "central-amqp");
|
|
35
|
+
for (const queueConfig of mq.queueConfigList) {
|
|
36
|
+
await CentralRabbitMQHelper.initializeAndBindQueue(rabbitmqChannel, exchangeName, queueConfig);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.log("[central-amqp] Error in initializing the rabbitmq exchanges and queues");
|
|
41
|
+
console.error(error);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
CentralRabbitMQHelper.initializeAndBindQueue = async (rabbitmqChannel, exchangeName, queueConfig) => {
|
|
46
|
+
try {
|
|
47
|
+
const {
|
|
48
|
+
queueName,
|
|
49
|
+
routingKeyList,
|
|
50
|
+
priorityQueue,
|
|
51
|
+
maxPriority
|
|
52
|
+
} = queueConfig;
|
|
53
|
+
let queue_name = applyQueueName(queueName);
|
|
54
|
+
await rabbitmqChannel.assertQueue(queue_name, {
|
|
55
|
+
durable: true,
|
|
56
|
+
...(priorityQueue && {
|
|
57
|
+
arguments: {
|
|
58
|
+
"x-max-priority": maxPriority
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
});
|
|
62
|
+
await queueLogger(`${queue_name} queue created!`, "central-amqp");
|
|
63
|
+
for (const routingKey of routingKeyList) {
|
|
64
|
+
let routing_key = applyRoutingKey(queueName, routingKey);
|
|
65
|
+
rabbitmqChannel.bindQueue(queue_name, exchangeName, routing_key);
|
|
66
|
+
await bindingLogger([`${exchangeName}`, `${queue_name} queue with routing key ${routing_key}`], "central-amqp");
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.log(`[central-amqp] Error while initializing and binding queue`);
|
|
70
|
+
console.error(error);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* This function is used publish into the exchange.
|
|
76
|
+
* @param exchangeName
|
|
77
|
+
* @param routingKey
|
|
78
|
+
* @param message
|
|
79
|
+
* @param priority (Optional)
|
|
80
|
+
* @param cacheKey (Optional)
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
CentralRabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0, cacheKey = null) => {
|
|
84
|
+
try {
|
|
85
|
+
const rabbitmqChannel = await centralRabbitmq();
|
|
86
|
+
const parsedMessage = typeof message === "object" ? JSON.stringify(message) : message;
|
|
87
|
+
const cacheGeneratedKey = cacheKey || randomUUID();
|
|
88
|
+
let exchange_name = applyExchangeName(exchangeName);
|
|
89
|
+
let routing_key = applyRoutingKeyIsolationForPublish(routingKey);
|
|
90
|
+
if (CachingEnabled) {
|
|
91
|
+
if (cacheKey) {} else {
|
|
92
|
+
const setCacheResponse = await CacheService.setCache(cacheGeneratedKey, {
|
|
93
|
+
exchange_name,
|
|
94
|
+
routing_key,
|
|
95
|
+
priority,
|
|
96
|
+
data: message
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const publishResponse = await rabbitmqChannel.publish(exchange_name, routing_key, Buffer.from(parsedMessage), {
|
|
101
|
+
persistent: true,
|
|
102
|
+
mandatory: true,
|
|
103
|
+
...(priority && {
|
|
104
|
+
priority
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
if (!publishResponse) {
|
|
108
|
+
const message = `[central-amqp] Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
109
|
+
console.log(message);
|
|
110
|
+
// offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
111
|
+
return {
|
|
112
|
+
error: true,
|
|
113
|
+
message: message,
|
|
114
|
+
data: null
|
|
115
|
+
};
|
|
116
|
+
} else {
|
|
117
|
+
console.log("[central-amqp] Sent %s to '%s'", routing_key, exchange_name);
|
|
118
|
+
if (CachingEnabled) {
|
|
119
|
+
const clearCacheByKeyResponse = await CacheService.clearCacheByKey(cacheGeneratedKey);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
error: false,
|
|
123
|
+
message: `[central-amqp] Message Published Successfully into the ${exchange_name}`,
|
|
124
|
+
data: null
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
let exchange_name = applyExchangeName(exchangeName);
|
|
129
|
+
let routing_key = applyRoutingKeyIsolationForPublish(routingKey);
|
|
130
|
+
const message = `[central-amqp] Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
131
|
+
console.log(message, error.message);
|
|
132
|
+
// offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
133
|
+
return {
|
|
134
|
+
error: true,
|
|
135
|
+
message: message,
|
|
136
|
+
data: null
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* This function is used to delete queue.
|
|
143
|
+
* @param {String} queueName
|
|
144
|
+
* @returns
|
|
145
|
+
*/
|
|
146
|
+
CentralRabbitMQHelper.deleteQueue = async queueName => {
|
|
147
|
+
try {
|
|
148
|
+
const rabbitmqChannel = await centralRabbitmq();
|
|
149
|
+
await rabbitmqChannel.deleteQueue(queueName);
|
|
150
|
+
console.log(`[central-amqp] Queue ${queueName} deleted successfully`);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.log(`[central-amqp] Error while deleting queue ${queueName}`);
|
|
153
|
+
console.error(error);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
module.exports = CentralRabbitMQHelper;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const amqp = require("amqplib");
|
|
2
|
+
const {
|
|
3
|
+
colorLogger
|
|
4
|
+
} = require("../../utils/log.util");
|
|
5
|
+
const {
|
|
6
|
+
timeout
|
|
7
|
+
} = require("../../utils/func.util");
|
|
8
|
+
const {
|
|
9
|
+
amqpReconnectTimeout
|
|
10
|
+
} = require("../../configs/main.configs");
|
|
11
|
+
const mbConfigs = require("../../configs/mb.configs");
|
|
12
|
+
let centralRabbitmqChannel;
|
|
13
|
+
async function centralRabbitmq() {
|
|
14
|
+
if (centralRabbitmqChannel) return centralRabbitmqChannel;
|
|
15
|
+
try {
|
|
16
|
+
connection = await amqp.connect(mbConfigs.centralRabbitmqUrl + "?heartbeat=60");
|
|
17
|
+
centralRabbitmqChannel = await connection.createConfirmChannel();
|
|
18
|
+
connection.on("error", err => {
|
|
19
|
+
if (err.message !== "Connection closing") {
|
|
20
|
+
console.error("[central-amqp] central rabbitmq conn error", err.message);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
connection.on("close", async () => {
|
|
24
|
+
console.error("[central-amqp] central rabbitmq reconnecting...");
|
|
25
|
+
centralRabbitmqChannel = null;
|
|
26
|
+
await timeout(amqpReconnectTimeout);
|
|
27
|
+
return centralRabbitmq();
|
|
28
|
+
});
|
|
29
|
+
await colorLogger("fe6f27", "ffe042", "143d59", `Connected to central rabbitmq successfully!`, "[central-amqp]");
|
|
30
|
+
return centralRabbitmqChannel;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("[central-amqp] Error connecting to central rabbitmq:", error.message);
|
|
33
|
+
centralRabbitmqChannel = null;
|
|
34
|
+
await timeout(amqpReconnectTimeout);
|
|
35
|
+
return centralRabbitmq();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
module.exports = centralRabbitmq;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "message-brokers",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.1-beta",
|
|
4
4
|
"description": "This package contains functions to publish the messages into the message brokers like rabbitmq.",
|
|
5
5
|
"main": "lib/message-brokers.js",
|
|
6
6
|
"types": "lib/types/message-brokers.d.ts",
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
"README.md",
|
|
11
11
|
"package.json"
|
|
12
12
|
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./lib/message-brokers.js",
|
|
15
|
+
"./rabbitmq-utils": "./lib/utils/rabbitmq.util.js"
|
|
16
|
+
},
|
|
13
17
|
"scripts": {
|
|
14
18
|
"install_deps": "NODE_ENV='development' yarn",
|
|
15
19
|
"build": "babel ./src -d ./lib && cp -r ./src/types/message-brokers.d.ts ./lib/types",
|