message-brokers 1.11.0-beta.1 → 1.11.0-beta.3
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.
- package/README.md +57 -1
- package/lib/configs/mb.configs.js +3 -2
- package/lib/types/rabbitmq-utils.d.ts +35 -0
- package/package.json +22 -3
package/README.md
CHANGED
|
@@ -55,7 +55,7 @@ RABBITMQ_DEPLOYMENT_ENV=stage
|
|
|
55
55
|
#
|
|
56
56
|
# Example:
|
|
57
57
|
# base.queue → base.queue.stage
|
|
58
|
-
# base-key → base-key
|
|
58
|
+
# base-key → base-key-stage
|
|
59
59
|
#
|
|
60
60
|
# Use this when multiple environments (stage/dev/prod) share the same RabbitMQ cluster.
|
|
61
61
|
|
|
@@ -74,6 +74,7 @@ RABBITMQ_ISOLATED_NAMESPACE=prod-2
|
|
|
74
74
|
#
|
|
75
75
|
# Example:
|
|
76
76
|
# messaging.queue → messaging.queue.prod-2
|
|
77
|
+
# messaging-key → messaging-key-prod-2
|
|
77
78
|
#
|
|
78
79
|
# Purpose:
|
|
79
80
|
# - Enables tenant-level or client-level isolation within the same broker
|
|
@@ -193,6 +194,61 @@ messageBrokers.publishPastDataReminderRequest(reminder_request);
|
|
|
193
194
|
messageBrokers.publishPastDataReminderMessage(reminder_message);
|
|
194
195
|
```
|
|
195
196
|
|
|
197
|
+
## RabbitMQ Utils Usage
|
|
198
|
+
|
|
199
|
+
The queue / exchange / routing key naming logic (`RABBITMQ_ISOLATED_NAMESPACE` and `RABBITMQ_DEPLOYMENT_ENV`) can be imported on its own, so other apps build the same names without duplicating it.
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
// ES5 Syntax
|
|
203
|
+
const {
|
|
204
|
+
applyExchangeName,
|
|
205
|
+
applyQueueName,
|
|
206
|
+
applyRoutingKey,
|
|
207
|
+
} = require("message-brokers/rabbitmq-utils");
|
|
208
|
+
// ES6 Syntax
|
|
209
|
+
import { applyExchangeName, applyQueueName, applyRoutingKey } from "message-brokers/rabbitmq-utils";
|
|
210
|
+
|
|
211
|
+
const exchange = "hv.exchange";
|
|
212
|
+
const queue = "billing.events";
|
|
213
|
+
const routingKey = "billing";
|
|
214
|
+
|
|
215
|
+
const newExchangeName = applyExchangeName(exchange); // hv.exchange.stage
|
|
216
|
+
const newQueueName = applyQueueName(queue); // billing.events.prod-2.stage
|
|
217
|
+
const newRoutingKey = applyRoutingKey(queue, routingKey); // billing-prod-2-stage
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
The example output assumes `RABBITMQ_ISOLATED_NAMESPACE=prod-2` and `RABBITMQ_DEPLOYMENT_ENV=stage`; the namespace is only added for the isolated queues / routing keys.
|
|
221
|
+
|
|
222
|
+
The package's `exports` allow only `message-brokers`, `message-brokers/rabbitmq-utils`, `message-brokers/lib/*` and `message-brokers/package.json` to be imported.
|
|
223
|
+
|
|
224
|
+
## Queue isolation
|
|
225
|
+
|
|
226
|
+
Some deployments run two or more servers (stacks) against the same RabbitMQ with the same `RABBITMQ_DEPLOYMENT_ENV`. Each server then sets its own `RABBITMQ_ISOLATED_NAMESPACE` (e.g. `prod-1`, `prod-2`).
|
|
227
|
+
|
|
228
|
+
- **Shared queue (not isolated):** every server binds the same queue and routing key, so a message published by one server can be consumed by any of them. The data flows across servers.
|
|
229
|
+
- **Isolated queue:** each server gets its own queue (`<queue>.<ns>.<env>`) and routing key (`<key>-<ns>-<env>`), so a message stays on the server that published it.
|
|
230
|
+
|
|
231
|
+
Isolation is decided per queue, because one deployment usually needs both kinds. It's configured in `src/configs/mb.configs.js`:
|
|
232
|
+
|
|
233
|
+
- `rabbitmqIsolatedQueues`: queue names. Bindings use these to get the namespace.
|
|
234
|
+
- `rabbitmqIsolatedRoutingKeys`: routing keys. Publishing uses these to get the namespace.
|
|
235
|
+
|
|
236
|
+
An isolated queue must be on **both** lists, otherwise the publish key and the binding key differ and messages are dropped. Both lists are matched by prefix.
|
|
237
|
+
|
|
238
|
+
| Queue | Isolated |
|
|
239
|
+
|---|---|
|
|
240
|
+
| `data-flattening.har.queue` | no |
|
|
241
|
+
| `messaging.har.lab-api.queue` | yes |
|
|
242
|
+
| `datacloak.queue` | no |
|
|
243
|
+
| `raw-queue-processor` | no |
|
|
244
|
+
| `v2.lab-data.queue` | yes |
|
|
245
|
+
| `v2.merged-lab-data.queue` | yes |
|
|
246
|
+
| `billing.events` | yes |
|
|
247
|
+
| `llm-reminder.past-data.queue` | yes |
|
|
248
|
+
| `messaging.past-data.reminder-message.queue` | yes |
|
|
249
|
+
|
|
250
|
+
When you add a queue, decide whether its data may cross servers or must stay on one. Changing the isolation of an existing queue renames it on deployments that set `RABBITMQ_ISOLATED_NAMESPACE`. Upgrade the publisher and the consumers together, then drain and delete the old queue, because the package doesn't remove it.
|
|
251
|
+
|
|
196
252
|
## Publish options
|
|
197
253
|
|
|
198
254
|
Every publish method takes an optional last argument `options` (since 1.11.0). Everything is off by default: a call without options behaves as before, except that the message now gets a `message_id`.
|
|
@@ -5,8 +5,9 @@ const mbConfigs = {
|
|
|
5
5
|
cachingEnabled: process.env.CACHING_ENABLED || false,
|
|
6
6
|
// Queues that are used in env where multiple instances of same stack share the same rabbitmq.
|
|
7
7
|
// To avoid messages between different instances, we are isolating the queues.
|
|
8
|
-
rabbitmqIsolatedQueues: ["messaging.har.lab-api.queue", "billing.events", "llm-reminder.past-data.queue", "messaging.past-data.reminder-message.queue"],
|
|
8
|
+
rabbitmqIsolatedQueues: ["messaging.har.lab-api.queue", "billing.events", "llm-reminder.past-data.queue", "messaging.past-data.reminder-message.queue", "v2.lab-data.queue", "v2.merged-lab-data.queue"],
|
|
9
9
|
// Matched by prefix: "past_data." covers past_data.reminder_request and past_data.reminder_message.
|
|
10
|
-
|
|
10
|
+
// The lab-data keys are listed in full so the plain "lab_api_response" key (datacloak.queue) stays shared.
|
|
11
|
+
rabbitmqIsolatedRoutingKeys: ["har.lab_api_response", "billing", "past_data.", "lab_api_response.amqp", "merged_lab_api_response.amqp"]
|
|
11
12
|
};
|
|
12
13
|
module.exports = mbConfigs;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RabbitMQ naming helpers, importable on their own:
|
|
3
|
+
* const { applyQueueName } = require("message-brokers/rabbitmq-utils");
|
|
4
|
+
*
|
|
5
|
+
* Names are built from RABBITMQ_ISOLATED_NAMESPACE (only for the isolated queues / routing keys
|
|
6
|
+
* listed in the package's mb.configs) and RABBITMQ_DEPLOYMENT_ENV (for every name).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Whether the queue starts with one of the isolated queue prefixes.
|
|
11
|
+
*/
|
|
12
|
+
export function isIsolatedQueue(queueName: string): boolean;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Queue name with ".<RABBITMQ_ISOLATED_NAMESPACE>" (isolated queues only) and
|
|
16
|
+
* ".<RABBITMQ_DEPLOYMENT_ENV>" appended when they are set.
|
|
17
|
+
*/
|
|
18
|
+
export function applyQueueName(queueName: string): string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Routing key with "-<RABBITMQ_ISOLATED_NAMESPACE>" (when the queue is isolated) and
|
|
22
|
+
* "-<RABBITMQ_DEPLOYMENT_ENV>" appended when they are set.
|
|
23
|
+
*/
|
|
24
|
+
export function applyRoutingKey(queueName: string, routingKey: string): string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Exchange name with ".<RABBITMQ_DEPLOYMENT_ENV>" appended when it is set (exchanges are never isolated).
|
|
28
|
+
*/
|
|
29
|
+
export function applyExchangeName(exchangeName: string): string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Routing key used when publishing: "-<RABBITMQ_ISOLATED_NAMESPACE>" (when the routing key starts
|
|
33
|
+
* with one of the isolated routing key prefixes) and "-<RABBITMQ_DEPLOYMENT_ENV>" appended when they are set.
|
|
34
|
+
*/
|
|
35
|
+
export function applyRoutingKeyIsolationForPublish(routingKey: string): string;
|
package/package.json
CHANGED
|
@@ -1,19 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "message-brokers",
|
|
3
|
-
"version": "1.11.0-beta.
|
|
3
|
+
"version": "1.11.0-beta.3",
|
|
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",
|
|
7
|
+
"typesVersions": {
|
|
8
|
+
"*": {
|
|
9
|
+
"rabbitmq-utils": [
|
|
10
|
+
"lib/types/rabbitmq-utils.d.ts"
|
|
11
|
+
]
|
|
12
|
+
}
|
|
13
|
+
},
|
|
7
14
|
"files": [
|
|
8
15
|
"lib",
|
|
9
16
|
"LICENSE",
|
|
10
17
|
"README.md",
|
|
11
18
|
"package.json"
|
|
12
19
|
],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./lib/types/message-brokers.d.ts",
|
|
23
|
+
"default": "./lib/message-brokers.js"
|
|
24
|
+
},
|
|
25
|
+
"./rabbitmq-utils": {
|
|
26
|
+
"types": "./lib/types/rabbitmq-utils.d.ts",
|
|
27
|
+
"default": "./lib/utils/rabbitmq.utils.js"
|
|
28
|
+
},
|
|
29
|
+
"./lib/*": "./lib/*",
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
13
32
|
"scripts": {
|
|
14
33
|
"install_deps": "NODE_ENV='development' yarn",
|
|
15
|
-
"build": "babel ./src -d ./lib && mkdir -p ./lib/types && cp ./src/types
|
|
16
|
-
"watch": "mkdir -p ./lib/types && cp ./src/types
|
|
34
|
+
"build": "babel ./src -d ./lib && mkdir -p ./lib/types && cp ./src/types/*.d.ts ./lib/types/",
|
|
35
|
+
"watch": "mkdir -p ./lib/types && cp ./src/types/*.d.ts ./lib/types/ && babel ./src -d ./lib --watch",
|
|
17
36
|
"prepublishOnly": "npm run build"
|
|
18
37
|
},
|
|
19
38
|
"repository": {
|