@imqueue/core 3.2.4 → 3.3.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/CHANGELOG.md +1 -1
- package/index.d.ts +70 -9
- package/index.js +70 -9
- package/package.json +1 -5
- package/src/ClusterManager.d.ts +81 -2
- package/src/ClusterManager.js +51 -2
- package/src/ClusteredRedisQueue.d.ts +361 -67
- package/src/ClusteredRedisQueue.js +340 -67
- package/src/IMQMode.d.ts +24 -0
- package/src/IMQMode.js +24 -0
- package/src/IMessageQueue.d.ts +410 -136
- package/src/RedisQueue.d.ts +284 -158
- package/src/RedisQueue.js +282 -163
- package/src/UDPClusterManager.d.ts +149 -33
- package/src/UDPClusterManager.js +74 -9
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/profile.d.ts +170 -31
- package/src/profile.js +138 -32
- package/src/redis.d.ts +14 -4
- package/src/redis.js +1 -1
|
@@ -32,47 +32,56 @@ import { DEFAULT_IMQ_OPTIONS, IMQMode, RedisQueue, } from './index.js';
|
|
|
32
32
|
*/
|
|
33
33
|
const SEND_INIT_TIMEOUT = +(process.env.IMQ_SEND_INIT_TIMEOUT || 0) || 30000;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
|
|
37
|
-
*
|
|
35
|
+
* Scales a single logical queue horizontally across several redis instances.
|
|
36
|
+
* This is what {@link IMQ.create} returns when {@link IMQOptions.cluster} or
|
|
37
|
+
* {@link IMQOptions.clusterManagers} is supplied.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* Distribution is asymmetric, and this is the most important thing to know about
|
|
41
|
+
* the class: {@link ClusteredRedisQueue.send} routes each message to exactly
|
|
42
|
+
* one server, chosen by health-aware round-robin that skips instances whose
|
|
43
|
+
* writer connection is not ready. Every other operation — `start`, `stop`,
|
|
44
|
+
* `clear`, `destroy`, `publish`, `subscribe`, `unsubscribe` and `queueLength` —
|
|
45
|
+
* fans out to every server.
|
|
46
|
+
*
|
|
47
|
+
* Every fan-out uses `Promise.all`, so one failing host fails the whole call with
|
|
48
|
+
* no partial-failure reporting and no rollback.
|
|
49
|
+
*
|
|
50
|
+
* The class only `implements` the `EventEmitter` interface rather than extending
|
|
51
|
+
* it, so `instanceof EventEmitter` is false and every emitter method is a
|
|
52
|
+
* delegating shim — see the individual methods for their fan-out semantics, and
|
|
53
|
+
* note in particular that {@link ClusteredRedisQueue.once} is per-server.
|
|
38
54
|
*/
|
|
39
55
|
export class ClusteredRedisQueue {
|
|
40
56
|
name;
|
|
41
57
|
/**
|
|
42
|
-
* Logger
|
|
58
|
+
* Logger used for this cluster's own messages, defaulting to
|
|
59
|
+
* {@link IMQOptions.logger} or `console`.
|
|
43
60
|
*
|
|
44
|
-
* @
|
|
61
|
+
* @remarks
|
|
62
|
+
* Replacing it affects cluster-level logging only — each per-host queue keeps
|
|
63
|
+
* the logger it was constructed with.
|
|
45
64
|
*/
|
|
46
65
|
logger;
|
|
47
66
|
/**
|
|
48
67
|
* RedisQueue instances collection
|
|
49
|
-
*
|
|
50
|
-
* @type {RedisQueue[]}
|
|
51
68
|
*/
|
|
52
69
|
imqs = [];
|
|
53
70
|
/**
|
|
54
71
|
* Options associated with this queue instance
|
|
55
|
-
*
|
|
56
|
-
* @type {IMQOptions}
|
|
57
72
|
*/
|
|
58
73
|
options;
|
|
59
74
|
/**
|
|
60
75
|
* Part of options without cluster definitions - which are generic for
|
|
61
76
|
* RedisQueue instances
|
|
62
|
-
*
|
|
63
|
-
* @type {IMQOptions}
|
|
64
77
|
*/
|
|
65
78
|
mqOptions;
|
|
66
79
|
/**
|
|
67
80
|
* Cluster servers option definitions
|
|
68
|
-
*
|
|
69
|
-
* @type {IMessageQueueConnection[]}
|
|
70
81
|
*/
|
|
71
82
|
servers = [];
|
|
72
83
|
/**
|
|
73
84
|
* Current queue index (round-robin)
|
|
74
|
-
*
|
|
75
|
-
* @type {number}
|
|
76
85
|
*/
|
|
77
86
|
currentQueue = 0;
|
|
78
87
|
/**
|
|
@@ -81,22 +90,16 @@ export class ClusteredRedisQueue {
|
|
|
81
90
|
sendInitTimeout = SEND_INIT_TIMEOUT;
|
|
82
91
|
/**
|
|
83
92
|
* Total length of RedisQueue instances
|
|
84
|
-
*
|
|
85
|
-
* @type {number}
|
|
86
93
|
*/
|
|
87
94
|
imqLength = 0;
|
|
88
95
|
/**
|
|
89
96
|
* Template EventEmitter instance used to replicate queue EventEmitters when
|
|
90
97
|
* dynamically modifying the cluster
|
|
91
|
-
* @type {EventEmitter}
|
|
92
|
-
* @private
|
|
93
98
|
*/
|
|
94
99
|
templateEmitter;
|
|
95
100
|
/**
|
|
96
101
|
* Cluster EventEmitter instance used to notify about changes of
|
|
97
102
|
* cluster servers
|
|
98
|
-
* @type {EventEmitter}
|
|
99
|
-
* @private
|
|
100
103
|
*/
|
|
101
104
|
clusterEmitter;
|
|
102
105
|
state = {
|
|
@@ -105,13 +108,40 @@ export class ClusteredRedisQueue {
|
|
|
105
108
|
};
|
|
106
109
|
initializedClusters = [];
|
|
107
110
|
/**
|
|
108
|
-
*
|
|
111
|
+
* Creates a clustered queue.
|
|
112
|
+
*
|
|
113
|
+
* @param name - queue name, used as the queue name for every per-host queue
|
|
114
|
+
* in the cluster
|
|
115
|
+
* @param options - queue options; must supply {@link IMQOptions.cluster},
|
|
116
|
+
* {@link IMQOptions.clusterManagers}, or both
|
|
117
|
+
* @param _mode - accepted only for {@link IMessageQueueConstructor}
|
|
118
|
+
* signature compatibility and ignored: the underlying queues always
|
|
119
|
+
* run in {@link IMQMode.BOTH}
|
|
120
|
+
* @throws TypeError when neither `cluster` nor a non-empty `clusterManagers`
|
|
121
|
+
* is supplied
|
|
109
122
|
*
|
|
110
|
-
* @
|
|
111
|
-
* @
|
|
112
|
-
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* Construction is not inert. One {@link RedisQueue} is created per static
|
|
125
|
+
* cluster entry, and every cluster manager is initialized immediately — so
|
|
126
|
+
* dynamically discovered servers can join before
|
|
127
|
+
* {@link ClusteredRedisQueue.start} is ever called.
|
|
128
|
+
*
|
|
129
|
+
* Cluster entries contribute only `id`, `host` and `port`. All other
|
|
130
|
+
* connection settings, including `username` and `password`, are inherited from
|
|
131
|
+
* the top-level options, so per-server credentials in a cluster entry are
|
|
132
|
+
* ignored.
|
|
113
133
|
*/
|
|
114
|
-
constructor(
|
|
134
|
+
constructor(
|
|
135
|
+
/**
|
|
136
|
+
* Name of this queue, used as the queue name for every per-host queue in
|
|
137
|
+
* the cluster.
|
|
138
|
+
*
|
|
139
|
+
* @remarks
|
|
140
|
+
* It is read when a server joins, so changing it after construction
|
|
141
|
+
* affects only servers added later — which would silently split the
|
|
142
|
+
* cluster across two queue names. Avoid reassigning it.
|
|
143
|
+
*/
|
|
144
|
+
name, options, _mode = IMQMode.BOTH) {
|
|
115
145
|
this.name = name;
|
|
116
146
|
this.templateEmitter = new EventEmitter();
|
|
117
147
|
this.clusterEmitter = new EventEmitter();
|
|
@@ -138,36 +168,59 @@ export class ClusteredRedisQueue {
|
|
|
138
168
|
}
|
|
139
169
|
}
|
|
140
170
|
/**
|
|
141
|
-
* Starts
|
|
142
|
-
*
|
|
171
|
+
* Starts every server's queue concurrently.
|
|
172
|
+
*
|
|
173
|
+
* @returns this queue instance
|
|
174
|
+
*
|
|
175
|
+
* @remarks
|
|
176
|
+
* A failure on any one server rejects this call while the others continue
|
|
177
|
+
* starting, and the cluster stays in the started state — so servers that join
|
|
178
|
+
* afterwards are still started automatically.
|
|
143
179
|
*
|
|
144
|
-
*
|
|
180
|
+
* Always emits one informational log line, regardless of
|
|
181
|
+
* {@link IMQOptions.verbose}.
|
|
145
182
|
*/
|
|
146
183
|
async start() {
|
|
147
184
|
this.state.started = true;
|
|
148
185
|
return await this.batch('start', 'Starting clustered redis message queue...');
|
|
149
186
|
}
|
|
150
187
|
/**
|
|
151
|
-
* Stops
|
|
152
|
-
* Supposed to be an async function.
|
|
188
|
+
* Stops message handling on every server concurrently.
|
|
153
189
|
*
|
|
154
|
-
* @returns
|
|
190
|
+
* @returns this queue instance
|
|
191
|
+
*
|
|
192
|
+
* @remarks
|
|
193
|
+
* Connections are kept and any active subscription is retained, so servers
|
|
194
|
+
* joining afterwards are still subscribed to the remembered channel while
|
|
195
|
+
* remaining stopped. Use {@link ClusteredRedisQueue.destroy} to tear the
|
|
196
|
+
* connections down.
|
|
155
197
|
*/
|
|
156
198
|
async stop() {
|
|
157
199
|
this.state.started = false;
|
|
158
200
|
return await this.batch('stop', 'Stopping clustered redis message queue...');
|
|
159
201
|
}
|
|
160
202
|
/**
|
|
161
|
-
* Sends a message to
|
|
162
|
-
*
|
|
203
|
+
* Sends a message to one server of the cluster, selected by health-aware
|
|
204
|
+
* round-robin.
|
|
205
|
+
*
|
|
206
|
+
* @param toQueue - queue name to which a message should be sent to
|
|
207
|
+
* @param message - message data
|
|
208
|
+
* @param delay - if specified, a message will be handled in the target queue
|
|
209
|
+
* after a specified period of time in milliseconds
|
|
210
|
+
* @param errorHandler - callback called only when an internal error occurs
|
|
211
|
+
* during message send execution
|
|
212
|
+
* @returns message identifier
|
|
213
|
+
* @throws TypeError propagated from the selected server when it is in
|
|
214
|
+
* {@link IMQMode.WORKER}-only mode
|
|
215
|
+
*
|
|
216
|
+
* @remarks
|
|
217
|
+
* This is the one operation that does not fan out — the message goes to a
|
|
218
|
+
* single server, and not to a stable one.
|
|
163
219
|
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
|
|
168
|
-
* @param {(err: Error) => void} [errorHandler] - callback called only when
|
|
169
|
-
internal error occurs during message send execution.
|
|
170
|
-
* @returns {Promise<string>} - message identifier
|
|
220
|
+
* When the cluster currently has no servers the send is held until the first
|
|
221
|
+
* server becomes ready, and rejects if none appears within 30 seconds
|
|
222
|
+
* (override with the `IMQ_SEND_INIT_TIMEOUT` environment variable, in
|
|
223
|
+
* milliseconds).
|
|
171
224
|
*/
|
|
172
225
|
async send(toQueue, message, delay, errorHandler) {
|
|
173
226
|
if (!this.imqLength) {
|
|
@@ -181,8 +234,6 @@ export class ClusteredRedisQueue {
|
|
|
181
234
|
* whose redis connection is currently ready so messages are not routed
|
|
182
235
|
* to a host that is known to be down. Falls back to the plain
|
|
183
236
|
* round-robin pick when no instance reports are ready.
|
|
184
|
-
*
|
|
185
|
-
* @returns {RedisQueue}
|
|
186
237
|
*/
|
|
187
238
|
selectQueue() {
|
|
188
239
|
const count = this.imqLength;
|
|
@@ -202,8 +253,6 @@ export class ClusteredRedisQueue {
|
|
|
202
253
|
* Sends a message once the first cluster server becomes available.
|
|
203
254
|
* Rejects (rather than hanging forever) if none appears within the
|
|
204
255
|
* configured timeout and propagates any sent failure.
|
|
205
|
-
*
|
|
206
|
-
* @returns {Promise<string>}
|
|
207
256
|
*/
|
|
208
257
|
sendWhenInitialized(toQueue, message, delay, errorHandler) {
|
|
209
258
|
return new Promise((resolve, reject) => {
|
|
@@ -220,11 +269,17 @@ export class ClusteredRedisQueue {
|
|
|
220
269
|
});
|
|
221
270
|
}
|
|
222
271
|
/**
|
|
223
|
-
*
|
|
224
|
-
* listeners
|
|
225
|
-
*
|
|
272
|
+
* Destroys every server's queue — closing their connections and removing
|
|
273
|
+
* their event listeners — then unregisters this cluster from all configured
|
|
274
|
+
* cluster managers.
|
|
275
|
+
*
|
|
276
|
+
* @remarks
|
|
277
|
+
* Unregistering shuts a manager down entirely once it has no clusters left,
|
|
278
|
+
* which for {@link UDPClusterManager} also terminates its shared UDP worker.
|
|
226
279
|
*
|
|
227
|
-
*
|
|
280
|
+
* The instance must not be reused afterwards: internal routing state is not
|
|
281
|
+
* cleared, so a subsequent {@link ClusteredRedisQueue.send} would silently
|
|
282
|
+
* re-open a connection.
|
|
228
283
|
*/
|
|
229
284
|
async destroy() {
|
|
230
285
|
this.state.started = false;
|
|
@@ -239,14 +294,25 @@ export class ClusteredRedisQueue {
|
|
|
239
294
|
}
|
|
240
295
|
}
|
|
241
296
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
297
|
+
* Deletes this queue's data on every redis host in the cluster,
|
|
298
|
+
* concurrently.
|
|
244
299
|
*
|
|
245
|
-
* @returns
|
|
300
|
+
* @returns this queue instance
|
|
246
301
|
*/
|
|
247
302
|
async clear() {
|
|
248
303
|
return await this.batch('clear', 'Clearing clustered redis message queue...');
|
|
249
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Returns the total number of messages waiting, summed across every redis
|
|
307
|
+
* host in the cluster.
|
|
308
|
+
*
|
|
309
|
+
* @returns sum of the per-host queue lengths
|
|
310
|
+
*
|
|
311
|
+
* @remarks
|
|
312
|
+
* Resolves to `0` when the cluster has no servers, and rejects if any single
|
|
313
|
+
* host cannot be queried. As with {@link RedisQueue.queueLength}, delayed and
|
|
314
|
+
* in-flight messages are not counted.
|
|
315
|
+
*/
|
|
250
316
|
async queueLength() {
|
|
251
317
|
const promises = [];
|
|
252
318
|
for (const imq of this.imqs) {
|
|
@@ -263,9 +329,8 @@ export class ClusteredRedisQueue {
|
|
|
263
329
|
/**
|
|
264
330
|
* Batch imq action processing on all registered imqs at once
|
|
265
331
|
*
|
|
266
|
-
* @param
|
|
267
|
-
* @param
|
|
268
|
-
* @returns {Promise<this>}
|
|
332
|
+
* @param action -
|
|
333
|
+
* @param message -
|
|
269
334
|
*/
|
|
270
335
|
async batch(action, message) {
|
|
271
336
|
this.logger.info(message);
|
|
@@ -284,10 +349,10 @@ export class ClusteredRedisQueue {
|
|
|
284
349
|
* (method chosen by name), so a single contained cast bridges the dynamic
|
|
285
350
|
* call while the public method signatures below stay fully typed.
|
|
286
351
|
*
|
|
287
|
-
* @
|
|
288
|
-
* @param
|
|
289
|
-
* @param
|
|
290
|
-
* @returns
|
|
352
|
+
* @typeParam K - EventEmitter method name
|
|
353
|
+
* @param method - name of the EventEmitter method to invoke
|
|
354
|
+
* @param args - arguments to pass to the method
|
|
355
|
+
* @returns results from each emitter call
|
|
291
356
|
*/
|
|
292
357
|
applyToEmitters(method, args) {
|
|
293
358
|
const results = [];
|
|
@@ -297,66 +362,226 @@ export class ClusteredRedisQueue {
|
|
|
297
362
|
}
|
|
298
363
|
return results;
|
|
299
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* Registers a listener on every server's queue and on the internal template
|
|
367
|
+
* used to seed servers that join later.
|
|
368
|
+
*
|
|
369
|
+
* @param args - the arguments `EventEmitter.on` accepts
|
|
370
|
+
* @returns this queue instance
|
|
371
|
+
*/
|
|
300
372
|
on(...args) {
|
|
301
373
|
this.applyToEmitters('on', args);
|
|
302
374
|
return this;
|
|
303
375
|
}
|
|
376
|
+
/**
|
|
377
|
+
* Removes a listener from every server's queue and from the internal
|
|
378
|
+
* template.
|
|
379
|
+
*
|
|
380
|
+
* @param args - the arguments `EventEmitter.off` accepts
|
|
381
|
+
* @returns this queue instance
|
|
382
|
+
*/
|
|
304
383
|
off(...args) {
|
|
305
384
|
this.applyToEmitters('off', args);
|
|
306
385
|
return this;
|
|
307
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Registers a one-shot listener on every server's queue and on the internal
|
|
389
|
+
* template.
|
|
390
|
+
*
|
|
391
|
+
* @param args - the arguments `EventEmitter.once` accepts
|
|
392
|
+
* @returns this queue instance
|
|
393
|
+
*
|
|
394
|
+
* @remarks
|
|
395
|
+
* Because registration is replicated per server, this arms one independent
|
|
396
|
+
* one-shot listener per server — so the callback may run once for each
|
|
397
|
+
* server in the cluster rather than once overall, and more as servers join.
|
|
398
|
+
* Use {@link ClusteredRedisQueue.on} plus explicit de-registration if you
|
|
399
|
+
* need at-most-once semantics.
|
|
400
|
+
*/
|
|
308
401
|
once(...args) {
|
|
309
402
|
this.applyToEmitters('once', args);
|
|
310
403
|
return this;
|
|
311
404
|
}
|
|
405
|
+
/**
|
|
406
|
+
* Registers a listener on every server's queue and on the internal template.
|
|
407
|
+
* Alias of {@link ClusteredRedisQueue.on}.
|
|
408
|
+
*
|
|
409
|
+
* @param args - the arguments `EventEmitter.addListener` accepts
|
|
410
|
+
* @returns this queue instance
|
|
411
|
+
*/
|
|
312
412
|
addListener(...args) {
|
|
313
413
|
this.applyToEmitters('addListener', args);
|
|
314
414
|
return this;
|
|
315
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Removes a listener from every server's queue and from the internal
|
|
418
|
+
* template. Alias of {@link ClusteredRedisQueue.off}.
|
|
419
|
+
*
|
|
420
|
+
* @param args - the arguments `EventEmitter.removeListener` accepts
|
|
421
|
+
* @returns this queue instance
|
|
422
|
+
*/
|
|
316
423
|
removeListener(...args) {
|
|
317
424
|
this.applyToEmitters('removeListener', args);
|
|
318
425
|
return this;
|
|
319
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
* Removes every listener from every server's queue and from the internal
|
|
429
|
+
* template, so servers that join later also start clean.
|
|
430
|
+
*
|
|
431
|
+
* @param args - the arguments `EventEmitter.removeAllListeners` accepts
|
|
432
|
+
* @returns this queue instance
|
|
433
|
+
*/
|
|
320
434
|
removeAllListeners(...args) {
|
|
321
435
|
this.applyToEmitters('removeAllListeners', args);
|
|
322
436
|
return this;
|
|
323
437
|
}
|
|
438
|
+
/**
|
|
439
|
+
* Registers a listener at the front of the queue on every server's queue and
|
|
440
|
+
* on the internal template.
|
|
441
|
+
*
|
|
442
|
+
* @param args - the arguments `EventEmitter.prependListener` accepts
|
|
443
|
+
* @returns this queue instance
|
|
444
|
+
*/
|
|
324
445
|
prependListener(...args) {
|
|
325
446
|
this.applyToEmitters('prependListener', args);
|
|
326
447
|
return this;
|
|
327
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* Registers a one-shot listener at the front of the queue on every server's
|
|
451
|
+
* queue and on the internal template.
|
|
452
|
+
*
|
|
453
|
+
* @param args - the arguments `EventEmitter.prependOnceListener` accepts
|
|
454
|
+
* @returns this queue instance
|
|
455
|
+
*
|
|
456
|
+
* @remarks
|
|
457
|
+
* As with {@link ClusteredRedisQueue.once}, the listener is armed per server,
|
|
458
|
+
* so it may fire more than once across the cluster.
|
|
459
|
+
*/
|
|
328
460
|
prependOnceListener(...args) {
|
|
329
461
|
this.applyToEmitters('prependOnceListener', args);
|
|
330
462
|
return this;
|
|
331
463
|
}
|
|
464
|
+
/**
|
|
465
|
+
* Sets the maximum listener count on every server's queue and on the internal
|
|
466
|
+
* template.
|
|
467
|
+
*
|
|
468
|
+
* @param args - the arguments `EventEmitter.setMaxListeners` accepts
|
|
469
|
+
* @returns this queue instance
|
|
470
|
+
*/
|
|
332
471
|
setMaxListeners(...args) {
|
|
333
472
|
this.applyToEmitters('setMaxListeners', args);
|
|
334
473
|
return this;
|
|
335
474
|
}
|
|
336
|
-
|
|
337
|
-
|
|
475
|
+
/**
|
|
476
|
+
* Returns the listeners of every server's queue plus the internal template,
|
|
477
|
+
* concatenated.
|
|
478
|
+
*
|
|
479
|
+
* @param args - the arguments `EventEmitter.listeners` accepts
|
|
480
|
+
* @returns the aggregated listeners across the cluster
|
|
481
|
+
*
|
|
482
|
+
* @remarks
|
|
483
|
+
* Because registration is replicated, one listener registered through this
|
|
484
|
+
* class appears once per server plus once for the template — so the array
|
|
485
|
+
* length is a multiple of the logical listener count, not the count itself.
|
|
486
|
+
* Do not compare it against
|
|
487
|
+
* {@link ClusteredRedisQueue.listenerCount}, which reports a single server.
|
|
488
|
+
*
|
|
489
|
+
* The return type is deliberately widened to `any[]`, so Node's per-event
|
|
490
|
+
* listener typing is not available here.
|
|
491
|
+
*/
|
|
338
492
|
listeners(...args) {
|
|
339
493
|
return this.applyToEmitters('listeners', args).flat();
|
|
340
494
|
}
|
|
495
|
+
/**
|
|
496
|
+
* Returns the raw listeners of every server's queue plus the internal
|
|
497
|
+
* template, concatenated.
|
|
498
|
+
*
|
|
499
|
+
* @param args - the arguments `EventEmitter.rawListeners` accepts
|
|
500
|
+
* @returns the aggregated raw listeners across the cluster
|
|
501
|
+
*
|
|
502
|
+
* @remarks
|
|
503
|
+
* Aggregated in the same way as {@link ClusteredRedisQueue.listeners}, with
|
|
504
|
+
* the same multiplicity caveat.
|
|
505
|
+
*/
|
|
341
506
|
rawListeners(...args) {
|
|
342
507
|
return this.applyToEmitters('rawListeners', args).flat();
|
|
343
508
|
}
|
|
509
|
+
/**
|
|
510
|
+
* Returns the maximum listener count of the internal template emitter.
|
|
511
|
+
*
|
|
512
|
+
* @returns the template's maximum listener count
|
|
513
|
+
*
|
|
514
|
+
* @remarks
|
|
515
|
+
* Reads the template only — never the underlying per-server queues.
|
|
516
|
+
*/
|
|
344
517
|
getMaxListeners() {
|
|
345
518
|
return this.templateEmitter.getMaxListeners();
|
|
346
519
|
}
|
|
520
|
+
/**
|
|
521
|
+
* Emits an event on every server's queue and on the internal template, so
|
|
522
|
+
* each listener registered through this class runs once per server.
|
|
523
|
+
*
|
|
524
|
+
* @param args - the arguments `EventEmitter.emit` accepts
|
|
525
|
+
* @returns always `true`
|
|
526
|
+
*
|
|
527
|
+
* @remarks
|
|
528
|
+
* The per-emitter results are discarded, so unlike `EventEmitter.emit` the
|
|
529
|
+
* return value does not report whether the event had any listeners — it is
|
|
530
|
+
* `true` even for an empty cluster with no listeners at all.
|
|
531
|
+
*/
|
|
347
532
|
emit(...args) {
|
|
348
533
|
this.applyToEmitters('emit', args);
|
|
349
534
|
return true;
|
|
350
535
|
}
|
|
536
|
+
/**
|
|
537
|
+
* Returns the event names reported by a single representative emitter.
|
|
538
|
+
*
|
|
539
|
+
* @returns the event names from the first server's queue, or from the internal
|
|
540
|
+
* template when the cluster is empty
|
|
541
|
+
*
|
|
542
|
+
* @remarks
|
|
543
|
+
* Like {@link ClusteredRedisQueue.listenerCount}, this does not aggregate
|
|
544
|
+
* across the cluster.
|
|
545
|
+
*/
|
|
351
546
|
eventNames() {
|
|
352
547
|
const source = this.imqs[0] || this.templateEmitter;
|
|
353
548
|
return source.eventNames();
|
|
354
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* Returns the listener count reported by a single representative emitter.
|
|
552
|
+
*
|
|
553
|
+
* @param args - the arguments `EventEmitter.listenerCount` accepts
|
|
554
|
+
* @returns the count from the first server's queue, or from the internal
|
|
555
|
+
* template when the cluster is empty
|
|
556
|
+
*
|
|
557
|
+
* @remarks
|
|
558
|
+
* This deliberately does not aggregate, which makes it inconsistent with
|
|
559
|
+
* {@link ClusteredRedisQueue.listeners}: for a three-server cluster with one
|
|
560
|
+
* registered `message` listener this returns `1` while `listeners('message')`
|
|
561
|
+
* returns four entries.
|
|
562
|
+
*/
|
|
355
563
|
listenerCount(...args) {
|
|
356
564
|
const source = this.imqs[0] || this.templateEmitter;
|
|
357
565
|
const fn = source.listenerCount;
|
|
358
566
|
return fn.apply(source, args);
|
|
359
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* Publishes the payload on every redis host in the cluster.
|
|
570
|
+
*
|
|
571
|
+
* @param data - payload to publish as a channel message
|
|
572
|
+
* @param toName - optional different pub/sub name to publish to
|
|
573
|
+
* @throws TypeError propagated from any host that has no writer connection
|
|
574
|
+
*
|
|
575
|
+
* @remarks
|
|
576
|
+
* This is the opposite of {@link ClusteredRedisQueue.send}: the same payload
|
|
577
|
+
* goes to all hosts. A subscriber connected to several of them therefore
|
|
578
|
+
* receives one copy per host.
|
|
579
|
+
*
|
|
580
|
+
* Publication is not atomic — if any host has no writer connection the call
|
|
581
|
+
* rejects even though other hosts may already have published. On an empty
|
|
582
|
+
* cluster it resolves without publishing anything, and unlike `send()` it does
|
|
583
|
+
* not wait for a server to appear.
|
|
584
|
+
*/
|
|
360
585
|
async publish(data, toName) {
|
|
361
586
|
const promises = [];
|
|
362
587
|
for (const imq of this.imqs) {
|
|
@@ -364,6 +589,25 @@ export class ClusteredRedisQueue {
|
|
|
364
589
|
}
|
|
365
590
|
await Promise.all(promises);
|
|
366
591
|
}
|
|
592
|
+
/**
|
|
593
|
+
* Subscribes the given handler on every redis host in the cluster, and
|
|
594
|
+
* remembers the subscription so servers that join later are subscribed
|
|
595
|
+
* automatically.
|
|
596
|
+
*
|
|
597
|
+
* @param channel - channel name within the queue's prefix namespace
|
|
598
|
+
* @param handler - invoked with the parsed payload of each published message
|
|
599
|
+
* @throws TypeError when a different channel name is supplied while a
|
|
600
|
+
* subscription is already open on the underlying queues
|
|
601
|
+
*
|
|
602
|
+
* @remarks
|
|
603
|
+
* Only one channel per instance is supported. Calling this again with the
|
|
604
|
+
* same channel registers the handler a second time; calling it with a
|
|
605
|
+
* different channel rejects — and the remembered subscription is left
|
|
606
|
+
* pointing at the rejected name, which is what newly joining servers would
|
|
607
|
+
* then use.
|
|
608
|
+
*
|
|
609
|
+
* The handler receives one invocation per host that delivers the message.
|
|
610
|
+
*/
|
|
367
611
|
async subscribe(channel, handler) {
|
|
368
612
|
this.state.subscription = { channel, handler };
|
|
369
613
|
const promises = [];
|
|
@@ -372,6 +616,14 @@ export class ClusteredRedisQueue {
|
|
|
372
616
|
}
|
|
373
617
|
await Promise.all(promises);
|
|
374
618
|
}
|
|
619
|
+
/**
|
|
620
|
+
* Unsubscribes from the channel on every redis host and forgets the
|
|
621
|
+
* remembered subscription, so servers joining later are no longer subscribed
|
|
622
|
+
* automatically.
|
|
623
|
+
*
|
|
624
|
+
* @remarks
|
|
625
|
+
* Resolves without effect on an empty cluster.
|
|
626
|
+
*/
|
|
375
627
|
async unsubscribe() {
|
|
376
628
|
this.state.subscription = null;
|
|
377
629
|
const promises = [];
|
|
@@ -381,20 +633,41 @@ export class ClusteredRedisQueue {
|
|
|
381
633
|
await Promise.all(promises);
|
|
382
634
|
}
|
|
383
635
|
/**
|
|
384
|
-
* Adds
|
|
636
|
+
* Adds a single server to the cluster and returns its registration record.
|
|
637
|
+
*
|
|
638
|
+
* @param server - address of the server to add
|
|
639
|
+
* @returns the registration record: the resolved `id`, `host` and `port` plus
|
|
640
|
+
* the {@link RedisQueue} instance (`imq`) created for that host, so
|
|
641
|
+
* callers can inspect or address that specific host
|
|
385
642
|
*
|
|
386
|
-
* @
|
|
387
|
-
*
|
|
643
|
+
* @remarks
|
|
644
|
+
* Registration is idempotent, and the match rule is broader than an id
|
|
645
|
+
* comparison: a server counts as already present when its `id` matches an
|
|
646
|
+
* existing entry or when its host and port do. Two different ids on the
|
|
647
|
+
* same host and port are therefore treated as one server, and the existing
|
|
648
|
+
* record is returned unchanged without creating a queue.
|
|
649
|
+
*
|
|
650
|
+
* For a genuinely new server this returns as soon as the record is created —
|
|
651
|
+
* starting the queue and re-applying any active subscription happen
|
|
652
|
+
* asynchronously afterwards.
|
|
388
653
|
*/
|
|
389
654
|
addServer(server) {
|
|
390
655
|
this.verbose(`Adding new server: ${JSON.stringify(server)}`);
|
|
391
656
|
return this.addServerWithQueueInitializing(server, true);
|
|
392
657
|
}
|
|
393
658
|
/**
|
|
394
|
-
* Removes server from the cluster
|
|
659
|
+
* Removes a server from the cluster, matching by `id` or by host and port.
|
|
660
|
+
*
|
|
661
|
+
* @param server - address of the server to remove
|
|
662
|
+
*
|
|
663
|
+
* @remarks
|
|
664
|
+
* A silent no-op when no server matches. Routing stops using the host
|
|
665
|
+
* immediately, but teardown of its redis connections is started in the
|
|
666
|
+
* background and not awaited: in-flight work on that host is not drained,
|
|
667
|
+
* and teardown errors are only logged.
|
|
395
668
|
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
669
|
+
* Because the underlying queues are matched by host and port, all entries
|
|
670
|
+
* sharing an address are removed together.
|
|
398
671
|
*/
|
|
399
672
|
removeServer(server) {
|
|
400
673
|
this.verbose(`Removing the server: ${JSON.stringify(server)}`);
|
package/src/IMQMode.d.ts
CHANGED
|
@@ -19,8 +19,32 @@
|
|
|
19
19
|
* purchase a proprietary commercial license. Please contact us at
|
|
20
20
|
* <support@imqueue.com> to get commercial licensing options.
|
|
21
21
|
*/
|
|
22
|
+
/**
|
|
23
|
+
* Operating mode of a queue instance, selecting which halves of the queue are
|
|
24
|
+
* active. Passed as the third constructor argument and defaults to
|
|
25
|
+
* {@link IMQMode.BOTH}.
|
|
26
|
+
*
|
|
27
|
+
* All modes still open a writer connection and take part in watcher election;
|
|
28
|
+
* the mode only controls whether a reader is created and whether sending is
|
|
29
|
+
* allowed.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* The members carry implicit numeric values and `BOTH` is `0`, so a falsy check
|
|
33
|
+
* such as `mode || IMQMode.WORKER` silently resolves to `WORKER`. Do not
|
|
34
|
+
* persist these values either — they shift if the members are reordered.
|
|
35
|
+
*/
|
|
22
36
|
export declare enum IMQMode {
|
|
37
|
+
/**
|
|
38
|
+
* Consume and produce. This is the default.
|
|
39
|
+
*/
|
|
23
40
|
BOTH = 0,
|
|
41
|
+
/**
|
|
42
|
+
* Consume only. No messages can be sent — `send()` throws a `TypeError`.
|
|
43
|
+
*/
|
|
24
44
|
WORKER = 1,
|
|
45
|
+
/**
|
|
46
|
+
* Produce only. No reader connection is opened, so the queue never emits
|
|
47
|
+
* `message` events and never releases delayed messages.
|
|
48
|
+
*/
|
|
25
49
|
PUBLISHER = 2
|
|
26
50
|
}
|