@rxdi/rabbitmq-pubsub 0.7.257 → 0.7.259
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/dist/subscriber.d.ts +13 -0
- package/dist/subscriber.js +73 -11
- package/package.json +1 -1
package/dist/subscriber.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { Channel, Message, Options } from "amqplib";
|
|
|
2
2
|
import { IRabbitMqConnectionFactory } from "./connectionFactory";
|
|
3
3
|
import { IQueueNameConfig } from "./common";
|
|
4
4
|
import { Logger } from "./childLogger";
|
|
5
|
+
/** Wait before subscribing again after a lost connection / channel / consumer. */
|
|
6
|
+
export declare const RESUBSCRIBE_DELAY_MS = 5000;
|
|
5
7
|
export interface IRabbitMqSubscriberDisposer {
|
|
6
8
|
(): Promise<void>;
|
|
7
9
|
}
|
|
@@ -9,6 +11,17 @@ export declare class RabbitMqSubscriber {
|
|
|
9
11
|
private logger;
|
|
10
12
|
private connectionFactory;
|
|
11
13
|
constructor(logger: Logger, connectionFactory: IRabbitMqConnectionFactory);
|
|
14
|
+
/**
|
|
15
|
+
* Subscribes to a queue and KEEPS the subscription: when the connection,
|
|
16
|
+
* the channel or the consumer is lost (the broker restarted, the queue was
|
|
17
|
+
* deleted, the first connect failed because the broker was not up yet) it
|
|
18
|
+
* subscribes again after RESUBSCRIBE_DELAY_MS, until disposed.
|
|
19
|
+
*
|
|
20
|
+
* Before, a subscription was made once. A service that survived a broker
|
|
21
|
+
* restart stayed up and deaf - no ticks, no jobs - until someone restarted
|
|
22
|
+
* it; one that started while the broker was down never listened at all
|
|
23
|
+
* (2026-11-17, a RabbitMQ pod evicted twice).
|
|
24
|
+
*/
|
|
12
25
|
subscribe<T>(queue: string | IQueueNameConfig, action: (message: T) => Promise<IRabbitMqSubscriberDisposer>, options?: Partial<IQueueNameConfig>): Promise<IRabbitMqSubscriberDisposer>;
|
|
13
26
|
private setupChannel;
|
|
14
27
|
private subscribeToChannel;
|
package/dist/subscriber.js
CHANGED
|
@@ -9,28 +9,83 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.RabbitMqSubscriber = void 0;
|
|
12
|
+
exports.RabbitMqSubscriber = exports.RESUBSCRIBE_DELAY_MS = void 0;
|
|
13
13
|
const common_1 = require("./common");
|
|
14
14
|
const childLogger_1 = require("./childLogger");
|
|
15
|
+
/** Wait before subscribing again after a lost connection / channel / consumer. */
|
|
16
|
+
exports.RESUBSCRIBE_DELAY_MS = 5000;
|
|
15
17
|
class RabbitMqSubscriber {
|
|
16
18
|
constructor(logger, connectionFactory) {
|
|
17
19
|
this.logger = logger;
|
|
18
20
|
this.connectionFactory = connectionFactory;
|
|
19
21
|
this.logger = (0, childLogger_1.createChildLogger)(logger, "RabbitMqConsumer");
|
|
20
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Subscribes to a queue and KEEPS the subscription: when the connection,
|
|
25
|
+
* the channel or the consumer is lost (the broker restarted, the queue was
|
|
26
|
+
* deleted, the first connect failed because the broker was not up yet) it
|
|
27
|
+
* subscribes again after RESUBSCRIBE_DELAY_MS, until disposed.
|
|
28
|
+
*
|
|
29
|
+
* Before, a subscription was made once. A service that survived a broker
|
|
30
|
+
* restart stayed up and deaf - no ticks, no jobs - until someone restarted
|
|
31
|
+
* it; one that started while the broker was down never listened at all
|
|
32
|
+
* (2026-11-17, a RabbitMQ pod evicted twice).
|
|
33
|
+
*/
|
|
21
34
|
subscribe(queue, action, options) {
|
|
22
35
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
36
|
const queueConfig = (0, common_1.asPubSubQueueNameConfig)(queue);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
let disposed = false;
|
|
38
|
+
let dispose;
|
|
39
|
+
let pending;
|
|
40
|
+
const again = (why) => {
|
|
41
|
+
if (disposed || pending) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
this.logger.error("subscription to queue '%s' lost (%s) - subscribing again in %d ms", queueConfig.name, why, exports.RESUBSCRIBE_DELAY_MS);
|
|
45
|
+
dispose = undefined;
|
|
46
|
+
pending = setTimeout(() => {
|
|
47
|
+
pending = undefined;
|
|
48
|
+
void attempt();
|
|
49
|
+
}, exports.RESUBSCRIBE_DELAY_MS);
|
|
50
|
+
};
|
|
51
|
+
const attempt = () => __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
if (disposed) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const connection = yield this.connectionFactory.create();
|
|
57
|
+
const channel = yield connection.createChannel();
|
|
58
|
+
// an 'error' nobody listens to would crash the process; a channel
|
|
59
|
+
// also closes when its connection does
|
|
60
|
+
channel.on("error", (err) => this.logger.error(err, "channel error on queue '%s'", queueConfig.name));
|
|
61
|
+
channel.on("close", () => again("channel closed"));
|
|
62
|
+
if (options === null || options === void 0 ? void 0 : options.prefetch) {
|
|
63
|
+
yield channel.prefetch(options.prefetch, options.globalPrefetch);
|
|
64
|
+
}
|
|
65
|
+
this.logger.trace("got channel for queue '%s'", queueConfig.name);
|
|
66
|
+
const queueName = yield this.setupChannel(channel, Object.assign(Object.assign({}, queueConfig), options));
|
|
67
|
+
this.logger.debug("queue name generated for subscription queue '(%s)' is '(%s)'", queueConfig.name, queueName);
|
|
68
|
+
const queConfig = Object.assign(Object.assign(Object.assign({}, queueConfig), options), { dlq: queueName });
|
|
69
|
+
dispose = yield this.subscribeToChannel(channel, queConfig, action);
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
this.logger.error(err, "failed to subscribe to queue '%s'", queueConfig.name);
|
|
73
|
+
again("subscribe failed");
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
yield attempt();
|
|
77
|
+
return (() => __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
disposed = true;
|
|
79
|
+
if (pending) {
|
|
80
|
+
clearTimeout(pending);
|
|
81
|
+
pending = undefined;
|
|
82
|
+
}
|
|
83
|
+
const current = dispose;
|
|
84
|
+
dispose = undefined;
|
|
85
|
+
if (current) {
|
|
86
|
+
yield current();
|
|
87
|
+
}
|
|
88
|
+
}));
|
|
34
89
|
});
|
|
35
90
|
}
|
|
36
91
|
setupChannel(channel, queueConfig) {
|
|
@@ -42,6 +97,13 @@ class RabbitMqSubscriber {
|
|
|
42
97
|
this.logger.trace("subscribing to queue '%s'", queueConfig.name);
|
|
43
98
|
const opts = yield channel.consume(queueConfig.dlq, (message) => __awaiter(this, void 0, void 0, function* () {
|
|
44
99
|
var _a, _b;
|
|
100
|
+
if (message === null) {
|
|
101
|
+
// the broker cancelled the consumer (its queue was deleted): closing
|
|
102
|
+
// the channel makes `subscribe` set everything up again
|
|
103
|
+
this.logger.error("consumer of queue '%s' was cancelled by the broker", queueConfig.name);
|
|
104
|
+
yield channel.close().catch(() => undefined);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
45
107
|
let msg = this.getMessageObject(message);
|
|
46
108
|
this.logger.trace("message arrived from queue '%s' (%j)", queueConfig.name, msg);
|
|
47
109
|
try {
|