@imqueue/core 2.0.24 → 2.0.26
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/package.json +1 -1
- package/src/ClusteredRedisQueue.d.ts +1 -0
- package/src/ClusteredRedisQueue.js +10 -1
- package/src/RedisQueue.d.ts +0 -11
- package/src/RedisQueue.js +58 -76
- package/src/UDPClusterManager.d.ts +10 -0
- package/src/UDPClusterManager.js +2 -0
- package/src/UDPWorker.js +9 -7
package/package.json
CHANGED
|
@@ -144,6 +144,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
144
144
|
*/
|
|
145
145
|
clear(): Promise<ClusteredRedisQueue>;
|
|
146
146
|
queueLength(): Promise<number>;
|
|
147
|
+
private verbose;
|
|
147
148
|
/**
|
|
148
149
|
* Batch imq action processing on all registered imqs at once
|
|
149
150
|
*
|
|
@@ -97,6 +97,7 @@ class ClusteredRedisQueue {
|
|
|
97
97
|
this.addServerWithQueueInitializing(server, false);
|
|
98
98
|
}
|
|
99
99
|
if ((_b = this.options.clusterManagers) === null || _b === void 0 ? void 0 : _b.length) {
|
|
100
|
+
this.verbose('Initializing cluster managers...');
|
|
100
101
|
for (const manager of this.options.clusterManagers) {
|
|
101
102
|
this.initializedClusters.push(manager.init({
|
|
102
103
|
add: this.addServer.bind(this),
|
|
@@ -215,6 +216,11 @@ class ClusteredRedisQueue {
|
|
|
215
216
|
const lengths = await Promise.all(promises);
|
|
216
217
|
return lengths.reduce((total, length) => total + length, 0);
|
|
217
218
|
}
|
|
219
|
+
verbose(message) {
|
|
220
|
+
if (this.options.verbose) {
|
|
221
|
+
this.logger.info(`[IMQ-CORE][ClusteredRedisQueue][${this.name}]: ${message}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
218
224
|
/**
|
|
219
225
|
* Batch imq action processing on all registered imqs at once
|
|
220
226
|
*
|
|
@@ -224,7 +230,7 @@ class ClusteredRedisQueue {
|
|
|
224
230
|
* @return {Promise<this>}
|
|
225
231
|
*/
|
|
226
232
|
async batch(action, message) {
|
|
227
|
-
this.logger.
|
|
233
|
+
this.logger.info(message);
|
|
228
234
|
const promises = [];
|
|
229
235
|
for (const imq of this.imqs) {
|
|
230
236
|
promises.push(imq[action]());
|
|
@@ -366,6 +372,7 @@ class ClusteredRedisQueue {
|
|
|
366
372
|
* @returns {void}
|
|
367
373
|
*/
|
|
368
374
|
addServer(server) {
|
|
375
|
+
this.verbose(`Adding new server: ${JSON.stringify(server)}`);
|
|
369
376
|
return this.addServerWithQueueInitializing(server, true);
|
|
370
377
|
}
|
|
371
378
|
/**
|
|
@@ -375,6 +382,7 @@ class ClusteredRedisQueue {
|
|
|
375
382
|
* @returns {void}
|
|
376
383
|
*/
|
|
377
384
|
removeServer(server) {
|
|
385
|
+
this.verbose(`Removing the server: ${JSON.stringify(server)}`);
|
|
378
386
|
const remove = this.findServer(server);
|
|
379
387
|
if (!remove) {
|
|
380
388
|
return;
|
|
@@ -423,6 +431,7 @@ class ClusteredRedisQueue {
|
|
|
423
431
|
}
|
|
424
432
|
async initializeQueue(imq) {
|
|
425
433
|
(0, _1.copyEventEmitter)(this.templateEmitter, imq);
|
|
434
|
+
this.verbose(`Initializing queue with state: ${JSON.stringify(this.state)}`);
|
|
426
435
|
if (this.state.started) {
|
|
427
436
|
await imq.start();
|
|
428
437
|
}
|
package/src/RedisQueue.d.ts
CHANGED
|
@@ -311,16 +311,6 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
311
311
|
* @private
|
|
312
312
|
*/
|
|
313
313
|
private scheduleReconnect;
|
|
314
|
-
/**
|
|
315
|
-
* Builds and returns connection ready state handler
|
|
316
|
-
*
|
|
317
|
-
* @access private
|
|
318
|
-
* @param {RedisQueue} context
|
|
319
|
-
* @param {RedisConnectionChannel} channel
|
|
320
|
-
* @param {(...args: any[]) => void} resolve
|
|
321
|
-
* @return {() => Promise<void>}
|
|
322
|
-
*/
|
|
323
|
-
private onReadyHandler;
|
|
324
314
|
/**
|
|
325
315
|
* Generates channel name
|
|
326
316
|
*
|
|
@@ -336,7 +326,6 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
336
326
|
* @access private
|
|
337
327
|
* @param {RedisQueue} context
|
|
338
328
|
* @param {RedisConnectionChannel} channel
|
|
339
|
-
* @param {(...args: any[]) => void} reject
|
|
340
329
|
* @return {(err: Error) => void}
|
|
341
330
|
*/
|
|
342
331
|
private onErrorHandler;
|
package/src/RedisQueue.js
CHANGED
|
@@ -586,42 +586,55 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
586
586
|
if (context[channel]) {
|
|
587
587
|
return context[channel];
|
|
588
588
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
context[channel] = redis;
|
|
608
|
-
context[channel].__imq = true;
|
|
609
|
-
for (const event of [
|
|
610
|
-
'wait',
|
|
611
|
-
'reconnecting',
|
|
612
|
-
'connecting',
|
|
613
|
-
'connect',
|
|
614
|
-
'close',
|
|
615
|
-
]) {
|
|
616
|
-
redis.on(event, () => {
|
|
617
|
-
context.verbose(`Redis Event fired: ${event}`);
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
redis.setMaxListeners(IMQ_REDIS_MAX_LISTENERS_LIMIT);
|
|
621
|
-
redis.on('ready', this.onReadyHandler(context, channel, resolve));
|
|
622
|
-
redis.on('error', this.onErrorHandler(context, channel, reject));
|
|
623
|
-
redis.on('end', this.onCloseHandler(context, channel));
|
|
589
|
+
const redis = new redis_1.default({
|
|
590
|
+
// istanbul ignore next
|
|
591
|
+
port: options.port || 6379,
|
|
592
|
+
// istanbul ignore next
|
|
593
|
+
host: options.host || 'localhost',
|
|
594
|
+
// istanbul ignore next
|
|
595
|
+
username: options.username,
|
|
596
|
+
// istanbul ignore next
|
|
597
|
+
password: options.password,
|
|
598
|
+
connectionName: this.getChannelName(context.name + '', options.prefix || '', channel),
|
|
599
|
+
retryStrategy: this.retryStrategy(),
|
|
600
|
+
autoResubscribe: true,
|
|
601
|
+
enableOfflineQueue: true,
|
|
602
|
+
autoResendUnfulfilledCommands: true,
|
|
603
|
+
offlineQueue: true,
|
|
604
|
+
maxRetriesPerRequest: null,
|
|
605
|
+
enableReadyCheck: channel !== 'subscription',
|
|
606
|
+
lazyConnect: true,
|
|
624
607
|
});
|
|
608
|
+
context[channel] = redis;
|
|
609
|
+
context[channel].__imq = true;
|
|
610
|
+
for (const event of [
|
|
611
|
+
'wait',
|
|
612
|
+
'reconnecting',
|
|
613
|
+
'connecting',
|
|
614
|
+
'connect',
|
|
615
|
+
'close',
|
|
616
|
+
]) {
|
|
617
|
+
redis.on(event, () => {
|
|
618
|
+
context.verbose(`Redis Event fired: ${event}`);
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
redis.setMaxListeners(IMQ_REDIS_MAX_LISTENERS_LIMIT);
|
|
622
|
+
redis.on('error', this.onErrorHandler(context, channel));
|
|
623
|
+
redis.on('end', this.onCloseHandler(context, channel));
|
|
624
|
+
await redis.connect();
|
|
625
|
+
this.logger.info('%s: %s channel connected, host %s, pid %s', context.name, channel, this.redisKey, process.pid);
|
|
626
|
+
switch (channel) {
|
|
627
|
+
case 'reader':
|
|
628
|
+
this.read();
|
|
629
|
+
break;
|
|
630
|
+
case 'writer':
|
|
631
|
+
await this.processDelayed(this.key);
|
|
632
|
+
break;
|
|
633
|
+
case 'watcher':
|
|
634
|
+
await this.initWatcher();
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
return context[channel];
|
|
625
638
|
}
|
|
626
639
|
// istanbul ignore next
|
|
627
640
|
/**
|
|
@@ -649,12 +662,10 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
649
662
|
if (this.reconnecting[channel]) {
|
|
650
663
|
return;
|
|
651
664
|
}
|
|
652
|
-
this.reconnecting[channel] = true;
|
|
653
665
|
const attempts = (this.reconnectAttempts[channel] || 0) + 1;
|
|
666
|
+
const delay = Math.min(30000, 1000 * Math.pow(2, attempts - 1));
|
|
667
|
+
this.reconnecting[channel] = true;
|
|
654
668
|
this.reconnectAttempts[channel] = attempts;
|
|
655
|
-
const base = Math.min(30000, 1000 * Math.pow(2, attempts - 1));
|
|
656
|
-
const jitter = Math.floor(base * 0.2 * Math.random());
|
|
657
|
-
const delay = base + jitter;
|
|
658
669
|
this.verbose(`Scheduling ${channel} reconnect in ${delay} ms (attempt ${attempts})`);
|
|
659
670
|
if (this.reconnectTimers[channel]) {
|
|
660
671
|
clearTimeout(this.reconnectTimers[channel]);
|
|
@@ -697,33 +708,6 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
697
708
|
}
|
|
698
709
|
}, delay);
|
|
699
710
|
}
|
|
700
|
-
/**
|
|
701
|
-
* Builds and returns connection ready state handler
|
|
702
|
-
*
|
|
703
|
-
* @access private
|
|
704
|
-
* @param {RedisQueue} context
|
|
705
|
-
* @param {RedisConnectionChannel} channel
|
|
706
|
-
* @param {(...args: any[]) => void} resolve
|
|
707
|
-
* @return {() => Promise<void>}
|
|
708
|
-
*/
|
|
709
|
-
onReadyHandler(context, channel, resolve) {
|
|
710
|
-
this.verbose(`Redis ${channel} channel ready!`);
|
|
711
|
-
return (async () => {
|
|
712
|
-
this.logger.info('%s: %s channel connected, host %s, pid %s', context.name, channel, this.redisKey, process.pid);
|
|
713
|
-
switch (channel) {
|
|
714
|
-
case 'reader':
|
|
715
|
-
this.read();
|
|
716
|
-
break;
|
|
717
|
-
case 'writer':
|
|
718
|
-
await this.processDelayed(this.key);
|
|
719
|
-
break;
|
|
720
|
-
case 'watcher':
|
|
721
|
-
await this.initWatcher();
|
|
722
|
-
break;
|
|
723
|
-
}
|
|
724
|
-
resolve(context[channel]);
|
|
725
|
-
});
|
|
726
|
-
}
|
|
727
711
|
// noinspection JSMethodCanBeStatic
|
|
728
712
|
/**
|
|
729
713
|
* Generates channel name
|
|
@@ -743,22 +727,20 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
743
727
|
* @access private
|
|
744
728
|
* @param {RedisQueue} context
|
|
745
729
|
* @param {RedisConnectionChannel} channel
|
|
746
|
-
* @param {(...args: any[]) => void} reject
|
|
747
730
|
* @return {(err: Error) => void}
|
|
748
731
|
*/
|
|
749
|
-
onErrorHandler(context, channel
|
|
732
|
+
onErrorHandler(context, channel) {
|
|
750
733
|
// istanbul ignore next
|
|
751
|
-
return ((
|
|
752
|
-
|
|
734
|
+
return ((error) => {
|
|
735
|
+
var _a;
|
|
736
|
+
this.verbose(`Redis Error: ${error}`);
|
|
753
737
|
if (this.destroyed) {
|
|
754
738
|
return;
|
|
755
739
|
}
|
|
756
|
-
this.logger.error(`${context.name}: error connecting redis host ${this.redisKey} on ${channel}, pid ${process.pid}:`,
|
|
757
|
-
if (
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
else {
|
|
761
|
-
// Try to recover the channel using our reconnection routine
|
|
740
|
+
this.logger.error(`${context.name}: error connecting redis host ${this.redisKey} on ${channel}, pid ${process.pid}:`, error);
|
|
741
|
+
if (error.code === 'ECONNREFUSED' ||
|
|
742
|
+
error.code === 'ETIMEDOUT' ||
|
|
743
|
+
((_a = context[channel]) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') {
|
|
762
744
|
this.scheduleReconnect(channel);
|
|
763
745
|
}
|
|
764
746
|
});
|
|
@@ -51,6 +51,16 @@ export interface UDPClusterManagerOptions {
|
|
|
51
51
|
* @type {number}
|
|
52
52
|
*/
|
|
53
53
|
aliveTimeoutCorrection: number;
|
|
54
|
+
/**
|
|
55
|
+
* Message queue alive-server check flag. If set to false, the server will
|
|
56
|
+
* not be checked for liveness on each broadcast message with a timeout.
|
|
57
|
+
* Can be specified by the environment variable if the given option is not
|
|
58
|
+
* bypassed: IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK
|
|
59
|
+
*
|
|
60
|
+
* @default true
|
|
61
|
+
* @type {boolean}
|
|
62
|
+
*/
|
|
63
|
+
useAliveCheck: boolean;
|
|
54
64
|
}
|
|
55
65
|
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: UDPClusterManagerOptions;
|
|
56
66
|
export declare class UDPClusterManager extends ClusterManager {
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -28,10 +28,12 @@ const ClusterManager_1 = require("./ClusterManager");
|
|
|
28
28
|
const worker_threads_1 = require("worker_threads");
|
|
29
29
|
const path = require("path");
|
|
30
30
|
process.setMaxListeners(10000);
|
|
31
|
+
const IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK = !!+(process.env.IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK || 1);
|
|
31
32
|
exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
32
33
|
port: 63000,
|
|
33
34
|
address: '255.255.255.255',
|
|
34
35
|
aliveTimeoutCorrection: 5000,
|
|
36
|
+
useAliveCheck: IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK,
|
|
35
37
|
};
|
|
36
38
|
class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
37
39
|
constructor(options) {
|
package/src/UDPWorker.js
CHANGED
|
@@ -34,7 +34,7 @@ var MessageType;
|
|
|
34
34
|
MessageType["Up"] = "up";
|
|
35
35
|
MessageType["Down"] = "down";
|
|
36
36
|
})(MessageType || (MessageType = {}));
|
|
37
|
-
class
|
|
37
|
+
class UDPWorker {
|
|
38
38
|
constructor(options, messagePort) {
|
|
39
39
|
this.options = options;
|
|
40
40
|
this.messagePort = messagePort;
|
|
@@ -66,15 +66,17 @@ class UDPClusterWorker {
|
|
|
66
66
|
addServer(message) {
|
|
67
67
|
this.messagePort.postMessage({
|
|
68
68
|
type: 'cluster:add',
|
|
69
|
-
server:
|
|
69
|
+
server: UDPWorker.mapMessage(message),
|
|
70
70
|
});
|
|
71
|
-
this.
|
|
71
|
+
if (this.options.useAliveCheck) {
|
|
72
|
+
this.serverAliveWait(message);
|
|
73
|
+
}
|
|
72
74
|
}
|
|
73
75
|
removeServer(message) {
|
|
74
|
-
this.servers.delete(
|
|
76
|
+
this.servers.delete(UDPWorker.getServerKey(message));
|
|
75
77
|
this.messagePort.postMessage({
|
|
76
78
|
type: 'cluster:remove',
|
|
77
|
-
server:
|
|
79
|
+
server: UDPWorker.mapMessage(message),
|
|
78
80
|
});
|
|
79
81
|
}
|
|
80
82
|
static mapMessage(message) {
|
|
@@ -92,7 +94,7 @@ class UDPClusterWorker {
|
|
|
92
94
|
const stamp = (0, uuid_1.uuid)();
|
|
93
95
|
const correction = (_a = this.options.aliveTimeoutCorrection) !== null && _a !== void 0 ? _a : 0;
|
|
94
96
|
const effectiveTimeout = message.timeout + correction + 1;
|
|
95
|
-
const key =
|
|
97
|
+
const key = UDPWorker.getServerKey(message);
|
|
96
98
|
this.servers.set(key, stamp);
|
|
97
99
|
const t = setTimeout(() => setImmediate(() => {
|
|
98
100
|
if (this.servers.get(key) === stamp) {
|
|
@@ -168,6 +170,6 @@ class UDPClusterWorker {
|
|
|
168
170
|
}
|
|
169
171
|
}
|
|
170
172
|
if (!worker_threads_1.isMainThread && worker_threads_1.parentPort) {
|
|
171
|
-
new
|
|
173
|
+
new UDPWorker(worker_threads_1.workerData, worker_threads_1.parentPort);
|
|
172
174
|
}
|
|
173
175
|
//# sourceMappingURL=UDPWorker.js.map
|