@imqueue/core 2.0.4 → 2.0.5

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.4",
3
+ "version": "2.0.5",
4
4
  "description": "Simple JSON-based messaging queue for inter service communication",
5
5
  "keywords": [
6
6
  "message-queue",
@@ -25,7 +25,7 @@ import { IMessageQueueConnection, IServerInput } from './IMessageQueue';
25
25
  export interface ICluster {
26
26
  add: (server: IServerInput) => void;
27
27
  remove: (server: IServerInput) => void;
28
- find: <T extends IMessageQueueConnection>(server: IServerInput) => T | undefined;
28
+ find: <T extends IMessageQueueConnection>(server: IServerInput, strict?: boolean) => T | undefined;
29
29
  }
30
30
  export interface InitializedCluster extends ICluster {
31
31
  id: string;
@@ -34,6 +34,7 @@ export declare abstract class ClusterManager {
34
34
  protected clusters: InitializedCluster[];
35
35
  protected constructor();
36
36
  init(cluster: ICluster): InitializedCluster;
37
+ anyCluster(fn: (cluster: InitializedCluster) => Promise<void> | void): Promise<void>;
37
38
  remove(cluster: string | InitializedCluster, destroy?: boolean): Promise<void>;
38
39
  abstract destroy(): Promise<void>;
39
40
  }
@@ -11,6 +11,9 @@ class ClusterManager {
11
11
  this.clusters.push(initializedCluster);
12
12
  return initializedCluster;
13
13
  }
14
+ async anyCluster(fn) {
15
+ await Promise.all(this.clusters.map(cluster => fn(cluster)));
16
+ }
14
17
  async remove(cluster, destroy = true) {
15
18
  const id = typeof cluster === 'string' ? cluster : cluster.id;
16
19
  this.clusters = this.clusters.filter(cluster => cluster.id !== id);
@@ -22,7 +22,7 @@
22
22
  * <support@imqueue.com> to get commercial licensing options.
23
23
  */
24
24
  import { EventEmitter } from 'events';
25
- import { ILogger, IMessageQueue, IMQMode, IMQOptions, JsonObject, EventMap, IServerInput } from '.';
25
+ import { EventMap, ILogger, IMessageQueue, IMQMode, IMQOptions, IServerInput, JsonObject } from '.';
26
26
  /**
27
27
  * Class ClusteredRedisQueue
28
28
  * Implements the possibility to scale queues horizontally between several
@@ -367,7 +367,7 @@ class ClusteredRedisQueue {
367
367
  * @returns {void}
368
368
  */
369
369
  removeServer(server) {
370
- const remove = this.findServer(server);
370
+ const remove = this.findServer(server, true);
371
371
  if (!remove) {
372
372
  return;
373
373
  }
@@ -391,14 +391,12 @@ class ClusteredRedisQueue {
391
391
  const opts = Object.assign(Object.assign({}, this.mqOptions), newServer);
392
392
  const imq = new _1.RedisQueue(this.name, opts);
393
393
  if (initializeQueue) {
394
- this.initializeQueue(imq)
395
- .then(() => {
394
+ this.initializeQueue(imq).then(() => {
396
395
  this.clusterEmitter.emit('initialized', {
397
396
  server: newServer,
398
397
  imq,
399
398
  });
400
- })
401
- .catch();
399
+ });
402
400
  }
403
401
  newServer.imq = imq;
404
402
  this.imqs.push(imq);
@@ -418,16 +416,19 @@ class ClusteredRedisQueue {
418
416
  await imq.subscribe(this.state.subscription.channel, this.state.subscription.handler);
419
417
  }
420
418
  }
421
- findServer(server) {
422
- return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server));
419
+ findServer(server, strict = false) {
420
+ return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server, strict));
423
421
  }
424
- static matchServers(source, target) {
422
+ static matchServers(source, target, strict = false) {
425
423
  const sameAddress = target.host === source.host
426
424
  && target.port === source.port;
427
425
  if (!target.id && !source.id) {
428
426
  return sameAddress;
429
427
  }
430
428
  const sameId = target.id === source.id;
429
+ if (strict) {
430
+ return sameId && sameAddress;
431
+ }
431
432
  return sameId || sameAddress;
432
433
  }
433
434
  }
@@ -92,7 +92,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
92
92
  */
93
93
  private destroyed;
94
94
  /**
95
- * True if the current instance owns watcher connection, false otherwise
95
+ * True if the current instance owns a watcher connection, false otherwise
96
96
  *
97
97
  * @type {boolean}
98
98
  */
@@ -230,7 +230,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
230
230
  */
231
231
  private get watcher();
232
232
  /**
233
- * Watcher setter, sets the watcher connection property for this
233
+ * Watcher setter sets the watcher connection property for this
234
234
  * queue instance
235
235
  *
236
236
  * @param {IRedisClient} conn
@@ -332,14 +332,14 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
332
332
  */
333
333
  private process;
334
334
  /**
335
- * Returns number of established watcher connections on redis
335
+ * Returns the number of established watcher connections on redis
336
336
  *
337
337
  * @access private
338
338
  * @returns {Promise<number>}
339
339
  */
340
340
  private watcherCount;
341
341
  /**
342
- * Processes delayed message by its given redis key
342
+ * Processes delayed a message by its given redis key
343
343
  *
344
344
  * @access private
345
345
  * @param {string} key
package/src/RedisQueue.js CHANGED
@@ -128,7 +128,7 @@ class RedisQueue extends events_1.EventEmitter {
128
128
  */
129
129
  this.destroyed = false;
130
130
  /**
131
- * True if the current instance owns watcher connection, false otherwise
131
+ * True if the current instance owns a watcher connection, false otherwise
132
132
  *
133
133
  * @type {boolean}
134
134
  */
@@ -246,6 +246,7 @@ class RedisQueue extends events_1.EventEmitter {
246
246
  if (this.initialized) {
247
247
  return this;
248
248
  }
249
+ this.destroyed = false;
249
250
  const connPromises = [];
250
251
  // istanbul ignore next
251
252
  if (!this.reader && this.isWorker()) {
@@ -277,7 +278,6 @@ class RedisQueue extends events_1.EventEmitter {
277
278
  }
278
279
  await this.initWatcher();
279
280
  this.initialized = true;
280
- this.destroyed = false;
281
281
  return this;
282
282
  }
283
283
  /**
@@ -297,6 +297,9 @@ class RedisQueue extends events_1.EventEmitter {
297
297
  if (!this.writer) {
298
298
  await this.start();
299
299
  }
300
+ if (!this.writer) {
301
+ throw new TypeError('IMQ: unable to initialize queue!');
302
+ }
300
303
  const id = (0, _1.uuid)();
301
304
  const data = { id, message, from: this.name };
302
305
  const key = `${this.options.prefix}:${toQueue}`;
@@ -420,7 +423,7 @@ class RedisQueue extends events_1.EventEmitter {
420
423
  }
421
424
  // noinspection JSUnusedLocalSymbols
422
425
  /**
423
- * Watcher setter, sets the watcher connection property for this
426
+ * Watcher setter sets the watcher connection property for this
424
427
  * queue instance
425
428
  *
426
429
  * @param {IRedisClient} conn
@@ -633,22 +636,22 @@ class RedisQueue extends events_1.EventEmitter {
633
636
  return this;
634
637
  }
635
638
  /**
636
- * Returns number of established watcher connections on redis
639
+ * Returns the number of established watcher connections on redis
637
640
  *
638
641
  * @access private
639
642
  * @returns {Promise<number>}
640
643
  */
641
644
  // istanbul ignore next
642
645
  async watcherCount() {
646
+ if (!this.writer) {
647
+ return 0;
648
+ }
643
649
  const rx = new RegExp(`\\bname=${this.options.prefix}:[\\S]+?:watcher:`);
644
650
  const list = await this.writer.client('LIST');
645
- return (list || '')
646
- .split(/\r?\n/)
647
- .filter(client => rx.test(client))
648
- .length;
651
+ return list.split(/\r?\n/).filter(client => rx.test(client)).length;
649
652
  }
650
653
  /**
651
- * Processes delayed message by its given redis key
654
+ * Processes delayed a message by its given redis key
652
655
  *
653
656
  * @access private
654
657
  * @param {string} key
@@ -1,18 +1,10 @@
1
- import { ICluster, ClusterManager } from './ClusterManager';
1
+ import { ClusterManager } from './ClusterManager';
2
2
  export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: {
3
3
  broadcastPort: number;
4
4
  broadcastAddress: string;
5
5
  aliveTimeoutCorrection: number;
6
6
  };
7
7
  export interface UDPClusterManagerOptions {
8
- /**
9
- * Represents the cluster operations that are responsible for managing
10
- * clusters. This includes operations such as adding, removing, or checking
11
- * if a cluster server exists.
12
- *
13
- * @type {ICluster}
14
- */
15
- cluster?: ICluster;
16
8
  /**
17
9
  * Message queue broadcast port
18
10
  *
@@ -74,7 +74,7 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
74
74
  }
75
75
  if (!server && message.type === MessageType.Up) {
76
76
  cluster.add(message);
77
- const added = cluster.find(message);
77
+ const added = cluster.find(message, true);
78
78
  if (added) {
79
79
  UDPClusterManager.serverAliveWait(cluster, added, aliveTimeoutCorrection);
80
80
  }
@@ -94,9 +94,9 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
94
94
  && !UDPClusterManager.verifyHosts(message.host, context.options.includeHosts)) {
95
95
  return;
96
96
  }
97
- for (const cluster of context.clusters) {
97
+ context.anyCluster(cluster => {
98
98
  UDPClusterManager.processMessageOnCluster(cluster, message, context.options.aliveTimeoutCorrection);
99
- }
99
+ }).then();
100
100
  };
101
101
  }
102
102
  static parseBroadcastedMessage(input) {
@@ -112,26 +112,40 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
112
112
  };
113
113
  }
114
114
  static serverAliveWait(cluster, server, aliveTimeoutCorrection, message) {
115
- clearTimeout(server.timer);
115
+ if (server.timer) {
116
+ clearTimeout(server.timer);
117
+ server.timer = undefined;
118
+ }
116
119
  server.timestamp = Date.now();
117
120
  if (message) {
118
121
  server.timeout = message.timeout;
119
122
  }
120
123
  const correction = aliveTimeoutCorrection || 0;
121
124
  const timeout = (server.timeout || 0) + correction;
122
- server.timer = setTimeout(() => {
123
- const existing = cluster.find(server);
124
- if (!existing) {
125
+ if (timeout <= 0) {
126
+ return;
127
+ }
128
+ const timerId = setTimeout(() => {
129
+ const existing = cluster.find(server, true);
130
+ if (!existing || existing.timer !== timerId) {
125
131
  return;
126
132
  }
127
133
  const now = Date.now();
128
- const delta = now - (existing.timestamp || now);
134
+ if (!existing.timestamp) {
135
+ clearTimeout(existing.timer);
136
+ existing.timer = undefined;
137
+ cluster.remove(existing);
138
+ return;
139
+ }
140
+ const delta = now - existing.timestamp;
129
141
  const currentTimeout = (existing.timeout || 0) + correction;
130
142
  if (delta >= currentTimeout) {
131
- clearTimeout(server.timer);
132
- cluster.remove(server);
143
+ clearTimeout(existing.timer);
144
+ existing.timer = undefined;
145
+ cluster.remove(existing);
133
146
  }
134
147
  }, timeout);
148
+ server.timer = timerId;
135
149
  }
136
150
  /**
137
151
  * Destroys the UDPClusterManager by closing all opened network connections