@imqueue/core 2.0.15 → 2.0.17
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/debug.d.ts +1 -0
- package/debug_getter.d.ts +9 -0
- package/debug_listener.d.ts +1 -0
- package/package.json +2 -2
- package/src/IMessageQueue.d.ts +16 -0
- package/src/RedisQueue.d.ts +2 -1
- package/src/RedisQueue.js +122 -30
package/debug.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.17",
|
|
4
4
|
"description": "Simple JSON-based messaging queue for inter service communication",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"message-queue",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
|
|
39
39
|
"license": "GPL-3.0-only",
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"ioredis": "^5.
|
|
41
|
+
"ioredis": "^5.8.2"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@eslint/js": "^9.33.0",
|
package/src/IMessageQueue.d.ts
CHANGED
|
@@ -243,6 +243,22 @@ export interface IMQOptions extends Partial<IMessageQueueConnection> {
|
|
|
243
243
|
* @type {ClusterManager[]}
|
|
244
244
|
*/
|
|
245
245
|
clusterManagers?: ClusterManager[];
|
|
246
|
+
/**
|
|
247
|
+
* Enables/disables verbose logging
|
|
248
|
+
*
|
|
249
|
+
* @default false
|
|
250
|
+
* @type {boolean}
|
|
251
|
+
*/
|
|
252
|
+
verbose?: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* Enables/disables extended verbose logging. The output may contain
|
|
255
|
+
* sensitive information, so use it with caution. Does not work if a verbose
|
|
256
|
+
* option is disabled.
|
|
257
|
+
*
|
|
258
|
+
* @default false
|
|
259
|
+
* @type {boolean}
|
|
260
|
+
*/
|
|
261
|
+
verboseExtended?: boolean;
|
|
246
262
|
}
|
|
247
263
|
export interface EventMap {
|
|
248
264
|
message: [data: any, id: string, from: string];
|
package/src/RedisQueue.d.ts
CHANGED
|
@@ -138,6 +138,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
138
138
|
* @param {IMQMode} [mode]
|
|
139
139
|
*/
|
|
140
140
|
constructor(name: string, options?: Partial<IMQOptions>, mode?: IMQMode);
|
|
141
|
+
private verbose;
|
|
141
142
|
/**
|
|
142
143
|
* Creates a subscription channel over redis and sets up channel
|
|
143
144
|
* data read handler
|
|
@@ -447,7 +448,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
447
448
|
*/
|
|
448
449
|
private ownWatch;
|
|
449
450
|
/**
|
|
450
|
-
* This method returns watcher lock resolver function
|
|
451
|
+
* This method returns a watcher lock resolver function
|
|
451
452
|
*
|
|
452
453
|
* @access private
|
|
453
454
|
* @param {(...args: any[]) => void} resolve
|
package/src/RedisQueue.js
CHANGED
|
@@ -57,6 +57,8 @@ exports.DEFAULT_IMQ_OPTIONS = {
|
|
|
57
57
|
safeDeliveryTtl: 5000,
|
|
58
58
|
useGzip: false,
|
|
59
59
|
watcherCheckDelay: 5000,
|
|
60
|
+
verbose: false,
|
|
61
|
+
verboseExtended: false,
|
|
60
62
|
};
|
|
61
63
|
exports.IMQ_SHUTDOWN_TIMEOUT = +(process.env.IMQ_SHUTDOWN_TIMEOUT || 1000);
|
|
62
64
|
/**
|
|
@@ -100,6 +102,7 @@ function pack(data) {
|
|
|
100
102
|
function unpack(data) {
|
|
101
103
|
return JSON.parse((0, zlib_1.gunzipSync)(Buffer.from(data, 'binary')).toString());
|
|
102
104
|
}
|
|
105
|
+
const IMQ_REDIS_MAX_LISTENERS_LIMIT = +(process.env.IMQ_REDIS_MAX_LISTENERS_LIMIT || 10000);
|
|
103
106
|
/**
|
|
104
107
|
* Class RedisQueue
|
|
105
108
|
* Implements simple messaging queue over redis.
|
|
@@ -169,6 +172,19 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
169
172
|
this.unpack = this.options.useGzip ? unpack : JSON.parse;
|
|
170
173
|
/* tslint:enable */
|
|
171
174
|
this.redisKey = `${this.options.host}:${this.options.port}`;
|
|
175
|
+
this.verbose(`Initializing queue on ${this.options.host}:${this.options.port} with prefix ${this.options.prefix} and safeDelivery = ${this.options.safeDelivery}, and safeDeliveryTtl = ${this.options.safeDeliveryTtl}, and watcherCheckDelay = ${this.options.watcherCheckDelay}, and useGzip = ${this.options.useGzip}`);
|
|
176
|
+
}
|
|
177
|
+
verbose(message, sensitiveMessage, sensitiveOnly) {
|
|
178
|
+
if (sensitiveOnly && !this.options.verboseExtended) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (this.options.verbose) {
|
|
182
|
+
const text = `[IMQ-CORE][${this.name}]: ${message}`;
|
|
183
|
+
const fullText = this.options.verboseExtended
|
|
184
|
+
? `${text}${sensitiveMessage}`
|
|
185
|
+
: text;
|
|
186
|
+
this.logger.info(fullText);
|
|
187
|
+
}
|
|
172
188
|
}
|
|
173
189
|
/**
|
|
174
190
|
* Creates a subscription channel over redis and sets up channel
|
|
@@ -198,7 +214,9 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
198
214
|
if (ch === fcn && typeof handler === 'function') {
|
|
199
215
|
handler(JSON.parse(message));
|
|
200
216
|
}
|
|
217
|
+
this.verbose(`Received message from ${ch} channel`, `, data: ${JSON.stringify(message)}`);
|
|
201
218
|
});
|
|
219
|
+
this.verbose(`Subscribed to ${channel} channel`);
|
|
202
220
|
}
|
|
203
221
|
/**
|
|
204
222
|
* Closes subscription channel
|
|
@@ -207,12 +225,14 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
207
225
|
*/
|
|
208
226
|
async unsubscribe() {
|
|
209
227
|
if (this.subscription) {
|
|
228
|
+
this.verbose('Initialize unsubscribing...');
|
|
210
229
|
if (this.subscriptionName) {
|
|
211
|
-
this.subscription.unsubscribe(`${this.options.prefix}:${this.subscriptionName}`);
|
|
230
|
+
await this.subscription.unsubscribe(`${this.options.prefix}:${this.subscriptionName}`);
|
|
231
|
+
this.verbose(`Unsubscribed from ${this.subscriptionName} channel`);
|
|
212
232
|
}
|
|
213
233
|
this.subscription.removeAllListeners();
|
|
214
234
|
this.subscription.disconnect(false);
|
|
215
|
-
this.subscription.quit();
|
|
235
|
+
await this.subscription.quit();
|
|
216
236
|
}
|
|
217
237
|
this.subscriptionName = undefined;
|
|
218
238
|
this.subscription = undefined;
|
|
@@ -232,7 +252,10 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
232
252
|
if (!this.writer) {
|
|
233
253
|
throw new TypeError('Writer is not connected!');
|
|
234
254
|
}
|
|
235
|
-
|
|
255
|
+
const jsonData = JSON.stringify(data);
|
|
256
|
+
const name = toName || this.name;
|
|
257
|
+
await this.writer.publish(`${this.options.prefix}:${name}`, jsonData);
|
|
258
|
+
this.verbose(`Published message to ${name} channel`, `, data: ${jsonData}`);
|
|
236
259
|
}
|
|
237
260
|
/**
|
|
238
261
|
* Initializes and starts current queue routines
|
|
@@ -250,19 +273,27 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
250
273
|
const connPromises = [];
|
|
251
274
|
// istanbul ignore next
|
|
252
275
|
if (!this.reader && this.isWorker()) {
|
|
276
|
+
this.verbose('Initializing reader...');
|
|
253
277
|
connPromises.push(this.connect('reader', this.options));
|
|
254
278
|
}
|
|
255
279
|
if (!this.writer) {
|
|
280
|
+
this.verbose('Initializing writer...');
|
|
256
281
|
connPromises.push(this.connect('writer', this.options));
|
|
257
282
|
}
|
|
258
283
|
await Promise.all(connPromises);
|
|
284
|
+
this.verbose('Connections initialized');
|
|
259
285
|
if (!this.signalsInitialized) {
|
|
286
|
+
this.verbose('Setting up OS signal handlers...');
|
|
260
287
|
// istanbul ignore next
|
|
261
288
|
const free = async () => {
|
|
262
289
|
let exitCode = 0;
|
|
263
|
-
setTimeout(() =>
|
|
290
|
+
setTimeout(() => {
|
|
291
|
+
this.verbose(`Shutting down after ${exports.IMQ_SHUTDOWN_TIMEOUT} timeout`);
|
|
292
|
+
process.exit(exitCode);
|
|
293
|
+
}, exports.IMQ_SHUTDOWN_TIMEOUT);
|
|
264
294
|
try {
|
|
265
295
|
if (this.watchOwner) {
|
|
296
|
+
this.verbose('Freeing watcher lock...');
|
|
266
297
|
await this.unlock();
|
|
267
298
|
}
|
|
268
299
|
}
|
|
@@ -275,6 +306,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
275
306
|
process.on('SIGINT', free);
|
|
276
307
|
process.on('SIGABRT', free);
|
|
277
308
|
this.signalsInitialized = true;
|
|
309
|
+
this.verbose('OS signal handlers initialized!');
|
|
278
310
|
}
|
|
279
311
|
await this.initWatcher();
|
|
280
312
|
this.initialized = true;
|
|
@@ -304,24 +336,40 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
304
336
|
const data = { id, message, from: this.name };
|
|
305
337
|
const key = `${this.options.prefix}:${toQueue}`;
|
|
306
338
|
const packet = this.pack(data);
|
|
307
|
-
|
|
339
|
+
this.verbose('Message send', `: ${packet}`, true);
|
|
340
|
+
const cb = (error, op) => {
|
|
308
341
|
// istanbul ignore next
|
|
309
|
-
if (error
|
|
310
|
-
|
|
342
|
+
if (error) {
|
|
343
|
+
this.verbose(`Writer ${op} error: ${error}`);
|
|
344
|
+
if (errorHandler) {
|
|
345
|
+
errorHandler(error);
|
|
346
|
+
}
|
|
311
347
|
}
|
|
312
348
|
};
|
|
313
349
|
if (delay) {
|
|
314
|
-
this.writer.zadd(`${key}:delayed`, Date.now() + delay, packet, (
|
|
350
|
+
await this.writer.zadd(`${key}:delayed`, Date.now() + delay, packet, (e) => {
|
|
315
351
|
// istanbul ignore next
|
|
316
|
-
if (
|
|
317
|
-
cb(
|
|
352
|
+
if (e) {
|
|
353
|
+
cb(e, 'ZADD');
|
|
318
354
|
return;
|
|
319
355
|
}
|
|
320
|
-
this.writer.set(`${key}:${id}:ttl`, '', 'PX', delay, 'NX',
|
|
356
|
+
this.writer.set(`${key}:${id}:ttl`, '', 'PX', delay, 'NX', (err) => {
|
|
357
|
+
// istanbul ignore next
|
|
358
|
+
if (err) {
|
|
359
|
+
cb(err, 'SET');
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
}).catch((err) => cb(err, 'SET'));
|
|
321
363
|
});
|
|
322
364
|
}
|
|
323
365
|
else {
|
|
324
|
-
this.writer.lpush(key, packet,
|
|
366
|
+
await this.writer.lpush(key, packet, (err) => {
|
|
367
|
+
// istanbul ignore next
|
|
368
|
+
if (err) {
|
|
369
|
+
cb(err, 'LPUSH');
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
325
373
|
}
|
|
326
374
|
return id;
|
|
327
375
|
}
|
|
@@ -331,13 +379,16 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
331
379
|
* @returns {Promise<RedisQueue>}
|
|
332
380
|
*/
|
|
333
381
|
async stop() {
|
|
382
|
+
this.verbose('Stopping queue...');
|
|
334
383
|
if (this.reader) {
|
|
384
|
+
this.verbose('Destroying reader...');
|
|
335
385
|
this.reader.removeAllListeners();
|
|
336
|
-
this.reader.quit();
|
|
386
|
+
await this.reader.quit();
|
|
337
387
|
this.reader.disconnect(false);
|
|
338
388
|
delete this.reader;
|
|
339
389
|
}
|
|
340
390
|
this.initialized = false;
|
|
391
|
+
this.verbose('Queue stopped!');
|
|
341
392
|
return this;
|
|
342
393
|
}
|
|
343
394
|
/**
|
|
@@ -346,6 +397,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
346
397
|
* @returns {Promise<void>}
|
|
347
398
|
*/
|
|
348
399
|
async destroy() {
|
|
400
|
+
this.verbose('Destroying queue...');
|
|
349
401
|
this.destroyed = true;
|
|
350
402
|
this.removeAllListeners();
|
|
351
403
|
this.cleanSafeCheckInterval();
|
|
@@ -354,6 +406,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
354
406
|
await this.clear();
|
|
355
407
|
this.destroyWriter();
|
|
356
408
|
await this.unsubscribe();
|
|
409
|
+
this.verbose('Queue destroyed!');
|
|
357
410
|
}
|
|
358
411
|
/**
|
|
359
412
|
* Clears queue data in redis
|
|
@@ -365,10 +418,12 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
365
418
|
return this;
|
|
366
419
|
}
|
|
367
420
|
try {
|
|
421
|
+
this.verbose('Clearing expired queue keys...');
|
|
368
422
|
await Promise.all([
|
|
369
423
|
this.writer.del(this.key),
|
|
370
424
|
this.writer.del(`${this.key}:delayed`),
|
|
371
425
|
]);
|
|
426
|
+
this.verbose('Expired queue keys cleared!');
|
|
372
427
|
}
|
|
373
428
|
catch (err) {
|
|
374
429
|
// istanbul ignore next
|
|
@@ -476,10 +531,14 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
476
531
|
*/
|
|
477
532
|
destroyWatcher() {
|
|
478
533
|
if (this.watcher) {
|
|
534
|
+
this.verbose('Destroying watcher...');
|
|
479
535
|
this.watcher.removeAllListeners();
|
|
480
|
-
this.watcher.quit()
|
|
536
|
+
this.watcher.quit().catch(e => {
|
|
537
|
+
this.verbose(`Error quitting watcher: ${e}`);
|
|
538
|
+
});
|
|
481
539
|
this.watcher.disconnect(false);
|
|
482
540
|
delete RedisQueue.watchers[this.redisKey];
|
|
541
|
+
this.verbose('Watcher destroyed!');
|
|
483
542
|
}
|
|
484
543
|
}
|
|
485
544
|
/**
|
|
@@ -489,10 +548,14 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
489
548
|
*/
|
|
490
549
|
destroyWriter() {
|
|
491
550
|
if (this.writer) {
|
|
551
|
+
this.verbose('Destroying writer...');
|
|
492
552
|
this.writer.removeAllListeners();
|
|
493
|
-
this.writer.quit()
|
|
553
|
+
this.writer.quit().catch(e => {
|
|
554
|
+
this.verbose(`Error quitting writer: ${e}`);
|
|
555
|
+
});
|
|
494
556
|
this.writer.disconnect(false);
|
|
495
557
|
delete RedisQueue.writers[this.redisKey];
|
|
558
|
+
this.verbose('Writer destroyed!');
|
|
496
559
|
}
|
|
497
560
|
}
|
|
498
561
|
/**
|
|
@@ -505,6 +568,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
505
568
|
* @returns {Promise<IRedisClient>}
|
|
506
569
|
*/
|
|
507
570
|
async connect(channel, options, context = this) {
|
|
571
|
+
this.verbose(`Connecting to ${channel} channel...`);
|
|
508
572
|
// istanbul ignore next
|
|
509
573
|
if (context[channel]) {
|
|
510
574
|
return context[channel];
|
|
@@ -519,7 +583,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
519
583
|
username: options.username,
|
|
520
584
|
// istanbul ignore next
|
|
521
585
|
password: options.password,
|
|
522
|
-
connectionName: this.getChannelName(context.name, options.prefix || '', channel),
|
|
586
|
+
connectionName: this.getChannelName(context.name + '', options.prefix || '', channel),
|
|
523
587
|
retryStrategy: this.retryStrategy(context),
|
|
524
588
|
autoResubscribe: true,
|
|
525
589
|
enableReadyCheck: true,
|
|
@@ -528,7 +592,18 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
528
592
|
});
|
|
529
593
|
context[channel] = (0, _1.makeRedisSafe)(redis);
|
|
530
594
|
context[channel].__imq = true;
|
|
531
|
-
|
|
595
|
+
for (const event of [
|
|
596
|
+
'wait',
|
|
597
|
+
'reconnecting',
|
|
598
|
+
'connecting',
|
|
599
|
+
'connect',
|
|
600
|
+
'close',
|
|
601
|
+
]) {
|
|
602
|
+
redis.on(event, () => {
|
|
603
|
+
context.verbose(`Redis Event fired: ${event}`);
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
redis.setMaxListeners(IMQ_REDIS_MAX_LISTENERS_LIMIT);
|
|
532
607
|
redis.on('ready', this.onReadyHandler(context, channel, resolve));
|
|
533
608
|
redis.on('error', this.onErrorHandler(context, channel, reject));
|
|
534
609
|
redis.on('end', this.onCloseHandler(context, channel));
|
|
@@ -547,6 +622,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
547
622
|
if (context.destroyed) {
|
|
548
623
|
return null;
|
|
549
624
|
}
|
|
625
|
+
this.verbose('Redis connection error, retrying...');
|
|
550
626
|
return 200;
|
|
551
627
|
};
|
|
552
628
|
}
|
|
@@ -560,6 +636,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
560
636
|
* @return {() => Promise<void>}
|
|
561
637
|
*/
|
|
562
638
|
onReadyHandler(context, channel, resolve) {
|
|
639
|
+
this.verbose(`Redis ${channel} channel ready!`);
|
|
563
640
|
return (async () => {
|
|
564
641
|
this.logger.info('%s: %s channel connected, host %s, pid %s', context.name, channel, this.redisKey, process.pid);
|
|
565
642
|
switch (channel) {
|
|
@@ -601,6 +678,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
601
678
|
onErrorHandler(context, channel, reject) {
|
|
602
679
|
// istanbul ignore next
|
|
603
680
|
return ((err) => {
|
|
681
|
+
this.verbose(`Redis Error: ${err}`);
|
|
604
682
|
if (this.destroyed) {
|
|
605
683
|
return;
|
|
606
684
|
}
|
|
@@ -620,6 +698,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
620
698
|
* @return {(...args: any[]) => any}
|
|
621
699
|
*/
|
|
622
700
|
onCloseHandler(context, channel) {
|
|
701
|
+
this.verbose(`Redis ${channel} is closing...`);
|
|
623
702
|
// istanbul ignore next
|
|
624
703
|
return (() => {
|
|
625
704
|
this.initialized = false;
|
|
@@ -634,14 +713,18 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
634
713
|
* @returns {RedisQueue}
|
|
635
714
|
*/
|
|
636
715
|
process(msg) {
|
|
716
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
637
717
|
const [queue, data] = msg;
|
|
638
718
|
// istanbul ignore next
|
|
639
719
|
if (!queue || queue !== this.key) {
|
|
640
720
|
return this;
|
|
641
721
|
}
|
|
642
722
|
try {
|
|
723
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument
|
|
643
724
|
const { id, message, from } = this.unpack(data);
|
|
725
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
644
726
|
this.emit('message', message, id, from);
|
|
727
|
+
this.verbose('Message received', `: ${data}`, true);
|
|
645
728
|
}
|
|
646
729
|
catch (err) {
|
|
647
730
|
// istanbul ignore next
|
|
@@ -724,6 +807,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
724
807
|
if (!keys.length) {
|
|
725
808
|
return;
|
|
726
809
|
}
|
|
810
|
+
this.verbose(`Watching ${keys.length} keys: ${keys.map(key => `"${key}"`).join(', ')}`);
|
|
727
811
|
for (const key of keys) {
|
|
728
812
|
const kp = key.split(':');
|
|
729
813
|
if (Number(kp.pop()) < now) {
|
|
@@ -742,7 +826,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
742
826
|
*/
|
|
743
827
|
async onWatchMessage(...args) {
|
|
744
828
|
try {
|
|
745
|
-
const key = (args.pop() || '').split(':');
|
|
829
|
+
const key = ((args.pop() || '') + '').split(':');
|
|
746
830
|
if (key.pop() !== 'ttl') {
|
|
747
831
|
return;
|
|
748
832
|
}
|
|
@@ -777,18 +861,21 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
777
861
|
return this;
|
|
778
862
|
}
|
|
779
863
|
try {
|
|
780
|
-
this.writer.config('SET', 'notify-keyspace-events', 'Ex')
|
|
864
|
+
this.writer.config('SET', 'notify-keyspace-events', 'Ex').catch(err => {
|
|
865
|
+
this.emitError('OnConfig', 'events config error', err);
|
|
866
|
+
});
|
|
781
867
|
}
|
|
782
868
|
catch (err) {
|
|
783
869
|
this.emitError('OnConfig', 'events config error', err);
|
|
784
870
|
}
|
|
785
871
|
this.watcher.on('pmessage', this.onWatchMessage.bind(this));
|
|
786
|
-
this.watcher.psubscribe('__keyevent@0__:expired', `${this.options.prefix}:delayed:*`)
|
|
872
|
+
this.watcher.psubscribe('__keyevent@0__:expired', `${this.options.prefix}:delayed:*`).catch(err => {
|
|
873
|
+
this.verbose(`Error subscribing to watcher channel: ${err}`);
|
|
874
|
+
});
|
|
787
875
|
// watch for expired unhandled safe queues
|
|
788
876
|
if (!this.safeCheckInterval) {
|
|
789
|
-
// tslint:disable-next-line:triple-equals no-null-keyword
|
|
790
877
|
if (this.options.safeDeliveryTtl != null) {
|
|
791
|
-
this.safeCheckInterval = setInterval(async () => {
|
|
878
|
+
this.safeCheckInterval = setInterval((async () => {
|
|
792
879
|
if (!this.writer) {
|
|
793
880
|
this.cleanSafeCheckInterval();
|
|
794
881
|
return;
|
|
@@ -797,7 +884,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
797
884
|
await this.processWatch();
|
|
798
885
|
}
|
|
799
886
|
await this.processCleanup();
|
|
800
|
-
}, this.options.safeDeliveryTtl);
|
|
887
|
+
}), this.options.safeDeliveryTtl);
|
|
801
888
|
}
|
|
802
889
|
}
|
|
803
890
|
this.watcher.__ready__ = true;
|
|
@@ -810,13 +897,15 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
810
897
|
* @returns {Promise<RedisQueue | undefined>}
|
|
811
898
|
*/
|
|
812
899
|
async processCleanup() {
|
|
900
|
+
this.verbose('Cleaning up orphaned keys...');
|
|
813
901
|
try {
|
|
814
902
|
if (!this.options.cleanup) {
|
|
815
903
|
return;
|
|
816
904
|
}
|
|
817
905
|
const filter = new RegExp(this.options.prefix + ':' +
|
|
818
906
|
(this.options.cleanupFilter || '*').replace(/\*/g, '.*'), 'i');
|
|
819
|
-
|
|
907
|
+
this.verbose(`Cleaning up keys matching ${filter}`);
|
|
908
|
+
const clients = (await this.writer.client('LIST')).toString() || '';
|
|
820
909
|
const connectedKeys = (clients.match(RX_CLIENT_NAME) || [])
|
|
821
910
|
.filter((name) => RX_CLIENT_TEST.test(name) && filter.test(name))
|
|
822
911
|
.map((name) => name
|
|
@@ -825,6 +914,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
825
914
|
.filter((name, i, a) => a.indexOf(name) === i);
|
|
826
915
|
const keysToRemove = [];
|
|
827
916
|
let cursor = '0';
|
|
917
|
+
this.verbose(`Found connected keys: ${connectedKeys.map(k => `"${k}"`).join(', ')}`);
|
|
828
918
|
while (true) {
|
|
829
919
|
const data = await this.writer.scan(cursor, 'MATCH', `${this.options.prefix}:${this.options.cleanupFilter || '*'}`, 'COUNT', '1000');
|
|
830
920
|
cursor = data.shift();
|
|
@@ -837,6 +927,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
837
927
|
}
|
|
838
928
|
if (keysToRemove.length) {
|
|
839
929
|
await this.writer.del(...keysToRemove);
|
|
930
|
+
this.verbose(`Keys ${keysToRemove.map(k => `"${k}"`).join(', ')} were successfully removed!`);
|
|
840
931
|
}
|
|
841
932
|
}
|
|
842
933
|
catch (err) {
|
|
@@ -913,9 +1004,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
913
1004
|
}
|
|
914
1005
|
catch (err) {
|
|
915
1006
|
// istanbul ignore next
|
|
916
|
-
this.emitError('OnReadSafe', 'safe reader failed',
|
|
917
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
918
|
-
err);
|
|
1007
|
+
this.emitError('OnReadSafe', 'safe reader failed', err);
|
|
919
1008
|
}
|
|
920
1009
|
}
|
|
921
1010
|
/**
|
|
@@ -984,6 +1073,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
984
1073
|
emitError(eventName, message, err) {
|
|
985
1074
|
this.emit('error', err, eventName);
|
|
986
1075
|
this.logger.error(`${this.name}: ${message}, pid ${process.pid} on redis host ${this.redisKey}:`, err);
|
|
1076
|
+
this.verbose(`Error in event ${eventName}: ${message}, pid ${process.pid} on redis host ${this.redisKey}: ${err}`);
|
|
987
1077
|
}
|
|
988
1078
|
/**
|
|
989
1079
|
* Acquires an owner for watcher connection to this instance of the queue
|
|
@@ -994,6 +1084,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
994
1084
|
async ownWatch() {
|
|
995
1085
|
const owned = await this.lock();
|
|
996
1086
|
if (owned) {
|
|
1087
|
+
this.verbose('Watcher connection lock acquired!');
|
|
997
1088
|
for (const script of Object.keys(this.scripts)) {
|
|
998
1089
|
try {
|
|
999
1090
|
const checksum = sha1(this.scripts[script].code);
|
|
@@ -1015,7 +1106,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
1015
1106
|
}
|
|
1016
1107
|
// istanbul ignore next
|
|
1017
1108
|
/**
|
|
1018
|
-
* This method returns watcher lock resolver function
|
|
1109
|
+
* This method returns a watcher lock resolver function
|
|
1019
1110
|
*
|
|
1020
1111
|
* @access private
|
|
1021
1112
|
* @param {(...args: any[]) => void} resolve
|
|
@@ -1045,9 +1136,10 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
1045
1136
|
*/
|
|
1046
1137
|
// istanbul ignore next
|
|
1047
1138
|
async initWatcher() {
|
|
1048
|
-
return new Promise(async (resolve, reject) => {
|
|
1139
|
+
return new Promise((async (resolve, reject) => {
|
|
1049
1140
|
try {
|
|
1050
1141
|
if (!await this.watcherCount()) {
|
|
1142
|
+
this.verbose('Initializing watcher...');
|
|
1051
1143
|
await this.ownWatch();
|
|
1052
1144
|
if (this.watchOwner && this.watcher) {
|
|
1053
1145
|
resolve();
|
|
@@ -1065,7 +1157,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
1065
1157
|
this.logger.error(`${this.name}: error initializing watcher, pid ${process.pid} on redis host ${this.redisKey}`, err);
|
|
1066
1158
|
reject(err);
|
|
1067
1159
|
}
|
|
1068
|
-
});
|
|
1160
|
+
}));
|
|
1069
1161
|
}
|
|
1070
1162
|
}
|
|
1071
1163
|
exports.RedisQueue = RedisQueue;
|