@imqueue/core 2.0.26 → 3.1.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/README.md +73 -63
- package/index.d.ts +0 -1
- package/index.js +8 -9
- package/package.json +19 -71
- package/src/ClusterManager.d.ts +12 -3
- package/src/ClusterManager.js +21 -10
- package/src/ClusteredRedisQueue.d.ts +45 -14
- package/src/ClusteredRedisQueue.js +186 -168
- package/src/IMessageQueue.d.ts +89 -96
- package/src/IMessageQueue.js +2 -2
- package/src/RedisQueue.d.ts +193 -97
- package/src/RedisQueue.js +1328 -1068
- package/src/UDPClusterManager.d.ts +100 -26
- package/src/UDPClusterManager.js +225 -70
- package/src/UDPWorker.d.ts +53 -1
- package/src/UDPWorker.js +79 -40
- package/src/{promisify.d.ts → helpers/buildOptions.d.ts} +8 -15
- package/src/helpers/buildOptions.js +39 -0
- package/src/{copyEventEmitter.d.ts → helpers/copyEventEmitter.d.ts} +1 -1
- package/src/{copyEventEmitter.js → helpers/copyEventEmitter.js} +3 -4
- package/src/helpers/envInt.d.ts +32 -0
- package/src/helpers/envInt.js +39 -0
- package/src/helpers/escapeRegExp.d.ts +30 -0
- package/src/helpers/escapeRegExp.js +36 -0
- package/src/helpers/index.d.ts +31 -0
- package/src/helpers/index.js +48 -0
- package/src/helpers/pack.d.ts +7 -0
- package/src/helpers/pack.js +37 -0
- package/src/helpers/randomInt.d.ts +31 -0
- package/src/helpers/randomInt.js +37 -0
- package/src/helpers/sha1.d.ts +7 -0
- package/src/helpers/sha1.js +39 -0
- package/src/helpers/unpack.d.ts +7 -0
- package/src/helpers/unpack.js +37 -0
- package/src/index.d.ts +1 -14
- package/src/index.js +1 -19
- package/src/profile.d.ts +28 -38
- package/src/profile.js +100 -95
- package/src/redis.d.ts +1 -1
- package/src/redis.js +4 -2
- package/debug.d.ts +0 -1
- package/debug_getter.d.ts +0 -9
- package/debug_listener.d.ts +0 -1
- package/eslint.config.mjs +0 -98
- package/src/promisify.js +0 -106
- package/src/uuid.d.ts +0 -6
- package/src/uuid.js +0 -65
package/README.md
CHANGED
|
@@ -2,63 +2,63 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://rawgit.com/imqueue/core/master/LICENSE)
|
|
4
4
|
|
|
5
|
-
Simple JSON-based messaging queue for inter
|
|
5
|
+
Simple JSON-based messaging queue for inter-service communication
|
|
6
6
|
|
|
7
7
|
**Related packages:**
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
- [@imqueue/rpc](https://github.com/imqueue/rpc) - RPC-like client/service
|
|
10
|
+
implementation over @imqueue/core.
|
|
11
|
+
- [@imqueue/cli](https://github.com/imqueue/cli) - Command Line Interface
|
|
12
|
+
for imqueue.
|
|
13
13
|
|
|
14
14
|
# Features
|
|
15
15
|
|
|
16
16
|
With current implementation on RedisQueue:
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
- **TypeScript included!**
|
|
18
|
+
- **Fast, unreliable** message delivery (if a consumer grabs the message and dies,
|
|
19
|
+
the message will be lost). Up to ~35–40k of 1Kb messages per second on an i7 core
|
|
20
|
+
by benchmarks.
|
|
21
|
+
- **Fast, guaranteed** message delivery (only 1.5–2 times slower than
|
|
22
|
+
unreliable mode). If a consumer grabs a message and dies, it will be rescheduled
|
|
23
|
+
to the queue. Up to ~20–25k of 1Kb messages per second on an i7 core by benchmarks.
|
|
24
|
+
- **No timers or constant Redis polling** used for implementation, resulting in
|
|
25
|
+
no delays in delivery and low CPU usage on application workers. When idle,
|
|
26
|
+
it consumes no resources.
|
|
27
|
+
- **Supports Gzip compression for messages** (decreases traffic usage but is slower).
|
|
28
|
+
- **Concurrent workers model supported**, the same queue can have multiple
|
|
29
|
+
consumers.
|
|
30
|
+
- **Delayed messages supported**, fast as ~10K of 1Kb messages per second on i7
|
|
31
|
+
core by benchmarks.
|
|
32
|
+
- **Safe, predictable scaling of queues**. Scaling the number of workers does not
|
|
33
|
+
increase traffic usage.
|
|
34
|
+
- **Round-robin message balancing between multiple Redis instances**. This
|
|
35
|
+
allows easy horizontal scaling of the messaging queue across Redis instances.
|
|
36
|
+
- **TypeScript included!**
|
|
38
37
|
|
|
39
38
|
# Requirements
|
|
40
39
|
|
|
41
|
-
Currently this module
|
|
42
|
-
|
|
40
|
+
Currently this module has only one available adapter, which is Redis. Redis server
|
|
41
|
+
6.2+ is required (the queue relies on `LMOVE`/`BLMOVE` commands for safe message
|
|
42
|
+
delivery).
|
|
43
43
|
|
|
44
|
-
If config command is disabled on
|
|
45
|
-
|
|
44
|
+
If the config command is disabled on Redis, you must manually enable keyspace
|
|
45
|
+
notification events (particularly when using AWS ElasticCache), like this:
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
```
|
|
48
48
|
notify-keyspace-events Ex
|
|
49
|
-
|
|
49
|
+
```
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
More adapters will be added in the future as needed.
|
|
52
52
|
|
|
53
53
|
# Install
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
```bash
|
|
56
56
|
npm i --save @imqueue/core
|
|
57
|
-
|
|
57
|
+
```
|
|
58
58
|
|
|
59
59
|
# Usage
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
```typescript
|
|
62
62
|
import IMQ, { IMessageQueue, IJson } from '@imqueue/core';
|
|
63
63
|
|
|
64
64
|
(async () => {
|
|
@@ -90,27 +90,27 @@ import IMQ, { IMessageQueue, IJson } from '@imqueue/core';
|
|
|
90
90
|
const delay = 1000;
|
|
91
91
|
await queueOne.send('QueueOne', { delay }, delay);
|
|
92
92
|
})();
|
|
93
|
-
|
|
93
|
+
```
|
|
94
94
|
|
|
95
95
|
# Benchmarking
|
|
96
96
|
|
|
97
|
-
First
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
First, make sure redis-server is running on localhost. The current version of the
|
|
98
|
+
benchmark requires Redis to be running on localhost so it can measure its CPU
|
|
99
|
+
usage statistics.
|
|
100
100
|
|
|
101
|
-
All workers during benchmark test will have
|
|
102
|
-
|
|
101
|
+
All workers during the benchmark test will have dedicated CPU affinity to ensure
|
|
102
|
+
the collected statistics are as accurate as possible.
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
git clone git@github.com:
|
|
106
|
-
cd
|
|
104
|
+
```bash
|
|
105
|
+
git clone git@github.com:imqueue/core.git
|
|
106
|
+
cd core
|
|
107
107
|
node benchmark -c 4 -m 10000
|
|
108
|
-
|
|
108
|
+
```
|
|
109
109
|
|
|
110
110
|
Other possible benchmark options:
|
|
111
111
|
|
|
112
|
-
|
|
113
|
-
node benchmark -h
|
|
112
|
+
```
|
|
113
|
+
node benchmark -h
|
|
114
114
|
Options:
|
|
115
115
|
--version Show version number [boolean]
|
|
116
116
|
-h, --help Show help [boolean]
|
|
@@ -128,30 +128,40 @@ Options:
|
|
|
128
128
|
-p, --port Redis server port to connect to.
|
|
129
129
|
-t, --message-multiply-times Increase sample message data given number of
|
|
130
130
|
times.
|
|
131
|
-
|
|
131
|
+
```
|
|
132
132
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
The number of child workers running message queues is limited to the number of CPUs
|
|
134
|
+
in the system minus 2. The first CPU (CPU0) is reserved for OS tasks and the stats
|
|
135
|
+
collector process. The second CPU (CPU1) is dedicated to the local Redis process.
|
|
136
|
+
All others are available to run queue workers.
|
|
137
137
|
|
|
138
|
-
For example,
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
For example, on an 8-core machine you can safely run up to 6 workers. On a 4-core
|
|
139
|
+
machine, this limit is 2 workers. If there are fewer cores, the results will not
|
|
140
|
+
provide good visibility of the load.
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
predictable.*
|
|
142
|
+
**NOTE:** The paragraphs above apply to Linux only. On macOS there is no reliable way
|
|
143
|
+
to set process CPU affinity, and Windows support is not currently implemented for
|
|
144
|
+
benchmarking. This does not mean the benchmark won't work on macOS or Windows, but
|
|
145
|
+
the results will not be accurate or predictable on those platforms.
|
|
147
146
|
|
|
148
147
|
## Running Unit Tests
|
|
149
148
|
|
|
150
|
-
|
|
149
|
+
Tests run on the native Node.js test runner (`node:test`) with `node:assert` and
|
|
150
|
+
no external test framework, so a plain clone and install is all that is needed:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
151
153
|
git clone git@github.com:imqueue/core.git
|
|
152
|
-
cd
|
|
154
|
+
cd core
|
|
155
|
+
npm install
|
|
153
156
|
npm test
|
|
154
|
-
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
To produce a coverage report use:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npm run test-coverage # prints coverage summary to the console
|
|
163
|
+
npm run test-lcov # writes coverage/lcov.info
|
|
164
|
+
```
|
|
155
165
|
|
|
156
166
|
## License
|
|
157
167
|
|
package/index.d.ts
CHANGED
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
* purchase a proprietary commercial license. Please contact us at
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
|
-
import 'reflect-metadata';
|
|
25
24
|
import { IMessageQueue, IMQMode, IMQOptions } from './src';
|
|
26
25
|
export * from './src';
|
|
27
26
|
/**
|
package/index.js
CHANGED
|
@@ -37,13 +37,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
37
37
|
* purchase a proprietary commercial license. Please contact us at
|
|
38
38
|
* <support@imqueue.com> to get commercial licensing options.
|
|
39
39
|
*/
|
|
40
|
-
require("reflect-metadata");
|
|
41
40
|
const src_1 = require("./src");
|
|
42
41
|
__exportStar(require("./src"), exports);
|
|
43
42
|
/**
|
|
44
43
|
* Message Queue Factory
|
|
45
44
|
*/
|
|
46
45
|
class IMQ {
|
|
46
|
+
/**
|
|
47
|
+
* Base generic queue factory options
|
|
48
|
+
*
|
|
49
|
+
* @type {Partial<IMQOptions>}
|
|
50
|
+
*/
|
|
51
|
+
static options = {
|
|
52
|
+
vendor: 'Redis', // default vendor
|
|
53
|
+
};
|
|
47
54
|
/**
|
|
48
55
|
* Instantiate proper message queue instance using given user-options
|
|
49
56
|
*
|
|
@@ -62,13 +69,5 @@ class IMQ {
|
|
|
62
69
|
return new Adapter(name, options, mode);
|
|
63
70
|
}
|
|
64
71
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Base generic queue factory options
|
|
67
|
-
*
|
|
68
|
-
* @type {Partial<IMQOptions>}
|
|
69
|
-
*/
|
|
70
|
-
IMQ.options = {
|
|
71
|
-
vendor: 'Redis', // default vendor
|
|
72
|
-
};
|
|
73
72
|
exports.default = IMQ;
|
|
74
73
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Simple JSON-based messaging queue for inter service communication",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"message-queue",
|
|
@@ -12,20 +12,23 @@
|
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
14
|
"benchmark": "node benchmark -c $(( $(nproc) - 2 )) -m 100000",
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
15
|
+
"clean-compiled": "npm run clean-js && npm run clean-typedefs && npm run clean-maps",
|
|
16
|
+
"build": "npm run clean-compiled && tsc",
|
|
17
|
+
"prepare": "npm run build",
|
|
18
|
+
"lint": "oxlint",
|
|
19
|
+
"format": "oxfmt \"index.ts\" \"src/**/*.ts\" \"test/**/*.ts\" \"benchmark/**/*.ts\"",
|
|
20
|
+
"format:check": "oxfmt --check \"index.ts\" \"src/**/*.ts\" \"test/**/*.ts\" \"benchmark/**/*.ts\"",
|
|
21
|
+
"test": "npm run build && node --experimental-test-module-mocks --test --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
22
|
+
"test-coverage": "npm run build && node --experimental-test-module-mocks --enable-source-maps --test --experimental-test-coverage --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
23
|
+
"test-lcov": "npm run build && mkdir -p coverage && node --experimental-test-module-mocks --enable-source-maps --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-timeout=15000 $(find test -name '*.spec.js'); node scripts/strip-comment-coverage.mjs coverage/lcov.info",
|
|
24
|
+
"test-coverage-html": "npm run test-lcov; genhtml coverage/lcov.info --output-directory coverage/html --ignore-errors inconsistent,corrupt,format,mismatch && echo \"Coverage report: file://$(pwd)/coverage/html/index.html\"",
|
|
19
25
|
"test-dev": "npm run test && npm run clean-js && npm run clean-typedefs && npm run clean-maps",
|
|
20
|
-
"test-coverage": "cat ./coverage/lcov.info | CODECLIMATE_API_HOST=https://codebeat.co/webhooks/code_coverage CODECLIMATE_REPO_TOKEN=85bb2a18-4ebb-4e48-a2ce-92b7bf438b1a ./node_modules/.bin/codeclimate-test-reporter",
|
|
21
26
|
"clean-typedefs": "find . -name '*.d.ts' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
22
27
|
"clean-maps": "find . -name '*.js.map' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
23
28
|
"clean-js": "find . -name '*.js' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
24
|
-
"clean-tests": "rm -rf .nyc_output coverage",
|
|
25
|
-
"clean-doc": "rm -rf docs",
|
|
29
|
+
"clean-tests": "rm -rf .nyc_output coverage .agent-out",
|
|
26
30
|
"clean-benchmark": "rm -rf benchmark-result",
|
|
27
|
-
"clean": "npm run clean-tests && npm run clean-typedefs && npm run clean-maps && npm run clean-js && npm run clean-
|
|
28
|
-
"doc": "rm -rf docs && typedoc --excludePrivate --excludeExternals --hideGenerator --exclude \"**/+(test|node_modules|docs|coverage|benchmark|.nyc_output)/**/*\" --out ./docs . && /usr/bin/env node -e \"import('open').then(open => open.default('file://`pwd`/docs/index.html',{wait:false}))\""
|
|
31
|
+
"clean": "npm run clean-tests && npm run clean-typedefs && npm run clean-maps && npm run clean-js && npm run clean-benchmark"
|
|
29
32
|
},
|
|
30
33
|
"repository": {
|
|
31
34
|
"type": "git",
|
|
@@ -38,69 +41,14 @@
|
|
|
38
41
|
"author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
|
|
39
42
|
"license": "GPL-3.0-only",
|
|
40
43
|
"dependencies": {
|
|
41
|
-
"ioredis": "^5.
|
|
44
|
+
"ioredis": "^5.11.1"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
|
-
"@
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"@types/mock-require": "^3.0.0",
|
|
49
|
-
"@types/node": "^24.2.1",
|
|
50
|
-
"@types/sinon": "^17.0.4",
|
|
51
|
-
"@types/yargs": "^17.0.33",
|
|
52
|
-
"@typescript-eslint/eslint-plugin": "^8.39.0",
|
|
53
|
-
"@typescript-eslint/parser": "^8.39.0",
|
|
54
|
-
"@typescript-eslint/typescript-estree": "^8.39.0",
|
|
55
|
-
"chai": "^5.2.1",
|
|
56
|
-
"coveralls-next": "^5.0.0",
|
|
57
|
-
"eslint": "^9.33.0",
|
|
58
|
-
"eslint-plugin-jsdoc": "^52.0.4",
|
|
59
|
-
"mocha": "^11.7.1",
|
|
60
|
-
"mocha-lcov-reporter": "^1.3.0",
|
|
61
|
-
"mock-require": "^3.0.3",
|
|
62
|
-
"nyc": "^17.1.0",
|
|
63
|
-
"open": "^10.2.0",
|
|
64
|
-
"reflect-metadata": "^0.2.2",
|
|
65
|
-
"sinon": "^21.0.0",
|
|
66
|
-
"source-map-support": "^0.5.21",
|
|
67
|
-
"ts-node": "^10.9.2",
|
|
68
|
-
"typedoc": "^0.28.9",
|
|
69
|
-
"typescript": "^5.9.2",
|
|
70
|
-
"yargs": "^18.0.0"
|
|
47
|
+
"@types/node": "^24.9.1",
|
|
48
|
+
"oxfmt": "0.57.0",
|
|
49
|
+
"oxlint": "1.72.0",
|
|
50
|
+
"typescript": "^7.0.2"
|
|
71
51
|
},
|
|
72
52
|
"main": "index.js",
|
|
73
|
-
"
|
|
74
|
-
"definitions": "index.d.ts"
|
|
75
|
-
},
|
|
76
|
-
"mocha": {
|
|
77
|
-
"require": [
|
|
78
|
-
"ts-node/register",
|
|
79
|
-
"source-map-support/register"
|
|
80
|
-
],
|
|
81
|
-
"recursive": true,
|
|
82
|
-
"bail": true,
|
|
83
|
-
"full-trace": true,
|
|
84
|
-
"exit": true,
|
|
85
|
-
"timeout": 10000
|
|
86
|
-
},
|
|
87
|
-
"nyc": {
|
|
88
|
-
"check-coverage": false,
|
|
89
|
-
"extension": [
|
|
90
|
-
".ts"
|
|
91
|
-
],
|
|
92
|
-
"exclude": [
|
|
93
|
-
"**/*.d.ts",
|
|
94
|
-
"**/test/**"
|
|
95
|
-
],
|
|
96
|
-
"require": [
|
|
97
|
-
"ts-node/register"
|
|
98
|
-
],
|
|
99
|
-
"reporter": [
|
|
100
|
-
"html",
|
|
101
|
-
"text",
|
|
102
|
-
"text-summary",
|
|
103
|
-
"lcovonly"
|
|
104
|
-
]
|
|
105
|
-
}
|
|
53
|
+
"types": "index.d.ts"
|
|
106
54
|
}
|
package/src/ClusterManager.d.ts
CHANGED
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { IMessageQueueConnection, IServerInput } from './IMessageQueue';
|
|
25
25
|
export interface ICluster {
|
|
26
|
-
add:
|
|
26
|
+
add: (server: IServerInput) => IMessageQueueConnection;
|
|
27
27
|
remove: (server: IServerInput) => void;
|
|
28
|
-
find:
|
|
28
|
+
find: (server: IServerInput) => IMessageQueueConnection | undefined;
|
|
29
29
|
}
|
|
30
30
|
export interface InitializedCluster extends ICluster {
|
|
31
31
|
id: string;
|
|
@@ -34,7 +34,16 @@ export declare abstract class ClusterManager {
|
|
|
34
34
|
protected clusters: InitializedCluster[];
|
|
35
35
|
protected constructor();
|
|
36
36
|
init(cluster: ICluster): InitializedCluster;
|
|
37
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Applies the given callback to every registered cluster. Each cluster
|
|
39
|
+
* is handled independently: a callback that throws (synchronously or
|
|
40
|
+
* asynchronously) for one cluster never prevents the remaining clusters
|
|
41
|
+
* from being processed.
|
|
42
|
+
*
|
|
43
|
+
* @param {(cluster: InitializedCluster) => Promise<void> | void} fn
|
|
44
|
+
* @returns {Promise<void>}
|
|
45
|
+
*/
|
|
46
|
+
forEachCluster(fn: (cluster: InitializedCluster) => Promise<void> | void): Promise<void>;
|
|
38
47
|
remove(cluster: string | InitializedCluster, destroy?: boolean): Promise<void>;
|
|
39
48
|
abstract destroy(): Promise<void>;
|
|
40
49
|
}
|
package/src/ClusterManager.js
CHANGED
|
@@ -1,25 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ClusterManager = void 0;
|
|
4
|
-
const
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
class ClusterManager {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
6
|
+
clusters = [];
|
|
7
|
+
constructor() { }
|
|
9
8
|
init(cluster) {
|
|
10
|
-
const initializedCluster =
|
|
9
|
+
const initializedCluster = {
|
|
10
|
+
...cluster,
|
|
11
|
+
id: (0, node_crypto_1.randomUUID)(),
|
|
12
|
+
};
|
|
11
13
|
this.clusters.push(initializedCluster);
|
|
12
14
|
return initializedCluster;
|
|
13
15
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Applies the given callback to every registered cluster. Each cluster
|
|
18
|
+
* is handled independently: a callback that throws (synchronously or
|
|
19
|
+
* asynchronously) for one cluster never prevents the remaining clusters
|
|
20
|
+
* from being processed.
|
|
21
|
+
*
|
|
22
|
+
* @param {(cluster: InitializedCluster) => Promise<void> | void} fn
|
|
23
|
+
* @returns {Promise<void>}
|
|
24
|
+
*/
|
|
25
|
+
async forEachCluster(fn) {
|
|
26
|
+
await Promise.allSettled(this.clusters.map(async (cluster) => fn(cluster)));
|
|
16
27
|
}
|
|
17
28
|
async remove(cluster, destroy = true) {
|
|
18
29
|
const id = typeof cluster === 'string' ? cluster : cluster.id;
|
|
19
30
|
this.clusters = this.clusters.filter(cluster => cluster.id !== id);
|
|
20
|
-
if (this.clusters.length === 0
|
|
21
|
-
&&
|
|
22
|
-
|
|
31
|
+
if (this.clusters.length === 0 &&
|
|
32
|
+
destroy &&
|
|
33
|
+
typeof this.destroy === 'function') {
|
|
23
34
|
await this.destroy();
|
|
24
35
|
}
|
|
25
36
|
}
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
* purchase a proprietary commercial license. Please contact us at
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
|
-
import { EventEmitter } from 'events';
|
|
24
|
+
import { EventEmitter } from 'node:events';
|
|
25
25
|
import { EventMap, ILogger, IMessageQueue, IMessageQueueConnection, IMQMode, IMQOptions, IServerInput, JsonObject, RedisQueue } from '.';
|
|
26
26
|
interface ClusterServer extends IMessageQueueConnection {
|
|
27
27
|
imq?: RedisQueue;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Class ClusteredRedisQueue
|
|
31
|
-
|
|
31
|
+
Implements the possibility to scale queues horizontally between several
|
|
32
32
|
* redis instances.
|
|
33
33
|
*/
|
|
34
34
|
export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<EventMap> {
|
|
@@ -70,6 +70,10 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
70
70
|
* @type {number}
|
|
71
71
|
*/
|
|
72
72
|
private currentQueue;
|
|
73
|
+
/**
|
|
74
|
+
* Time (ms) send() waits for the first server when the cluster is empty
|
|
75
|
+
*/
|
|
76
|
+
private readonly sendInitTimeout;
|
|
73
77
|
/**
|
|
74
78
|
* Total length of RedisQueue instances
|
|
75
79
|
*
|
|
@@ -95,12 +99,11 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
95
99
|
/**
|
|
96
100
|
* Class constructor
|
|
97
101
|
*
|
|
98
|
-
* @constructor
|
|
99
102
|
* @param {string} name
|
|
100
103
|
* @param {Partial<IMQOptions>} options
|
|
101
|
-
* @param {IMQMode} [
|
|
104
|
+
* @param {IMQMode} [_mode]
|
|
102
105
|
*/
|
|
103
|
-
constructor(name: string, options?: Partial<IMQOptions>,
|
|
106
|
+
constructor(name: string, options?: Partial<IMQOptions>, _mode?: IMQMode);
|
|
104
107
|
/**
|
|
105
108
|
* Starts the messaging queue.
|
|
106
109
|
* Supposed to be an async function.
|
|
@@ -116,18 +119,35 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
116
119
|
*/
|
|
117
120
|
stop(): Promise<ClusteredRedisQueue>;
|
|
118
121
|
/**
|
|
119
|
-
* Sends a message to given queue name with the given data.
|
|
122
|
+
* Sends a message to a given queue name with the given data.
|
|
120
123
|
* Supposed to be an async function.
|
|
121
124
|
*
|
|
122
|
-
* @param {string} toQueue - queue name to which message should be sent to
|
|
125
|
+
* @param {string} toQueue - queue name to which a message should be sent to
|
|
123
126
|
* @param {JsonObject} message - message data
|
|
124
127
|
* @param {number} [delay] - if specified, a message will be handled in the
|
|
125
|
-
|
|
128
|
+
target queue after a specified period of time in milliseconds.
|
|
126
129
|
* @param {(err: Error) => void} [errorHandler] - callback called only when
|
|
127
|
-
|
|
130
|
+
internal error occurs during message send execution.
|
|
128
131
|
* @returns {Promise<string>} - message identifier
|
|
129
132
|
*/
|
|
130
133
|
send(toQueue: string, message: JsonObject, delay?: number, errorHandler?: (err: Error) => void): Promise<string>;
|
|
134
|
+
/**
|
|
135
|
+
* Picks the next queue for a round-robin send, preferring an instance
|
|
136
|
+
* whose redis connection is currently ready so messages are not routed
|
|
137
|
+
* to a host that is known to be down. Falls back to the plain
|
|
138
|
+
* round-robin pick when no instance reports are ready.
|
|
139
|
+
*
|
|
140
|
+
* @returns {RedisQueue}
|
|
141
|
+
*/
|
|
142
|
+
private selectQueue;
|
|
143
|
+
/**
|
|
144
|
+
* Sends a message once the first cluster server becomes available.
|
|
145
|
+
* Rejects (rather than hanging forever) if none appears within the
|
|
146
|
+
* configured timeout and propagates any sent failure.
|
|
147
|
+
*
|
|
148
|
+
* @returns {Promise<string>}
|
|
149
|
+
*/
|
|
150
|
+
private sendWhenInitialized;
|
|
131
151
|
/**
|
|
132
152
|
* Safely destroys the current queue, unregistered all set event
|
|
133
153
|
* listeners and connections.
|
|
@@ -148,12 +168,23 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
148
168
|
/**
|
|
149
169
|
* Batch imq action processing on all registered imqs at once
|
|
150
170
|
*
|
|
151
|
-
* @access private
|
|
152
171
|
* @param {string} action
|
|
153
172
|
* @param {string} message
|
|
154
|
-
* @
|
|
173
|
+
* @returns {Promise<this>}
|
|
155
174
|
*/
|
|
156
175
|
private batch;
|
|
176
|
+
/**
|
|
177
|
+
* Applies the named EventEmitter method to every underlying emitter,
|
|
178
|
+
* forwarding the call across the whole cluster. Dispatch is reflective
|
|
179
|
+
* (method chosen by name), so a single contained cast bridges the dynamic
|
|
180
|
+
* call while the public method signatures below stay fully typed.
|
|
181
|
+
*
|
|
182
|
+
* @template K - EventEmitter method name
|
|
183
|
+
* @param {K} method - name of the EventEmitter method to invoke
|
|
184
|
+
* @param {any[]} args - arguments to pass to the method
|
|
185
|
+
* @returns {unknown[]} - results from each emitter call
|
|
186
|
+
*/
|
|
187
|
+
private applyToEmitters;
|
|
157
188
|
on(...args: any[]): this;
|
|
158
189
|
off(...args: any[]): this;
|
|
159
190
|
once(...args: any[]): this;
|
|
@@ -167,10 +198,10 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
167
198
|
rawListeners(...args: any[]): any[];
|
|
168
199
|
getMaxListeners(): number;
|
|
169
200
|
emit(...args: any[]): boolean;
|
|
170
|
-
eventNames(
|
|
171
|
-
listenerCount(...args: any[]):
|
|
201
|
+
eventNames(): (keyof EventMap)[];
|
|
202
|
+
listenerCount(...args: any[]): number;
|
|
172
203
|
publish(data: JsonObject, toName?: string): Promise<void>;
|
|
173
|
-
subscribe(channel: string, handler: (data: JsonObject) =>
|
|
204
|
+
subscribe(channel: string, handler: (data: JsonObject) => void): Promise<void>;
|
|
174
205
|
unsubscribe(): Promise<void>;
|
|
175
206
|
/**
|
|
176
207
|
* Adds new servers to the cluster
|