@palmetto/pubsub 2.2.7 → 3.0.0
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
CHANGED
|
@@ -19,7 +19,7 @@ It is up to the pub/sub implementations what to do with rejected messages. Optio
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
21
21
|
```sh
|
|
22
|
-
yarn add @palmetto/pubsub zod
|
|
22
|
+
yarn add @palmetto/pubsub zod @palmetto/trace dd-trace
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
@@ -120,9 +120,9 @@ yarn add @palmetto/pubsub zod
|
|
|
120
120
|
|
|
121
121
|
```ts
|
|
122
122
|
import tracer from "dd-trace";
|
|
123
|
-
import {
|
|
123
|
+
import { registerDdTracer } from "@palmetto/trace";
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
registerDdTracer(tracer);
|
|
126
126
|
```
|
|
127
127
|
|
|
128
128
|
### Message logging
|
|
@@ -10,11 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.BullMqSubscriber = exports.MessageRetryError = exports.MessageFailError = void 0;
|
|
13
|
+
const trace_1 = require("@palmetto/trace");
|
|
13
14
|
const interfaces_js_1 = require("../interfaces.js");
|
|
14
15
|
const connection_js_1 = require("./connection.js");
|
|
15
16
|
const lazy_load_js_1 = require("../lazy-load.js");
|
|
16
17
|
const create_log_error_payload_js_1 = require("../create-log-error-payload.js");
|
|
17
|
-
const dd_trace_api_1 = require("dd-trace-api");
|
|
18
18
|
class SubscribedMessage {
|
|
19
19
|
constructor(owner, worker, logger) {
|
|
20
20
|
this.owner = owner;
|
|
@@ -65,7 +65,7 @@ class BullMqSubscriber {
|
|
|
65
65
|
context: "BullMqSubscriber",
|
|
66
66
|
});
|
|
67
67
|
const worker = new BullMqPackage.Worker(config.name, (job) => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
return yield
|
|
68
|
+
return yield (0, trace_1.getTracer)().trace("pubsub.bullmq.handle", {
|
|
69
69
|
resource: `handle ${config.name}`,
|
|
70
70
|
}, () => __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
const context = {
|
package/dist/publisher.js
CHANGED
|
@@ -13,9 +13,9 @@ exports.Publisher = void 0;
|
|
|
13
13
|
const zod_1 = require("zod");
|
|
14
14
|
const uuid_1 = require("uuid");
|
|
15
15
|
const crypto_1 = require("crypto");
|
|
16
|
+
const trace_1 = require("@palmetto/trace");
|
|
16
17
|
const errors_js_1 = require("./errors.js");
|
|
17
18
|
const message_logger_js_1 = require("./message-logger.js");
|
|
18
|
-
const dd_trace_api_1 = require("dd-trace-api");
|
|
19
19
|
class Publisher {
|
|
20
20
|
constructor(logger, providers) {
|
|
21
21
|
var _a, _b;
|
|
@@ -57,7 +57,7 @@ class Publisher {
|
|
|
57
57
|
if (messages.length === 0) {
|
|
58
58
|
return Promise.resolve();
|
|
59
59
|
}
|
|
60
|
-
return
|
|
60
|
+
return (0, trace_1.getTracer)().trace("pubsub.publish", {
|
|
61
61
|
resource: `publish ${config.transport} ${config.name}`,
|
|
62
62
|
}, (span) => {
|
|
63
63
|
return this.publishImpl(config, messages, span);
|
|
@@ -117,10 +117,10 @@ class RabbitMqPublisher {
|
|
|
117
117
|
timestamp: Date.now(),
|
|
118
118
|
persistent: true,
|
|
119
119
|
};
|
|
120
|
-
const oks = yield Promise.
|
|
120
|
+
const oks = yield Promise.allSettled(messages.map((message) => sender(Buffer.from(message, "utf8"), options)));
|
|
121
121
|
const failedMessages = oks
|
|
122
122
|
.map((ok, index) => ({ ok, index }))
|
|
123
|
-
.filter(({ ok }) =>
|
|
123
|
+
.filter(({ ok }) => ok.status === "rejected")
|
|
124
124
|
.map(({ index }) => messages[index]);
|
|
125
125
|
if (failedMessages.length > 0) {
|
|
126
126
|
const allOrSome = failedMessages.length === messages.length ? "all" : "some";
|
|
@@ -6,12 +6,13 @@ import { RabbitMqConnection } from "./connection.js";
|
|
|
6
6
|
declare class SubscribedMessage {
|
|
7
7
|
private readonly owner;
|
|
8
8
|
private readonly logger;
|
|
9
|
+
private readonly queueName;
|
|
9
10
|
readonly config: RabbitQueueExchangeConfiguration;
|
|
10
11
|
readonly onMessage: (s: string, context: RabbitMqMessageContext) => Promise<MessageResult> | MessageResult;
|
|
11
12
|
stop?: StopSubscribe | undefined;
|
|
12
13
|
channel?: ChannelWrapper;
|
|
13
14
|
busy: number;
|
|
14
|
-
constructor(owner: RabbitMqSubscriber, logger: Logger, config: RabbitQueueExchangeConfiguration, onMessage: (s: string, context: RabbitMqMessageContext) => Promise<MessageResult> | MessageResult, stop?: StopSubscribe | undefined);
|
|
15
|
+
constructor(owner: RabbitMqSubscriber, logger: Logger, queueName: string, config: RabbitQueueExchangeConfiguration, onMessage: (s: string, context: RabbitMqMessageContext) => Promise<MessageResult> | MessageResult, stop?: StopSubscribe | undefined);
|
|
15
16
|
stopSubscribe(): Promise<void>;
|
|
16
17
|
}
|
|
17
18
|
export declare class RabbitMqSubscriber implements SubscriberProvider {
|
|
@@ -10,16 +10,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.RabbitMqSubscriber = void 0;
|
|
13
|
+
const trace_1 = require("@palmetto/trace");
|
|
13
14
|
const interfaces_js_1 = require("../interfaces.js");
|
|
14
15
|
const errors_js_1 = require("../errors.js");
|
|
15
16
|
const config_js_1 = require("./config.js");
|
|
16
17
|
const connection_js_1 = require("./connection.js");
|
|
17
18
|
const create_log_error_payload_js_1 = require("../create-log-error-payload.js");
|
|
18
|
-
const dd_trace_api_1 = require("dd-trace-api");
|
|
19
19
|
class SubscribedMessage {
|
|
20
|
-
constructor(owner, logger, config, onMessage, stop) {
|
|
20
|
+
constructor(owner, logger, queueName, config, onMessage, stop) {
|
|
21
21
|
this.owner = owner;
|
|
22
22
|
this.logger = logger;
|
|
23
|
+
this.queueName = queueName;
|
|
23
24
|
this.config = config;
|
|
24
25
|
this.onMessage = onMessage;
|
|
25
26
|
this.stop = stop;
|
|
@@ -34,16 +35,16 @@ class SubscribedMessage {
|
|
|
34
35
|
this.stop = undefined;
|
|
35
36
|
this.owner.removeSubscriber(this);
|
|
36
37
|
if (this.busy) {
|
|
37
|
-
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a,
|
|
38
|
-
const start =
|
|
38
|
+
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `RabbitMq subscriber waiting for ${this.queueName} handler to finish`);
|
|
39
|
+
const start = Date.now();
|
|
39
40
|
const waitDelay = 5;
|
|
40
|
-
let busyCheck =
|
|
41
|
+
let busyCheck = 5000 / waitDelay; // wait a few seconds before shutdown
|
|
41
42
|
while (this.busy && busyCheck > 0) {
|
|
42
43
|
yield new Promise((resolve) => setTimeout(resolve, waitDelay));
|
|
43
44
|
busyCheck -= waitDelay;
|
|
44
45
|
}
|
|
45
|
-
const delay =
|
|
46
|
-
(_d = (_c = this.logger).debug) === null || _d === void 0 ? void 0 : _d.call(_c, `RabbitMq subscriber waited ${delay}ms for handler to finish`);
|
|
46
|
+
const delay = Date.now() - start;
|
|
47
|
+
(_d = (_c = this.logger).debug) === null || _d === void 0 ? void 0 : _d.call(_c, `RabbitMq subscriber waited ${delay}ms for ${this.queueName} handler to finish`);
|
|
47
48
|
}
|
|
48
49
|
yield s();
|
|
49
50
|
}
|
|
@@ -73,7 +74,7 @@ class RabbitMqSubscriber {
|
|
|
73
74
|
if (subscribedMessage) {
|
|
74
75
|
throw new errors_js_1.AlreadySubscribingError(queueName);
|
|
75
76
|
}
|
|
76
|
-
subscribedMessage = new SubscribedMessage(this, this.logger, config, onMessage);
|
|
77
|
+
subscribedMessage = new SubscribedMessage(this, this.logger, queueName, config, onMessage);
|
|
77
78
|
this.subscribers.set(queueName, subscribedMessage);
|
|
78
79
|
subscribedMessage.channel = this.connection.connection.createChannel({
|
|
79
80
|
setup: (channel) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -84,9 +85,9 @@ class RabbitMqSubscriber {
|
|
|
84
85
|
}
|
|
85
86
|
close() {
|
|
86
87
|
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
yield Promise.all(this.subscribers
|
|
89
|
+
.values()
|
|
90
|
+
.map((subscribedMessage) => subscribedMessage.stopSubscribe()));
|
|
90
91
|
this.subscribers.clear();
|
|
91
92
|
});
|
|
92
93
|
}
|
|
@@ -123,7 +124,7 @@ class RabbitMqSubscriber {
|
|
|
123
124
|
? setupResult.dlQueueName
|
|
124
125
|
: setupResult.queueName;
|
|
125
126
|
const onRabbitMessage = (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
yield
|
|
127
|
+
yield (0, trace_1.getTracer)().trace("pubsub.rabbit.consume", {
|
|
127
128
|
resource: `consume ${config.transport} ${queueName}`,
|
|
128
129
|
}, (span) => __awaiter(this, void 0, void 0, function* () {
|
|
129
130
|
try {
|
|
@@ -135,7 +136,7 @@ class RabbitMqSubscriber {
|
|
|
135
136
|
message: "Unexpected error handling RabbitMq message",
|
|
136
137
|
error: (0, create_log_error_payload_js_1.createLogErrorPayload)(err),
|
|
137
138
|
});
|
|
138
|
-
span
|
|
139
|
+
span.setTag("error", err);
|
|
139
140
|
try {
|
|
140
141
|
channel.nack(msg, undefined, true);
|
|
141
142
|
}
|
|
@@ -163,9 +164,10 @@ class RabbitMqSubscriber {
|
|
|
163
164
|
this.logger.log(`RabbitMQ consumer started for ${queueName}`);
|
|
164
165
|
// stop will cancel the subscriber
|
|
165
166
|
subscribedMessage.stop = () => __awaiter(this, void 0, void 0, function* () {
|
|
166
|
-
var _a, _b;
|
|
167
|
+
var _a, _b, _c, _d;
|
|
168
|
+
(_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `RabbitMQ consumer stopping for ${queueName}`);
|
|
167
169
|
yield channel.close();
|
|
168
|
-
(
|
|
170
|
+
(_d = (_c = this.logger).debug) === null || _d === void 0 ? void 0 : _d.call(_c, `RabbitMQ consumer stopped for ${queueName}`);
|
|
169
171
|
});
|
|
170
172
|
});
|
|
171
173
|
}
|
|
@@ -207,7 +209,7 @@ class RabbitMqSubscriber {
|
|
|
207
209
|
timestamp: (_d = sentDates.firstSent) === null || _d === void 0 ? void 0 : _d.valueOf(),
|
|
208
210
|
headers: {
|
|
209
211
|
[RETRIES_HEADER]: retries + 1,
|
|
210
|
-
[RETRYSENT_HEADER]:
|
|
212
|
+
[RETRYSENT_HEADER]: Date.now(),
|
|
211
213
|
},
|
|
212
214
|
}));
|
|
213
215
|
if (ok) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@palmetto/pubsub",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"main": "./dist/main.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "yarn run -T eslint --fix ./src",
|
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
"test:watch": "yarn run test-runner vitest watch"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"@palmetto/trace": "^0.0.1",
|
|
23
24
|
"@types/amqplib": "^0",
|
|
24
25
|
"@types/node": "^24.2.1",
|
|
25
26
|
"amqp-connection-manager": "^4.1.14",
|
|
26
27
|
"amqplib": "^0.10.8",
|
|
27
28
|
"bullmq": "^5.58.0",
|
|
28
|
-
"opentracing": "^0.14.7",
|
|
29
29
|
"ts-node": "^10.9.2",
|
|
30
30
|
"typescript": "^5.8.3",
|
|
31
31
|
"vitest": "^3.2.4",
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"dd-trace-api": "^1.0.0",
|
|
46
45
|
"uuid": "^11.1.0"
|
|
47
46
|
},
|
|
48
47
|
"peerDependencies": {
|
|
48
|
+
"@palmetto/trace": "^0.0.1",
|
|
49
49
|
"amqp-connection-manager": "^4.1.14",
|
|
50
50
|
"amqplib": "^0.10.8",
|
|
51
51
|
"bullmq": "^5.58.0",
|