@imqueue/core 2.0.2 → 2.0.4
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 +1 -1
- package/debug.d.ts +1 -0
- package/debug_getter.d.ts +9 -0
- package/debug_listener.d.ts +1 -0
- package/package.json +1 -1
- package/src/ClusterManager.d.ts +7 -2
- package/src/ClusterManager.js +13 -1
- package/src/ClusteredRedisQueue.d.ts +2 -1
- package/src/ClusteredRedisQueue.js +45 -3
- package/src/RedisQueue.js +1 -0
- package/src/UDPClusterManager.d.ts +14 -1
- package/src/UDPClusterManager.js +64 -17
- package/src/profile.js +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# I Message Queue (@imqueue/core)
|
|
2
2
|
|
|
3
|
-
[](https://github.com/imqueue/core)
|
|
4
4
|
[](https://codebeat.co/projects/github-com-imqueue-core-master)
|
|
5
5
|
[](https://coveralls.io/github/imqueue/core?branch=master)
|
|
6
6
|
[](https://snyk.io/test/github/imqueue/core?targetFile=package.json)
|
package/debug.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
package/src/ClusterManager.d.ts
CHANGED
|
@@ -27,8 +27,13 @@ export interface ICluster {
|
|
|
27
27
|
remove: (server: IServerInput) => void;
|
|
28
28
|
find: <T extends IMessageQueueConnection>(server: IServerInput) => T | undefined;
|
|
29
29
|
}
|
|
30
|
+
export interface InitializedCluster extends ICluster {
|
|
31
|
+
id: string;
|
|
32
|
+
}
|
|
30
33
|
export declare abstract class ClusterManager {
|
|
31
|
-
protected clusters:
|
|
34
|
+
protected clusters: InitializedCluster[];
|
|
32
35
|
protected constructor();
|
|
33
|
-
init(cluster: ICluster):
|
|
36
|
+
init(cluster: ICluster): InitializedCluster;
|
|
37
|
+
remove(cluster: string | InitializedCluster, destroy?: boolean): Promise<void>;
|
|
38
|
+
abstract destroy(): Promise<void>;
|
|
34
39
|
}
|
package/src/ClusterManager.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ClusterManager = void 0;
|
|
4
|
+
const uuid_1 = require("./uuid");
|
|
4
5
|
class ClusterManager {
|
|
5
6
|
constructor() {
|
|
6
7
|
this.clusters = [];
|
|
7
8
|
}
|
|
8
9
|
init(cluster) {
|
|
9
|
-
|
|
10
|
+
const initializedCluster = Object.assign(cluster, { id: (0, uuid_1.uuid)() });
|
|
11
|
+
this.clusters.push(initializedCluster);
|
|
12
|
+
return initializedCluster;
|
|
13
|
+
}
|
|
14
|
+
async remove(cluster, destroy = true) {
|
|
15
|
+
const id = typeof cluster === 'string' ? cluster : cluster.id;
|
|
16
|
+
this.clusters = this.clusters.filter(cluster => cluster.id !== id);
|
|
17
|
+
if (this.clusters.length === 0
|
|
18
|
+
&& destroy
|
|
19
|
+
&& typeof this.destroy === 'function') {
|
|
20
|
+
await this.destroy();
|
|
21
|
+
}
|
|
10
22
|
}
|
|
11
23
|
}
|
|
12
24
|
exports.ClusterManager = ClusterManager;
|
|
@@ -88,6 +88,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
88
88
|
*/
|
|
89
89
|
private readonly clusterEmitter;
|
|
90
90
|
private state;
|
|
91
|
+
private initializedClusters;
|
|
91
92
|
/**
|
|
92
93
|
* Class constructor
|
|
93
94
|
*
|
|
@@ -125,7 +126,7 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
125
126
|
*/
|
|
126
127
|
send(toQueue: string, message: JsonObject, delay?: number, errorHandler?: (err: Error) => void): Promise<string>;
|
|
127
128
|
/**
|
|
128
|
-
* Safely destroys current queue, unregistered all set event
|
|
129
|
+
* Safely destroys the current queue, unregistered all set event
|
|
129
130
|
* listeners and connections.
|
|
130
131
|
* Supposed to be an async function.
|
|
131
132
|
*
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
8
|
+
};
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.ClusteredRedisQueue = void 0;
|
|
4
11
|
/*!
|
|
@@ -73,6 +80,7 @@ class ClusteredRedisQueue {
|
|
|
73
80
|
started: false,
|
|
74
81
|
subscription: null,
|
|
75
82
|
};
|
|
83
|
+
this.initializedClusters = [];
|
|
76
84
|
this.templateEmitter = new events_1.EventEmitter();
|
|
77
85
|
this.clusterEmitter = new events_1.EventEmitter();
|
|
78
86
|
this.options = (0, _1.buildOptions)(_1.DEFAULT_IMQ_OPTIONS, options);
|
|
@@ -90,11 +98,11 @@ class ClusteredRedisQueue {
|
|
|
90
98
|
}
|
|
91
99
|
if ((_b = this.options.clusterManagers) === null || _b === void 0 ? void 0 : _b.length) {
|
|
92
100
|
for (const manager of this.options.clusterManagers) {
|
|
93
|
-
manager.init({
|
|
101
|
+
this.initializedClusters.push(manager.init({
|
|
94
102
|
add: this.addServer.bind(this),
|
|
95
103
|
remove: this.removeServer.bind(this),
|
|
96
104
|
find: this.findServer.bind(this),
|
|
97
|
-
});
|
|
105
|
+
}));
|
|
98
106
|
}
|
|
99
107
|
}
|
|
100
108
|
}
|
|
@@ -145,15 +153,49 @@ class ClusteredRedisQueue {
|
|
|
145
153
|
return id;
|
|
146
154
|
}
|
|
147
155
|
/**
|
|
148
|
-
* Safely destroys current queue, unregistered all set event
|
|
156
|
+
* Safely destroys the current queue, unregistered all set event
|
|
149
157
|
* listeners and connections.
|
|
150
158
|
* Supposed to be an async function.
|
|
151
159
|
*
|
|
152
160
|
* @returns {Promise<void>}
|
|
153
161
|
*/
|
|
154
162
|
async destroy() {
|
|
163
|
+
var _a, e_1, _b, _c, _d, e_2, _e, _f;
|
|
164
|
+
var _g;
|
|
155
165
|
this.state.started = false;
|
|
156
166
|
await this.batch('destroy', 'Destroying clustered redis message queue...');
|
|
167
|
+
if (!((_g = this.options.clusterManagers) === null || _g === void 0 ? void 0 : _g.length)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
for (var _h = true, _j = __asyncValues(this.options.clusterManagers), _k; _k = await _j.next(), _a = _k.done, !_a; _h = true) {
|
|
172
|
+
_c = _k.value;
|
|
173
|
+
_h = false;
|
|
174
|
+
const manager = _c;
|
|
175
|
+
try {
|
|
176
|
+
for (var _l = true, _m = (e_2 = void 0, __asyncValues(this.initializedClusters)), _o; _o = await _m.next(), _d = _o.done, !_d; _l = true) {
|
|
177
|
+
_f = _o.value;
|
|
178
|
+
_l = false;
|
|
179
|
+
const cluster = _f;
|
|
180
|
+
await manager.remove(cluster);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
184
|
+
finally {
|
|
185
|
+
try {
|
|
186
|
+
if (!_l && !_d && (_e = _m.return)) await _e.call(_m);
|
|
187
|
+
}
|
|
188
|
+
finally { if (e_2) throw e_2.error; }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
193
|
+
finally {
|
|
194
|
+
try {
|
|
195
|
+
if (!_h && !_a && (_b = _j.return)) await _b.call(_j);
|
|
196
|
+
}
|
|
197
|
+
finally { if (e_1) throw e_1.error; }
|
|
198
|
+
}
|
|
157
199
|
}
|
|
158
200
|
// noinspection JSUnusedGlobalSymbols
|
|
159
201
|
/**
|
package/src/RedisQueue.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ICluster, ClusterManager } from './ClusterManager';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: {
|
|
3
3
|
broadcastPort: number;
|
|
4
4
|
broadcastAddress: string;
|
|
5
5
|
aliveTimeoutCorrection: number;
|
|
@@ -70,7 +70,11 @@ export interface UDPClusterManagerOptions {
|
|
|
70
70
|
export declare class UDPClusterManager extends ClusterManager {
|
|
71
71
|
private static sockets;
|
|
72
72
|
private readonly options;
|
|
73
|
+
private socketKey;
|
|
74
|
+
private get socket();
|
|
75
|
+
private set socket(value);
|
|
73
76
|
constructor(options?: UDPClusterManagerOptions);
|
|
77
|
+
private static free;
|
|
74
78
|
private listenBroadcastedMessages;
|
|
75
79
|
private startListening;
|
|
76
80
|
private static verifyHosts;
|
|
@@ -78,5 +82,14 @@ export declare class UDPClusterManager extends ClusterManager {
|
|
|
78
82
|
private static processBroadcastedMessage;
|
|
79
83
|
private static parseBroadcastedMessage;
|
|
80
84
|
private static serverAliveWait;
|
|
85
|
+
/**
|
|
86
|
+
* Destroys the UDPClusterManager by closing all opened network connections
|
|
87
|
+
* and safely destroying all blocking sockets
|
|
88
|
+
*
|
|
89
|
+
* @returns {Promise<void>}
|
|
90
|
+
* @throws {Error}
|
|
91
|
+
*/
|
|
92
|
+
destroy(): Promise<void>;
|
|
93
|
+
private static destroySocket;
|
|
81
94
|
private static selectNetworkInterface;
|
|
82
95
|
}
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UDPClusterManager = exports.
|
|
3
|
+
exports.UDPClusterManager = exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = void 0;
|
|
4
4
|
const ClusterManager_1 = require("./ClusterManager");
|
|
5
5
|
const dgram_1 = require("dgram");
|
|
6
6
|
const os_1 = require("os");
|
|
7
|
-
var
|
|
8
|
-
(function (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})(
|
|
12
|
-
exports.
|
|
7
|
+
var MessageType;
|
|
8
|
+
(function (MessageType) {
|
|
9
|
+
MessageType["Up"] = "up";
|
|
10
|
+
MessageType["Down"] = "down";
|
|
11
|
+
})(MessageType || (MessageType = {}));
|
|
12
|
+
exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
|
|
13
13
|
broadcastPort: 63000,
|
|
14
14
|
broadcastAddress: '255.255.255.255',
|
|
15
15
|
aliveTimeoutCorrection: 1000,
|
|
@@ -30,20 +30,32 @@ const LOCALHOST_ADDRESSES = [
|
|
|
30
30
|
* ~~~
|
|
31
31
|
*/
|
|
32
32
|
class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
33
|
+
get socket() {
|
|
34
|
+
return UDPClusterManager.sockets[this.socketKey];
|
|
35
|
+
}
|
|
36
|
+
set socket(socket) {
|
|
37
|
+
UDPClusterManager.sockets[this.socketKey] = socket;
|
|
38
|
+
}
|
|
33
39
|
constructor(options) {
|
|
34
40
|
super();
|
|
35
|
-
this.options = Object.assign(Object.assign({}, exports.
|
|
41
|
+
this.options = Object.assign(Object.assign({}, exports.DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS), options || {});
|
|
36
42
|
this.startListening(this.options);
|
|
43
|
+
process.on('SIGTERM', UDPClusterManager.free);
|
|
44
|
+
process.on('SIGINT', UDPClusterManager.free);
|
|
45
|
+
process.on('SIGABRT', UDPClusterManager.free);
|
|
46
|
+
}
|
|
47
|
+
static async free() {
|
|
48
|
+
const socketKeys = Object.keys(UDPClusterManager.sockets);
|
|
49
|
+
await Promise.all(socketKeys.map(socketKey => UDPClusterManager.destroySocket(socketKey, UDPClusterManager.sockets[socketKey])));
|
|
37
50
|
}
|
|
38
51
|
listenBroadcastedMessages(listener, options) {
|
|
39
52
|
const address = UDPClusterManager.selectNetworkInterface(options);
|
|
40
|
-
|
|
41
|
-
if (!
|
|
42
|
-
|
|
43
|
-
socket.bind(options.broadcastPort, address);
|
|
44
|
-
UDPClusterManager.sockets[key] = socket;
|
|
53
|
+
this.socketKey = `${address}:${options.broadcastPort}`;
|
|
54
|
+
if (!this.socket) {
|
|
55
|
+
this.socket = (0, dgram_1.createSocket)({ type: 'udp4', reuseAddr: true });
|
|
56
|
+
this.socket.bind(options.broadcastPort, address);
|
|
45
57
|
}
|
|
46
|
-
|
|
58
|
+
this.socket.on('message', message => listener(UDPClusterManager.parseBroadcastedMessage(message)));
|
|
47
59
|
}
|
|
48
60
|
startListening(options = {}) {
|
|
49
61
|
this.listenBroadcastedMessages(UDPClusterManager.processBroadcastedMessage(this), options);
|
|
@@ -56,11 +68,11 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
56
68
|
}
|
|
57
69
|
static processMessageOnCluster(cluster, message, aliveTimeoutCorrection) {
|
|
58
70
|
const server = cluster.find(message);
|
|
59
|
-
if (server && message.type ===
|
|
71
|
+
if (server && message.type === MessageType.Down) {
|
|
60
72
|
clearTimeout(server.timer);
|
|
61
73
|
return cluster.remove(message);
|
|
62
74
|
}
|
|
63
|
-
if (!server && message.type ===
|
|
75
|
+
if (!server && message.type === MessageType.Up) {
|
|
64
76
|
cluster.add(message);
|
|
65
77
|
const added = cluster.find(message);
|
|
66
78
|
if (added) {
|
|
@@ -68,7 +80,7 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
68
80
|
}
|
|
69
81
|
return;
|
|
70
82
|
}
|
|
71
|
-
if (server && message.type ===
|
|
83
|
+
if (server && message.type === MessageType.Up) {
|
|
72
84
|
return UDPClusterManager.serverAliveWait(cluster, server, aliveTimeoutCorrection, message);
|
|
73
85
|
}
|
|
74
86
|
}
|
|
@@ -121,6 +133,41 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
121
133
|
}
|
|
122
134
|
}, timeout);
|
|
123
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Destroys the UDPClusterManager by closing all opened network connections
|
|
138
|
+
* and safely destroying all blocking sockets
|
|
139
|
+
*
|
|
140
|
+
* @returns {Promise<void>}
|
|
141
|
+
* @throws {Error}
|
|
142
|
+
*/
|
|
143
|
+
async destroy() {
|
|
144
|
+
await UDPClusterManager.destroySocket(this.socketKey, this.socket);
|
|
145
|
+
}
|
|
146
|
+
static async destroySocket(socketKey, socket) {
|
|
147
|
+
if (!socket) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
return await new Promise((resolve, reject) => {
|
|
151
|
+
try {
|
|
152
|
+
if (typeof socket.close === 'function') {
|
|
153
|
+
socket.removeAllListeners();
|
|
154
|
+
socket.close(() => {
|
|
155
|
+
socket === null || socket === void 0 ? void 0 : socket.unref();
|
|
156
|
+
if (socketKey
|
|
157
|
+
&& UDPClusterManager.sockets[socketKey]) {
|
|
158
|
+
delete UDPClusterManager.sockets[socketKey];
|
|
159
|
+
}
|
|
160
|
+
resolve();
|
|
161
|
+
});
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
resolve();
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
reject(e);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
124
171
|
static selectNetworkInterface(options) {
|
|
125
172
|
const interfaces = (0, os_1.networkInterfaces)();
|
|
126
173
|
const limitedBroadcastAddress = options.limitedBroadcastAddress;
|
package/src/profile.js
CHANGED
|
@@ -98,7 +98,7 @@ function logDebugInfo({ debugTime, debugArgs, className, args, methodName, start
|
|
|
98
98
|
? logger[logLevel].bind(logger) : undefined;
|
|
99
99
|
if (debugTime) {
|
|
100
100
|
// noinspection TypeScriptUnresolvedFunction
|
|
101
|
-
const time = parseInt((process.hrtime.bigint() - start), 10) / 1000;
|
|
101
|
+
const time = parseInt((process.hrtime.bigint() - BigInt(start)), 10) / 1000;
|
|
102
102
|
let timeStr;
|
|
103
103
|
// istanbul ignore next
|
|
104
104
|
switch (exports.IMQ_LOG_TIME_FORMAT) {
|