@imqueue/core 2.0.24 → 2.0.25

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/core",
3
- "version": "2.0.24",
3
+ "version": "2.0.25",
4
4
  "description": "Simple JSON-based messaging queue for inter service communication",
5
5
  "keywords": [
6
6
  "message-queue",
@@ -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
- return new Promise((resolve, reject) => {
590
- const redis = new redis_1.default({
591
- // istanbul ignore next
592
- port: options.port || 6379,
593
- // istanbul ignore next
594
- host: options.host || 'localhost',
595
- // istanbul ignore next
596
- username: options.username,
597
- // istanbul ignore next
598
- password: options.password,
599
- connectionName: this.getChannelName(context.name + '', options.prefix || '', channel),
600
- retryStrategy: this.retryStrategy(),
601
- autoResubscribe: true,
602
- enableOfflineQueue: true,
603
- autoResendUnfulfilledCommands: true,
604
- offlineQueue: true,
605
- maxRetriesPerRequest: null,
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
  /**
@@ -697,33 +710,6 @@ class RedisQueue extends events_1.EventEmitter {
697
710
  }
698
711
  }, delay);
699
712
  }
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
713
  // noinspection JSMethodCanBeStatic
728
714
  /**
729
715
  * Generates channel name
@@ -743,22 +729,20 @@ class RedisQueue extends events_1.EventEmitter {
743
729
  * @access private
744
730
  * @param {RedisQueue} context
745
731
  * @param {RedisConnectionChannel} channel
746
- * @param {(...args: any[]) => void} reject
747
732
  * @return {(err: Error) => void}
748
733
  */
749
- onErrorHandler(context, channel, reject) {
734
+ onErrorHandler(context, channel) {
750
735
  // istanbul ignore next
751
- return ((err) => {
752
- this.verbose(`Redis Error: ${err}`);
736
+ return ((error) => {
737
+ var _a;
738
+ this.verbose(`Redis Error: ${error}`);
753
739
  if (this.destroyed) {
754
740
  return;
755
741
  }
756
- this.logger.error(`${context.name}: error connecting redis host ${this.redisKey} on ${channel}, pid ${process.pid}:`, err);
757
- if (!this.initialized) {
758
- reject(err);
759
- }
760
- else {
761
- // Try to recover the channel using our reconnection routine
742
+ this.logger.error(`${context.name}: error connecting redis host ${this.redisKey} on ${channel}, pid ${process.pid}:`, error);
743
+ if (error.code === 'ECONNREFUSED' ||
744
+ error.code === 'ETIMEDOUT' ||
745
+ ((_a = context[channel]) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') {
762
746
  this.scheduleReconnect(channel);
763
747
  }
764
748
  });