@imqueue/core 1.13.2 → 2.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/LICENSE +585 -13
- package/README.md +2 -1
- package/eslint.config.mjs +98 -0
- package/index.d.ts +17 -11
- package/index.js +18 -12
- package/package.json +26 -21
- package/src/ClusterManager.d.ts +34 -0
- package/src/ClusterManager.js +13 -0
- package/src/ClusteredRedisQueue.d.ts +61 -22
- package/src/ClusteredRedisQueue.js +157 -31
- package/src/IMQMode.d.ts +17 -11
- package/src/IMQMode.js +17 -11
- package/src/IMessageQueue.d.ts +86 -30
- package/src/RedisQueue.d.ts +44 -41
- package/src/RedisQueue.js +160 -115
- package/src/UDPClusterManager.d.ts +82 -0
- package/src/UDPClusterManager.js +154 -0
- package/src/copyEventEmitter.d.ts +28 -0
- package/src/copyEventEmitter.js +23 -0
- package/src/index.d.ts +20 -11
- package/src/index.js +21 -13
- package/src/profile.d.ts +17 -11
- package/src/profile.js +21 -15
- package/src/promisify.d.ts +18 -11
- package/src/promisify.js +21 -15
- package/src/redis.d.ts +25 -24
- package/src/redis.js +44 -22
- package/src/uuid.js +18 -13
- package/debug.d.ts +0 -0
- package/tslint.json +0 -77
package/src/RedisQueue.js
CHANGED
|
@@ -9,38 +9,49 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.RedisQueue = exports.
|
|
12
|
+
exports.RedisQueue = exports.IMQ_SHUTDOWN_TIMEOUT = exports.DEFAULT_IMQ_OPTIONS = void 0;
|
|
13
|
+
exports.sha1 = sha1;
|
|
14
|
+
exports.intrand = intrand;
|
|
15
|
+
exports.pack = pack;
|
|
16
|
+
exports.unpack = unpack;
|
|
13
17
|
/*!
|
|
14
18
|
* Fast messaging queue over Redis
|
|
15
19
|
*
|
|
16
|
-
*
|
|
20
|
+
* I'm Queue Software Project
|
|
21
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
22
|
+
*
|
|
23
|
+
* This program is free software: you can redistribute it and/or modify
|
|
24
|
+
* it under the terms of the GNU General Public License as published by
|
|
25
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
26
|
+
* (at your option) any later version.
|
|
17
27
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
28
|
+
* This program is distributed in the hope that it will be useful,
|
|
29
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
30
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
31
|
+
* GNU General Public License for more details.
|
|
21
32
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* PERFORMANCE OF THIS SOFTWARE.
|
|
33
|
+
* You should have received a copy of the GNU General Public License
|
|
34
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
35
|
+
*
|
|
36
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
37
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
38
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
29
39
|
*/
|
|
30
40
|
const crypto = require("crypto");
|
|
31
41
|
const events_1 = require("events");
|
|
32
42
|
const os = require("os");
|
|
33
43
|
const zlib_1 = require("zlib");
|
|
34
44
|
const _1 = require(".");
|
|
45
|
+
const redis_1 = require("./redis");
|
|
35
46
|
const RX_CLIENT_NAME = /name=(\S+)/g;
|
|
36
47
|
const RX_CLIENT_TEST = /:(reader|writer|watcher)/;
|
|
37
48
|
const RX_CLIENT_CLEAN = /:(reader|writer|watcher).*$/;
|
|
38
49
|
exports.DEFAULT_IMQ_OPTIONS = {
|
|
50
|
+
host: 'localhost',
|
|
51
|
+
port: 6379,
|
|
39
52
|
cleanup: false,
|
|
40
53
|
cleanupFilter: '*',
|
|
41
|
-
host: 'localhost',
|
|
42
54
|
logger: console,
|
|
43
|
-
port: 6379,
|
|
44
55
|
prefix: 'imq',
|
|
45
56
|
safeDelivery: false,
|
|
46
57
|
safeDeliveryTtl: 5000,
|
|
@@ -59,7 +70,6 @@ function sha1(str) {
|
|
|
59
70
|
sha.update(str);
|
|
60
71
|
return sha.digest('hex');
|
|
61
72
|
}
|
|
62
|
-
exports.sha1 = sha1;
|
|
63
73
|
/**
|
|
64
74
|
* Returns random integer between given min and max
|
|
65
75
|
*
|
|
@@ -70,7 +80,6 @@ exports.sha1 = sha1;
|
|
|
70
80
|
function intrand(min, max) {
|
|
71
81
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
72
82
|
}
|
|
73
|
-
exports.intrand = intrand;
|
|
74
83
|
/**
|
|
75
84
|
* Compress given data and returns binary string
|
|
76
85
|
*
|
|
@@ -81,7 +90,6 @@ exports.intrand = intrand;
|
|
|
81
90
|
function pack(data) {
|
|
82
91
|
return (0, zlib_1.gzipSync)(JSON.stringify(data)).toString('binary');
|
|
83
92
|
}
|
|
84
|
-
exports.pack = pack;
|
|
85
93
|
/**
|
|
86
94
|
* Decompress binary string and returns plain data
|
|
87
95
|
*
|
|
@@ -92,7 +100,6 @@ exports.pack = pack;
|
|
|
92
100
|
function unpack(data) {
|
|
93
101
|
return JSON.parse((0, zlib_1.gunzipSync)(Buffer.from(data, 'binary')).toString());
|
|
94
102
|
}
|
|
95
|
-
exports.unpack = unpack;
|
|
96
103
|
/**
|
|
97
104
|
* Class RedisQueue
|
|
98
105
|
* Implements simple messaging queue over redis.
|
|
@@ -115,7 +122,13 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
115
122
|
*/
|
|
116
123
|
this.initialized = false;
|
|
117
124
|
/**
|
|
118
|
-
*
|
|
125
|
+
* Signals if the queue was destroyed
|
|
126
|
+
*
|
|
127
|
+
* @type {boolean}
|
|
128
|
+
*/
|
|
129
|
+
this.destroyed = false;
|
|
130
|
+
/**
|
|
131
|
+
* True if the current instance owns watcher connection, false otherwise
|
|
119
132
|
*
|
|
120
133
|
* @type {boolean}
|
|
121
134
|
*/
|
|
@@ -158,7 +171,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
158
171
|
this.redisKey = `${this.options.host}:${this.options.port}`;
|
|
159
172
|
}
|
|
160
173
|
/**
|
|
161
|
-
* Creates subscription channel over redis and sets up channel
|
|
174
|
+
* Creates a subscription channel over redis and sets up channel
|
|
162
175
|
* data read handler
|
|
163
176
|
*
|
|
164
177
|
* @param {string} channel
|
|
@@ -166,9 +179,11 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
166
179
|
* @return {Promise<void>}
|
|
167
180
|
*/
|
|
168
181
|
async subscribe(channel, handler) {
|
|
182
|
+
// istanbul ignore next
|
|
169
183
|
if (!channel) {
|
|
170
184
|
throw new TypeError(`${channel}: No subscription channel name provided!`);
|
|
171
185
|
}
|
|
186
|
+
// istanbul ignore next
|
|
172
187
|
if (this.subscriptionName && this.subscriptionName !== channel) {
|
|
173
188
|
throw new TypeError(`Invalid channel name provided: expected "${this.subscriptionName}", but "${channel}" given instead!`);
|
|
174
189
|
}
|
|
@@ -178,6 +193,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
178
193
|
const fcn = `${this.options.prefix}:${this.subscriptionName}`;
|
|
179
194
|
const chan = await this.connect('subscription', this.options);
|
|
180
195
|
await chan.subscribe(fcn);
|
|
196
|
+
// istanbul ignore next
|
|
181
197
|
chan.on('message', (ch, message) => {
|
|
182
198
|
if (ch === fcn && typeof handler === 'function') {
|
|
183
199
|
handler(JSON.parse(message));
|
|
@@ -195,8 +211,8 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
195
211
|
this.subscription.unsubscribe(`${this.options.prefix}:${this.subscriptionName}`);
|
|
196
212
|
}
|
|
197
213
|
this.subscription.removeAllListeners();
|
|
198
|
-
this.subscription.
|
|
199
|
-
this.subscription.
|
|
214
|
+
this.subscription.disconnect(false);
|
|
215
|
+
this.subscription.quit();
|
|
200
216
|
}
|
|
201
217
|
this.subscriptionName = undefined;
|
|
202
218
|
this.subscription = undefined;
|
|
@@ -205,9 +221,9 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
205
221
|
* Publishes a message to this queue subscription channel for currently
|
|
206
222
|
* subscribed clients.
|
|
207
223
|
*
|
|
208
|
-
* If toName specified will publish to
|
|
224
|
+
* If toName specified will publish to PubSub with a different name. This
|
|
209
225
|
* can be used to implement broadcasting some messages to other subscribers
|
|
210
|
-
* on other
|
|
226
|
+
* on other PubSub channels.
|
|
211
227
|
*
|
|
212
228
|
* @param {string} [toName]
|
|
213
229
|
* @param {JsonObject} data
|
|
@@ -261,10 +277,11 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
261
277
|
}
|
|
262
278
|
await this.initWatcher();
|
|
263
279
|
this.initialized = true;
|
|
280
|
+
this.destroyed = false;
|
|
264
281
|
return this;
|
|
265
282
|
}
|
|
266
283
|
/**
|
|
267
|
-
* Sends given message to a given queue (by name)
|
|
284
|
+
* Sends a given message to a given queue (by name)
|
|
268
285
|
*
|
|
269
286
|
* @param {string} toQueue
|
|
270
287
|
* @param {JsonObject} message
|
|
@@ -313,8 +330,8 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
313
330
|
async stop() {
|
|
314
331
|
if (this.reader) {
|
|
315
332
|
this.reader.removeAllListeners();
|
|
316
|
-
this.reader.
|
|
317
|
-
this.reader.
|
|
333
|
+
this.reader.quit();
|
|
334
|
+
this.reader.disconnect(false);
|
|
318
335
|
delete this.reader;
|
|
319
336
|
}
|
|
320
337
|
this.initialized = false;
|
|
@@ -326,6 +343,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
326
343
|
* @returns {Promise<void>}
|
|
327
344
|
*/
|
|
328
345
|
async destroy() {
|
|
346
|
+
this.destroyed = true;
|
|
329
347
|
this.removeAllListeners();
|
|
330
348
|
this.cleanSafeCheckInterval();
|
|
331
349
|
this.destroyWatcher();
|
|
@@ -342,10 +360,18 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
342
360
|
if (!this.writer) {
|
|
343
361
|
return this;
|
|
344
362
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
363
|
+
try {
|
|
364
|
+
await Promise.all([
|
|
365
|
+
this.writer.del(this.key),
|
|
366
|
+
this.writer.del(`${this.key}:delayed`),
|
|
367
|
+
]);
|
|
368
|
+
}
|
|
369
|
+
catch (err) {
|
|
370
|
+
// istanbul ignore next
|
|
371
|
+
if (this.initialized) {
|
|
372
|
+
this.logger.error(`${context.name}: error clearing the redis queue host ${this.redisKey} on writer, pid ${process.pid}:`, err);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
349
375
|
return this;
|
|
350
376
|
}
|
|
351
377
|
/**
|
|
@@ -403,7 +429,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
403
429
|
RedisQueue.watchers[this.redisKey] = conn;
|
|
404
430
|
}
|
|
405
431
|
/**
|
|
406
|
-
* Logger instance associated with current queue instance
|
|
432
|
+
* Logger instance associated with the current queue instance
|
|
407
433
|
* @type {ILogger}
|
|
408
434
|
*/
|
|
409
435
|
get logger() {
|
|
@@ -436,8 +462,8 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
436
462
|
destroyWatcher() {
|
|
437
463
|
if (this.watcher) {
|
|
438
464
|
this.watcher.removeAllListeners();
|
|
439
|
-
this.watcher.
|
|
440
|
-
this.watcher.
|
|
465
|
+
this.watcher.quit();
|
|
466
|
+
this.watcher.disconnect(false);
|
|
441
467
|
delete RedisQueue.watchers[this.redisKey];
|
|
442
468
|
}
|
|
443
469
|
}
|
|
@@ -449,16 +475,16 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
449
475
|
destroyWriter() {
|
|
450
476
|
if (this.writer) {
|
|
451
477
|
this.writer.removeAllListeners();
|
|
452
|
-
this.writer.
|
|
453
|
-
this.writer.
|
|
478
|
+
this.writer.quit();
|
|
479
|
+
this.writer.disconnect(false);
|
|
454
480
|
delete RedisQueue.writers[this.redisKey];
|
|
455
481
|
}
|
|
456
482
|
}
|
|
457
483
|
/**
|
|
458
|
-
* Establishes given connection channel by its
|
|
484
|
+
* Establishes a given connection channel by its name
|
|
459
485
|
*
|
|
460
486
|
* @access private
|
|
461
|
-
* @param {
|
|
487
|
+
* @param {RedisConnectionChannel} channel
|
|
462
488
|
* @param {IMQOptions} options
|
|
463
489
|
* @param {any} context
|
|
464
490
|
* @returns {Promise<IRedisClient>}
|
|
@@ -469,37 +495,55 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
469
495
|
return context[channel];
|
|
470
496
|
}
|
|
471
497
|
return new Promise((resolve, reject) => {
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
498
|
+
const redis = new redis_1.default({
|
|
499
|
+
// istanbul ignore next
|
|
500
|
+
port: options.port || 6379,
|
|
501
|
+
// istanbul ignore next
|
|
502
|
+
host: options.host || 'localhost',
|
|
503
|
+
// istanbul ignore next
|
|
504
|
+
username: options.username,
|
|
505
|
+
// istanbul ignore next
|
|
506
|
+
password: options.password,
|
|
507
|
+
connectionName: this.getChannelName(context.name, options.prefix || '', channel),
|
|
508
|
+
retryStrategy: this.retryStrategy(context),
|
|
509
|
+
autoResubscribe: true,
|
|
510
|
+
});
|
|
511
|
+
context[channel] = (0, _1.makeRedisSafe)(redis);
|
|
477
512
|
context[channel].__imq = true;
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
513
|
+
redis.setMaxListeners(10000);
|
|
514
|
+
redis.on('ready', this.onReadyHandler(context, channel, resolve));
|
|
515
|
+
redis.on('error', this.onErrorHandler(context, channel, reject));
|
|
516
|
+
redis.on('end', this.onCloseHandler(context, channel));
|
|
482
517
|
});
|
|
483
518
|
}
|
|
519
|
+
// istanbul ignore next
|
|
520
|
+
/**
|
|
521
|
+
* Builds and returns redis reconnection strategy
|
|
522
|
+
*
|
|
523
|
+
* @param {RedisQueue} context
|
|
524
|
+
* @returns {() => (number | void | null)}
|
|
525
|
+
* @private
|
|
526
|
+
*/
|
|
527
|
+
retryStrategy(context) {
|
|
528
|
+
return () => {
|
|
529
|
+
if (context.destroyed) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
return 200;
|
|
533
|
+
};
|
|
534
|
+
}
|
|
484
535
|
/**
|
|
485
536
|
* Builds and returns connection ready state handler
|
|
486
537
|
*
|
|
487
538
|
* @access private
|
|
488
|
-
* @param {
|
|
489
|
-
* @param {
|
|
490
|
-
* @param {string} channel
|
|
539
|
+
* @param {RedisQueue} context
|
|
540
|
+
* @param {RedisConnectionChannel} channel
|
|
491
541
|
* @param {(...args: any[]) => void} resolve
|
|
492
542
|
* @return {() => Promise<void>}
|
|
493
543
|
*/
|
|
494
|
-
onReadyHandler(
|
|
544
|
+
onReadyHandler(context, channel, resolve) {
|
|
495
545
|
return (async () => {
|
|
496
546
|
this.logger.info('%s: %s channel connected, host %s, pid %s', context.name, channel, this.redisKey, process.pid);
|
|
497
|
-
try {
|
|
498
|
-
await this.setChannelName(context[channel], context.name, options.prefix || '', channel);
|
|
499
|
-
}
|
|
500
|
-
catch (err) {
|
|
501
|
-
this.logger.warn('Error setting channel name:', err);
|
|
502
|
-
}
|
|
503
547
|
switch (channel) {
|
|
504
548
|
case 'reader':
|
|
505
549
|
this.read();
|
|
@@ -516,60 +560,45 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
516
560
|
}
|
|
517
561
|
// noinspection JSMethodCanBeStatic
|
|
518
562
|
/**
|
|
519
|
-
*
|
|
563
|
+
* Generates channel name
|
|
520
564
|
*
|
|
521
|
-
* @param {IRedisClient} channel
|
|
522
565
|
* @param {string} contextName
|
|
523
566
|
* @param {string} prefix
|
|
524
|
-
* @param {
|
|
567
|
+
* @param {RedisConnectionChannel} name
|
|
568
|
+
* @return {string}
|
|
525
569
|
*/
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
/**
|
|
530
|
-
* Builds and returns redis connection reconnect handler
|
|
531
|
-
*
|
|
532
|
-
* @access private
|
|
533
|
-
* @param {any} context
|
|
534
|
-
* @param {string} channel
|
|
535
|
-
* @return {() => void}
|
|
536
|
-
*/
|
|
537
|
-
onReconnectHandler(context, channel) {
|
|
538
|
-
// istanbul ignore next
|
|
539
|
-
return (async () => {
|
|
540
|
-
if (channel === 'watcher') {
|
|
541
|
-
await context[channel].punsubscribe();
|
|
542
|
-
this.watcher.__ready__ = false;
|
|
543
|
-
this.cleanSafeCheckInterval();
|
|
544
|
-
}
|
|
545
|
-
this.initialized = false;
|
|
546
|
-
this.logger.warn('%s: redis connection %s is reconnecting on host %s, ' +
|
|
547
|
-
'pid %s...', context.name, channel, this.redisKey, process.pid);
|
|
548
|
-
});
|
|
570
|
+
getChannelName(contextName, prefix, name) {
|
|
571
|
+
const uniqueSuffix = `pid:${process.pid}:host:${os.hostname()}`;
|
|
572
|
+
return `${prefix}:${contextName}:${name}:${uniqueSuffix}`;
|
|
549
573
|
}
|
|
550
574
|
/**
|
|
551
575
|
* Builds and returns connection error handler
|
|
552
576
|
*
|
|
553
577
|
* @access private
|
|
554
|
-
* @param context
|
|
555
|
-
* @param {
|
|
578
|
+
* @param {RedisQueue} context
|
|
579
|
+
* @param {RedisConnectionChannel} channel
|
|
556
580
|
* @param {(...args: any[]) => void} reject
|
|
557
581
|
* @return {(err: Error) => void}
|
|
558
582
|
*/
|
|
559
583
|
onErrorHandler(context, channel, reject) {
|
|
560
584
|
// istanbul ignore next
|
|
561
585
|
return ((err) => {
|
|
562
|
-
this.
|
|
586
|
+
if (this.destroyed) {
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
563
589
|
this.logger.error(`${context.name}: error connecting redis host ${this.redisKey} on ${channel}, pid ${process.pid}:`, err);
|
|
564
|
-
|
|
590
|
+
if (!this.initialized) {
|
|
591
|
+
this.initialized = false;
|
|
592
|
+
reject(err);
|
|
593
|
+
}
|
|
565
594
|
});
|
|
566
595
|
}
|
|
567
596
|
/**
|
|
568
597
|
* Builds and returns redis connection close handler
|
|
569
598
|
*
|
|
570
599
|
* @access private
|
|
571
|
-
* @param {
|
|
572
|
-
* @param {
|
|
600
|
+
* @param {RedisQueue} context
|
|
601
|
+
* @param {RedisConnectionChannel} channel
|
|
573
602
|
* @return {(...args: any[]) => any}
|
|
574
603
|
*/
|
|
575
604
|
onCloseHandler(context, channel) {
|
|
@@ -583,7 +612,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
583
612
|
* Processes given redis-queue message
|
|
584
613
|
*
|
|
585
614
|
* @access private
|
|
586
|
-
* @param {[any
|
|
615
|
+
* @param {[any, any]} msg
|
|
587
616
|
* @returns {RedisQueue}
|
|
588
617
|
*/
|
|
589
618
|
process(msg) {
|
|
@@ -611,9 +640,10 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
611
640
|
// istanbul ignore next
|
|
612
641
|
async watcherCount() {
|
|
613
642
|
const rx = new RegExp(`\\bname=${this.options.prefix}:[\\S]+?:watcher:`);
|
|
614
|
-
|
|
643
|
+
const list = await this.writer.client('LIST');
|
|
644
|
+
return (list || '')
|
|
615
645
|
.split(/\r?\n/)
|
|
616
|
-
.filter(
|
|
646
|
+
.filter(client => rx.test(client))
|
|
617
647
|
.length;
|
|
618
648
|
}
|
|
619
649
|
/**
|
|
@@ -638,16 +668,17 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
638
668
|
* Watch routine
|
|
639
669
|
*
|
|
640
670
|
* @access private
|
|
641
|
-
* @return {Promise<
|
|
671
|
+
* @return {Promise<void>}
|
|
642
672
|
*/
|
|
643
673
|
async processWatch() {
|
|
644
674
|
const now = Date.now();
|
|
645
675
|
let cursor = '0';
|
|
646
676
|
while (true) {
|
|
647
677
|
try {
|
|
648
|
-
const data = await this.writer.scan(cursor, '
|
|
678
|
+
const data = await this.writer.scan(cursor, 'MATCH', `${this.options.prefix}:*:worker:*`, 'COUNT', '1000');
|
|
649
679
|
cursor = data.shift();
|
|
650
|
-
|
|
680
|
+
const keys = data.shift() || [];
|
|
681
|
+
await this.processKeys(keys, now);
|
|
651
682
|
if (cursor === '0') {
|
|
652
683
|
return;
|
|
653
684
|
}
|
|
@@ -714,7 +745,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
714
745
|
}
|
|
715
746
|
}
|
|
716
747
|
/**
|
|
717
|
-
* Setups watch process on delayed messages
|
|
748
|
+
* Setups watch a process on delayed messages
|
|
718
749
|
*
|
|
719
750
|
* @access private
|
|
720
751
|
* @returns {RedisQueue}
|
|
@@ -725,7 +756,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
725
756
|
return this;
|
|
726
757
|
}
|
|
727
758
|
try {
|
|
728
|
-
this.writer.config('
|
|
759
|
+
this.writer.config('SET', 'notify-keyspace-events', 'Ex');
|
|
729
760
|
}
|
|
730
761
|
catch (err) {
|
|
731
762
|
this.emitError('OnConfig', 'events config error', err);
|
|
@@ -755,7 +786,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
755
786
|
* Cleans up orphaned keys from redis
|
|
756
787
|
*
|
|
757
788
|
* @access private
|
|
758
|
-
* @returns {RedisQueue}
|
|
789
|
+
* @returns {Promise<RedisQueue | undefined>}
|
|
759
790
|
*/
|
|
760
791
|
async processCleanup() {
|
|
761
792
|
try {
|
|
@@ -764,7 +795,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
764
795
|
}
|
|
765
796
|
const filter = new RegExp(this.options.prefix + ':' +
|
|
766
797
|
(this.options.cleanupFilter || '*').replace(/\*/g, '.*'), 'i');
|
|
767
|
-
const clients = await this.writer.client('
|
|
798
|
+
const clients = await this.writer.client('LIST');
|
|
768
799
|
const connectedKeys = (clients.match(RX_CLIENT_NAME) || [])
|
|
769
800
|
.filter((name) => RX_CLIENT_TEST.test(name) && filter.test(name))
|
|
770
801
|
.map((name) => name
|
|
@@ -774,10 +805,11 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
774
805
|
const keysToRemove = [];
|
|
775
806
|
let cursor = '0';
|
|
776
807
|
while (true) {
|
|
777
|
-
const data = await this.writer.scan(cursor, '
|
|
808
|
+
const data = await this.writer.scan(cursor, 'MATCH', `${this.options.prefix}:${this.options.cleanupFilter || '*'}`, 'COUNT', '1000');
|
|
778
809
|
cursor = data.shift();
|
|
779
|
-
|
|
780
|
-
|
|
810
|
+
const keys = data.shift() || [];
|
|
811
|
+
keysToRemove.push(...keys.filter(key => key !== this.lockKey &&
|
|
812
|
+
connectedKeys.every(connectedKey => key.indexOf(connectedKey) === -1)));
|
|
781
813
|
if (cursor === '0') {
|
|
782
814
|
break;
|
|
783
815
|
}
|
|
@@ -804,7 +836,9 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
804
836
|
}
|
|
805
837
|
try {
|
|
806
838
|
const msg = await this.reader.brpop(key, 0);
|
|
807
|
-
|
|
839
|
+
if (msg) {
|
|
840
|
+
this.process(msg);
|
|
841
|
+
}
|
|
808
842
|
}
|
|
809
843
|
catch (err) {
|
|
810
844
|
// istanbul ignore next
|
|
@@ -859,7 +893,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
859
893
|
}
|
|
860
894
|
}
|
|
861
895
|
/**
|
|
862
|
-
* Initializes read process on redis message queue
|
|
896
|
+
* Initializes a read process on the redis message queue
|
|
863
897
|
*
|
|
864
898
|
* @returns {RedisQueue}
|
|
865
899
|
*/
|
|
@@ -876,13 +910,16 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
876
910
|
return this;
|
|
877
911
|
}
|
|
878
912
|
/**
|
|
879
|
-
* Checks if watcher connection is locked
|
|
913
|
+
* Checks if the watcher connection is locked
|
|
880
914
|
*
|
|
881
915
|
* @access private
|
|
882
916
|
* @returns {Promise<boolean>}
|
|
883
917
|
*/
|
|
884
918
|
async isLocked() {
|
|
885
|
-
|
|
919
|
+
if (this.writer) {
|
|
920
|
+
return Boolean(Number(await this.writer.exists(this.lockKey)));
|
|
921
|
+
}
|
|
922
|
+
return false;
|
|
886
923
|
}
|
|
887
924
|
/**
|
|
888
925
|
* Locks watcher connection
|
|
@@ -891,7 +928,10 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
891
928
|
* @returns {Promise<boolean>}
|
|
892
929
|
*/
|
|
893
930
|
async lock() {
|
|
894
|
-
|
|
931
|
+
if (this.writer) {
|
|
932
|
+
return Boolean(Number(await this.writer.setnx(this.lockKey, '')));
|
|
933
|
+
}
|
|
934
|
+
return false;
|
|
895
935
|
}
|
|
896
936
|
/**
|
|
897
937
|
* Unlocks watcher connection
|
|
@@ -900,7 +940,10 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
900
940
|
* @returns {Promise<boolean>}
|
|
901
941
|
*/
|
|
902
942
|
async unlock() {
|
|
903
|
-
|
|
943
|
+
if (this.writer) {
|
|
944
|
+
return Boolean(Number(await this.writer.del(this.lockKey)));
|
|
945
|
+
}
|
|
946
|
+
return false;
|
|
904
947
|
}
|
|
905
948
|
// istanbul ignore next
|
|
906
949
|
/**
|
|
@@ -916,7 +959,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
916
959
|
this.logger.error(`${this.name}: ${message}, pid ${process.pid} on redis host ${this.redisKey}:`, err);
|
|
917
960
|
}
|
|
918
961
|
/**
|
|
919
|
-
* Acquires owner for watcher connection to this instance of the queue
|
|
962
|
+
* Acquires an owner for watcher connection to this instance of the queue
|
|
920
963
|
*
|
|
921
964
|
* @returns {Promise<void>}
|
|
922
965
|
*/
|
|
@@ -926,10 +969,12 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
926
969
|
if (owned) {
|
|
927
970
|
for (const script of Object.keys(this.scripts)) {
|
|
928
971
|
try {
|
|
929
|
-
const checksum =
|
|
930
|
-
|
|
972
|
+
const checksum = sha1(this.scripts[script].code);
|
|
973
|
+
this.scripts[script].checksum = checksum;
|
|
974
|
+
const scriptExists = await this.writer.script('EXISTS', checksum);
|
|
975
|
+
const loaded = (scriptExists || []).shift();
|
|
931
976
|
if (!loaded) {
|
|
932
|
-
await this.writer.script('
|
|
977
|
+
await this.writer.script('LOAD', this.scripts[script].code);
|
|
933
978
|
}
|
|
934
979
|
}
|
|
935
980
|
catch (err) {
|
|
@@ -943,7 +988,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
943
988
|
}
|
|
944
989
|
// istanbul ignore next
|
|
945
990
|
/**
|
|
946
|
-
*
|
|
991
|
+
* This method returns watcher lock resolver function
|
|
947
992
|
*
|
|
948
993
|
* @access private
|
|
949
994
|
* @param {(...args: any[]) => void} resolve
|
|
@@ -966,7 +1011,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
966
1011
|
});
|
|
967
1012
|
}
|
|
968
1013
|
/**
|
|
969
|
-
* Initializes single watcher connection across all queues with the same
|
|
1014
|
+
* Initializes a single watcher connection across all queues with the same
|
|
970
1015
|
* prefix.
|
|
971
1016
|
*
|
|
972
1017
|
* @returns {Promise<void>}
|
|
@@ -981,7 +1026,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
981
1026
|
resolve();
|
|
982
1027
|
}
|
|
983
1028
|
else {
|
|
984
|
-
// check for possible
|
|
1029
|
+
// check for possible deadlock to resolve
|
|
985
1030
|
setTimeout(this.watchLockResolver(resolve, reject), intrand(1, 50));
|
|
986
1031
|
}
|
|
987
1032
|
}
|