cloudcms-server 0.9.256 → 0.9.261
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/broadcast/providers/redis.js +24 -49
- package/clients/nrp.js +117 -0
- package/clients/redis.js +64 -0
- package/launchpad/index.js +161 -11
- package/launchpad/launchers/cluster.js +87 -109
- package/launchpad/launchers/redis.js +53 -0
- package/launchpad/launchers/single.js +30 -22
- package/locks/locks.js +4 -3
- package/locks/providers/cluster.js +1 -1
- package/locks/providers/memory.js +1 -1
- package/locks/providers/redis.js +62 -61
- package/middleware/admin/admin.js +2 -2
- package/middleware/authentication/providers/saml.js +2 -1
- package/middleware/awareness/awareness.js +12 -2
- package/middleware/awareness/providers/redis.js +224 -179
- package/middleware/cache/providers/redis.js +125 -68
- package/middleware/cloudcms/cloudcms.js +5 -1
- package/middleware/proxy/proxy.js +2 -1
- package/middleware/stores/stores.js +2 -2
- package/middleware/virtual-config/virtual-config.js +4 -4
- package/middleware/wcm/wcm.js +4 -4
- package/package.json +11 -7
- package/server/index.js +622 -591
- package/temp/clusterlock/index.js +2 -0
- package/temp/passport-saml/LICENSE +23 -0
- package/temp/passport-saml/README.md +406 -0
- package/temp/passport-saml/lib/node-saml/algorithms.d.ts +5 -0
- package/temp/passport-saml/lib/node-saml/algorithms.js +41 -0
- package/temp/passport-saml/lib/node-saml/algorithms.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/index.d.ts +3 -0
- package/temp/passport-saml/lib/node-saml/index.js +6 -0
- package/temp/passport-saml/lib/node-saml/index.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.d.ts +45 -0
- package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.js +86 -0
- package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/saml-post-signing.d.ts +3 -0
- package/temp/passport-saml/lib/node-saml/saml-post-signing.js +15 -0
- package/temp/passport-saml/lib/node-saml/saml-post-signing.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/saml.d.ts +77 -0
- package/temp/passport-saml/lib/node-saml/saml.js +1170 -0
- package/temp/passport-saml/lib/node-saml/saml.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/types.d.ts +95 -0
- package/temp/passport-saml/lib/node-saml/types.js +8 -0
- package/temp/passport-saml/lib/node-saml/types.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/utility.d.ts +3 -0
- package/temp/passport-saml/lib/node-saml/utility.js +19 -0
- package/temp/passport-saml/lib/node-saml/utility.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/xml.d.ts +21 -0
- package/temp/passport-saml/lib/node-saml/xml.js +140 -0
- package/temp/passport-saml/lib/node-saml/xml.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/index.d.ts +6 -0
- package/temp/passport-saml/lib/passport-saml/index.js +11 -0
- package/temp/passport-saml/lib/passport-saml/index.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.d.ts +13 -0
- package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.js +63 -0
- package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/strategy.d.ts +20 -0
- package/temp/passport-saml/lib/passport-saml/strategy.js +167 -0
- package/temp/passport-saml/lib/passport-saml/strategy.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/types.d.ts +51 -0
- package/temp/passport-saml/lib/passport-saml/types.js +11 -0
- package/temp/passport-saml/lib/passport-saml/types.js.map +1 -0
- package/temp/passport-saml/package.json +96 -0
- package/util/auth.js +1 -1
- package/util/cloudcms.js +6 -6
- package/util/proxy-factory.js +22 -7
- package/util/redis.js +51 -1
- package/util/util.js +1 -1
- package/launchpad/launchers/sticky-cluster.js +0 -43
package/README.md
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
var
|
|
1
|
+
var redisClientFactory = require("../../clients/redis");
|
|
2
|
+
var redisHelper = require("../../util/redis");
|
|
2
3
|
|
|
3
|
-
var NRP = require(
|
|
4
|
-
|
|
5
|
-
var logFactory = require("../../util/logger");
|
|
4
|
+
var NRP = require("../../clients/nrp");
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Redis broadcast provider.
|
|
@@ -12,55 +11,31 @@ var logFactory = require("../../util/logger");
|
|
|
12
11
|
exports = module.exports = function(broadcastConfig)
|
|
13
12
|
{
|
|
14
13
|
var nrp = null;
|
|
15
|
-
|
|
16
|
-
var logger =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// allow for global redis default
|
|
20
|
-
// allow for redis broadcast specific
|
|
21
|
-
// otherwise default to "error"
|
|
22
|
-
if (typeof(process.env.CLOUDCMS_REDIS_DEBUG_LEVEL) !== "undefined") {
|
|
23
|
-
logger.setLevel(("" + process.env.CLOUDCMS_REDIS_DEBUG_LEVEL).toLowerCase(), true);
|
|
24
|
-
}
|
|
25
|
-
else if (typeof(process.env.CLOUDCMS_BROADCAST_REDIS_DEBUG_LEVEL) !== "undefined") {
|
|
26
|
-
logger.setLevel(("" + process.env.CLOUDCMS_BROADCAST_REDIS_DEBUG_LEVEL).toLowerCase(), true);
|
|
27
|
-
}
|
|
28
|
-
|
|
14
|
+
|
|
15
|
+
var logger = redisHelper.redisLogger("REDIS_BROADCAST", "CLOUDCMS_BROADCAST_", "error")
|
|
16
|
+
|
|
29
17
|
var r = {};
|
|
30
18
|
|
|
31
19
|
r.start = function(callback)
|
|
32
20
|
{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
var nrpConfig = {
|
|
54
|
-
"port": redisPort,
|
|
55
|
-
"host": redisEndpoint,
|
|
56
|
-
"scope": "broadcast_cache"
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
logger.info("using config = " + nrpConfig);
|
|
60
|
-
|
|
61
|
-
nrp = new NRP(nrpConfig);
|
|
62
|
-
|
|
63
|
-
callback();
|
|
21
|
+
redisClientFactory.create(broadcastConfig, function(err, client) {
|
|
22
|
+
|
|
23
|
+
if (err) {
|
|
24
|
+
return callback(err);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var nrpConfig = {
|
|
28
|
+
"client": client,
|
|
29
|
+
"scope": "broadcast_cache"
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//logger.info("using config = " + nrpConfig);
|
|
33
|
+
|
|
34
|
+
nrp = new NRP(nrpConfig);
|
|
35
|
+
nrp.connect(function(err) {
|
|
36
|
+
callback(err);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
64
39
|
};
|
|
65
40
|
|
|
66
41
|
r.publish = function(topic, message, callback)
|
package/clients/nrp.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// a revision of
|
|
2
|
+
// https://raw.githubusercontent.com/louischatriot/node-redis-pubsub/master/lib/node-redis-pubsub.js
|
|
3
|
+
// that works with Redis 6+
|
|
4
|
+
|
|
5
|
+
"use strict";
|
|
6
|
+
var redis = require('redis');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Create a new NodeRedisPubsub instance that can subscribe to channels and publish messages
|
|
10
|
+
* @param {Object} options Options for the client creations:
|
|
11
|
+
* client - a connected Redis client
|
|
12
|
+
* scope - Optional, two NodeRedisPubsubs with different scopes will not share messages
|
|
13
|
+
*/
|
|
14
|
+
function NodeRedisPubsub(options)
|
|
15
|
+
{
|
|
16
|
+
if (!(this instanceof NodeRedisPubsub)){ return new NodeRedisPubsub(options); }
|
|
17
|
+
|
|
18
|
+
options || (options = {});
|
|
19
|
+
|
|
20
|
+
this.emitter = options.client.duplicate();
|
|
21
|
+
this.emitter.setMaxListeners(0);
|
|
22
|
+
this.receiver = options.client.duplicate();
|
|
23
|
+
this.receiver.setMaxListeners(0);
|
|
24
|
+
|
|
25
|
+
this.prefix = options.scope ? options.scope + ':' : '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
NodeRedisPubsub.prototype.connect = function(callback)
|
|
29
|
+
{
|
|
30
|
+
var self = this;
|
|
31
|
+
|
|
32
|
+
(async function() {
|
|
33
|
+
await self.emitter.connect();
|
|
34
|
+
await self.receiver.connect();
|
|
35
|
+
callback();
|
|
36
|
+
})();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Subscribe to a channel
|
|
41
|
+
* @param {String} channel The channel to subscribe to, can be a pattern e.g. 'user.*'
|
|
42
|
+
* @param {Function} handler Function to call with the received message.
|
|
43
|
+
* @param {Function} cb Optional callback to call once the handler is registered.
|
|
44
|
+
*/
|
|
45
|
+
NodeRedisPubsub.prototype.on = NodeRedisPubsub.prototype.subscribe = function(channel, handler, callback)
|
|
46
|
+
{
|
|
47
|
+
if (!callback)
|
|
48
|
+
{
|
|
49
|
+
callback = function(){};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
var self = this;
|
|
53
|
+
|
|
54
|
+
if (channel === "error")
|
|
55
|
+
{
|
|
56
|
+
self.errorHandler = handler;
|
|
57
|
+
self.emitter.on("error", handler);
|
|
58
|
+
self.receiver.on("error", handler);
|
|
59
|
+
return callback();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var listener = function(self, handler)
|
|
63
|
+
{
|
|
64
|
+
return function(message, channel) {
|
|
65
|
+
|
|
66
|
+
var jsonmsg = message;
|
|
67
|
+
try{
|
|
68
|
+
jsonmsg = JSON.parse(message);
|
|
69
|
+
} catch (ex){
|
|
70
|
+
if(typeof self.errorHandler === 'function'){
|
|
71
|
+
return self.errorHandler("Invalid JSON received! Channel: " + self.prefix + channel + " Message: " + message);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return handler(jsonmsg, channel);
|
|
75
|
+
}
|
|
76
|
+
}(self, handler);
|
|
77
|
+
|
|
78
|
+
(async function() {
|
|
79
|
+
await self.receiver.pSubscribe(self.prefix + channel, listener);
|
|
80
|
+
})();
|
|
81
|
+
|
|
82
|
+
callback();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Emit an event
|
|
87
|
+
* @param {String} channel Channel on which to emit the message
|
|
88
|
+
* @param {Object} message
|
|
89
|
+
*/
|
|
90
|
+
NodeRedisPubsub.prototype.emit = NodeRedisPubsub.prototype.publish = function (channel, message)
|
|
91
|
+
{
|
|
92
|
+
var self = this;
|
|
93
|
+
|
|
94
|
+
(async function() {
|
|
95
|
+
return await self.emitter.publish(self.prefix + channel, JSON.stringify(message));
|
|
96
|
+
})();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Safely close the redis connections 'soon'
|
|
101
|
+
*/
|
|
102
|
+
NodeRedisPubsub.prototype.quit = function()
|
|
103
|
+
{
|
|
104
|
+
this.emitter.quit();
|
|
105
|
+
this.receiver.quit();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Dangerously close the redis connections immediately
|
|
110
|
+
*/
|
|
111
|
+
NodeRedisPubsub.prototype.end = function()
|
|
112
|
+
{
|
|
113
|
+
this.emitter.end(true);
|
|
114
|
+
this.receiver.end(true);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
module.exports = NodeRedisPubsub;
|
package/clients/redis.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//var redis = require("redis");
|
|
2
|
+
var redisHelper = require("../util/redis");
|
|
3
|
+
|
|
4
|
+
var clients = {};
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Redis client factory.
|
|
8
|
+
*
|
|
9
|
+
* @type {*}
|
|
10
|
+
*/
|
|
11
|
+
exports = module.exports = {};
|
|
12
|
+
|
|
13
|
+
var create = exports.create = function(config, baseOptions, callback)
|
|
14
|
+
{
|
|
15
|
+
if (typeof(config) === "function") {
|
|
16
|
+
callback = config;
|
|
17
|
+
config = {};
|
|
18
|
+
baseOptions = {};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (typeof(baseOptions) === "function") {
|
|
22
|
+
callback = baseOptions;
|
|
23
|
+
baseOptions = {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!config) {
|
|
27
|
+
config = {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!baseOptions) {
|
|
31
|
+
baseOptions = {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var redisOptions = redisHelper.redisOptions(config);
|
|
35
|
+
if (baseOptions) {
|
|
36
|
+
for (var k in baseOptions) {
|
|
37
|
+
redisOptions[k] = baseOptions[k];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
var url = redisOptions.url;
|
|
42
|
+
|
|
43
|
+
// cached client?
|
|
44
|
+
var client = clients[url];
|
|
45
|
+
if (client) {
|
|
46
|
+
return callback(null, client);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// connect
|
|
50
|
+
(async function() {
|
|
51
|
+
await redisHelper.createAndConnect(redisOptions, function(err, client) {
|
|
52
|
+
|
|
53
|
+
if (err) {
|
|
54
|
+
return callback(err);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// cache it
|
|
58
|
+
clients[url] = client;
|
|
59
|
+
|
|
60
|
+
// return
|
|
61
|
+
return callback(null, client);
|
|
62
|
+
});
|
|
63
|
+
})();
|
|
64
|
+
};
|
package/launchpad/index.js
CHANGED
|
@@ -1,17 +1,167 @@
|
|
|
1
|
-
|
|
1
|
+
const cluster = require("cluster");
|
|
2
|
+
|
|
3
|
+
module.exports = function(type, config, options)
|
|
2
4
|
{
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
options.setup = process.env.CLOUDCMS_LAUNCHPAD_SETUP;
|
|
5
|
+
if (!type) {
|
|
6
|
+
type = process.env.CLOUDCMS_LAUNCHPAD_SETUP;
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
if (!type) {
|
|
9
|
+
type = "single";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
process.env.CLOUDCMS_LAUNCHPAD_SETUP = type
|
|
13
|
+
|
|
14
|
+
var launcherFactory = require("./launchers/" + type);
|
|
15
|
+
if (!launcherFactory)
|
|
16
|
+
{
|
|
17
|
+
throw new Error("Cannot find launcher factory: " + type);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var launcher = launcherFactory(config);
|
|
21
|
+
|
|
22
|
+
var reportFn = options.report;
|
|
23
|
+
if (!reportFn) {
|
|
24
|
+
options.report = reportFn = function () {};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var completionFn = options.complete;
|
|
28
|
+
if (!completionFn) {
|
|
29
|
+
options.complete = completionFn = function(err) {
|
|
30
|
+
throw new Error(err);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var fork = true;
|
|
35
|
+
if (type === "single") {
|
|
36
|
+
fork = false;
|
|
11
37
|
}
|
|
38
|
+
|
|
39
|
+
if (!fork)
|
|
40
|
+
{
|
|
41
|
+
return launchWorker(launcher, config, options, function(err, app, httpServer) {
|
|
42
|
+
|
|
43
|
+
if (err) {
|
|
44
|
+
return completionFn(config, err);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
var httpServerPort = -1;
|
|
48
|
+
// if (app) {
|
|
49
|
+
// httpServerPort = app.get("port");
|
|
50
|
+
// }
|
|
51
|
+
if (httpServerPort === -1) {
|
|
52
|
+
httpServerPort = process.env.PORT;
|
|
53
|
+
}
|
|
54
|
+
if (httpServerPort === -1) {
|
|
55
|
+
httpServerPort = 3000;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
httpServer.listen(httpServerPort);
|
|
59
|
+
|
|
60
|
+
reportFn(config);
|
|
61
|
+
completionFn(config);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
{
|
|
66
|
+
if (cluster.isMaster)
|
|
67
|
+
{
|
|
68
|
+
return launchMaster(launcher, config, options, function(err, workers) {
|
|
69
|
+
|
|
70
|
+
if (err) {
|
|
71
|
+
return completionFn(config, err);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//reportFn(config);
|
|
75
|
+
completionFn(config);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else
|
|
79
|
+
{
|
|
80
|
+
return launchWorker(launcher, config, options, function(err) {
|
|
81
|
+
completionFn(config, err);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
var launchMaster = function(launcher, config, options, done)
|
|
88
|
+
{
|
|
89
|
+
var createHttpServer = options.createHttpServer;
|
|
90
|
+
|
|
91
|
+
createHttpServer(null, function(err, httpServer) {
|
|
92
|
+
|
|
93
|
+
if (err) {
|
|
94
|
+
return done(err);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
launcher.startCluster(httpServer, function(err) {
|
|
98
|
+
|
|
99
|
+
if (err) {
|
|
100
|
+
return done(err);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
var httpServerPort = -1;
|
|
104
|
+
// if (app) {
|
|
105
|
+
// httpServerPort = app.get("port");
|
|
106
|
+
// }
|
|
107
|
+
if (httpServerPort === -1) {
|
|
108
|
+
httpServerPort = process.env.PORT;
|
|
109
|
+
}
|
|
110
|
+
if (httpServerPort === -1) {
|
|
111
|
+
httpServerPort = 3000;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
httpServer.listen(httpServerPort);
|
|
115
|
+
|
|
116
|
+
launcher.afterStartCluster(httpServer, function(err, workers) {
|
|
117
|
+
done(err, workers);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
};
|
|
12
122
|
|
|
13
|
-
|
|
123
|
+
var launchWorker = function(launcher, config, options, done)
|
|
124
|
+
{
|
|
125
|
+
var startServer = options.startServer;
|
|
126
|
+
|
|
127
|
+
var configureServer = options.configureServer;
|
|
128
|
+
if (!configureServer) {
|
|
129
|
+
options.configureServer = configureServer = function(config, app, httpServer, done) {
|
|
130
|
+
done();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
startServer(config, function(err, app, httpServer) {
|
|
135
|
+
|
|
136
|
+
if (err) {
|
|
137
|
+
return done(err);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
launcher.afterStartServer(app, httpServer, function(err) {
|
|
141
|
+
|
|
142
|
+
if (err) {
|
|
143
|
+
return done(err);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
configureServer(config, app, httpServer, function(err) {
|
|
147
|
+
|
|
148
|
+
if (err) {
|
|
149
|
+
return done(err);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// if we are on a worker process, then inform the master that we completed
|
|
153
|
+
if (process.send) {
|
|
154
|
+
process.send("worker-startup");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (process.env["CLUSTER_REPORT"])
|
|
158
|
+
{
|
|
159
|
+
var reportFn = options.report;
|
|
160
|
+
reportFn(config);
|
|
161
|
+
}
|
|
14
162
|
|
|
15
|
-
|
|
16
|
-
|
|
163
|
+
done(null, app, httpServer);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
17
167
|
};
|
|
@@ -1,121 +1,99 @@
|
|
|
1
|
-
var cluster = require("cluster");
|
|
2
|
-
var async = require("async");
|
|
3
|
-
|
|
4
1
|
var clusterlock = require("../../temp/clusterlock");
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
const cluster = require("cluster");
|
|
4
|
+
const { Server } = require("socket.io");
|
|
5
|
+
const numCPUs = require("os").cpus().length;
|
|
6
|
+
const { setupMaster, setupWorker } = require("@socket.io/sticky");
|
|
7
|
+
const { createAdapter, setupPrimary } = require("@socket.io/cluster-adapter");
|
|
8
|
+
|
|
9
|
+
module.exports = function(config) {
|
|
10
|
+
|
|
11
|
+
var r = {};
|
|
12
|
+
|
|
13
|
+
r.startCluster = function(httpServer, callback)
|
|
14
|
+
{
|
|
15
|
+
// setup sticky sessions
|
|
16
|
+
setupMaster(httpServer, {
|
|
17
|
+
loadBalancingMethod: "least-connection",
|
|
18
|
+
});
|
|
7
19
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// always take up the max # of CPU's
|
|
11
|
-
var cpuCount = require("os").cpus().length;
|
|
20
|
+
// setup connections between the workers
|
|
21
|
+
setupPrimary();
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
var completionCallback = options.complete;
|
|
19
|
-
if (!completionCallback) {
|
|
20
|
-
completionCallback = function() { };
|
|
21
|
-
}
|
|
23
|
+
// needed for packets containing buffers
|
|
24
|
+
cluster.setupPrimary({
|
|
25
|
+
serialization: "advanced"
|
|
26
|
+
});
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
return callback();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
r.afterStartCluster = function(httpServer, callback)
|
|
24
32
|
{
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
// start up cluster locks
|
|
34
|
+
clusterlock.setup();
|
|
35
|
+
|
|
36
|
+
var startupCount = 0;
|
|
37
|
+
|
|
38
|
+
var workers = [];
|
|
39
|
+
for (let i = 0; i < numCPUs; i++)
|
|
29
40
|
{
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
var spawn = function (i, afterSpawnFn) {
|
|
34
|
-
|
|
35
|
-
if (!afterSpawnFn) {
|
|
36
|
-
afterSpawnFn = function () { };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
workers[i] = cluster.fork();
|
|
40
|
-
|
|
41
|
-
workers[i].on('exit', function (worker, workers, i) {
|
|
42
|
-
return function() {
|
|
43
|
-
console.error("launchpad: worker " + i + " exited");
|
|
44
|
-
worker.exited = true;
|
|
45
|
-
|
|
46
|
-
// are all workers exited?
|
|
47
|
-
var all = true;
|
|
48
|
-
for (var z = 0; z < workers.length; z++)
|
|
49
|
-
{
|
|
50
|
-
if (!workers[z].exited) {
|
|
51
|
-
all = false;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (all)
|
|
56
|
-
{
|
|
57
|
-
console.log("launchpad: all workers exited, terminating process");
|
|
58
|
-
return process.exit(-1);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// set a timeout to otherwise restart worker in 5 seconds
|
|
62
|
-
setTimeout(function() {
|
|
63
|
-
console.log("launchpad: restarting worker: " + i);
|
|
64
|
-
spawn(i);
|
|
65
|
-
worker.exited = false;
|
|
66
|
-
}, 5000);
|
|
67
|
-
}
|
|
68
|
-
}(workers[i], workers, i));
|
|
69
|
-
|
|
70
|
-
workers[i].on('message', function (msg, c) {
|
|
71
|
-
|
|
72
|
-
if (msg === "server-startup")
|
|
73
|
-
{
|
|
74
|
-
afterSpawnFn();
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
spawn(i, function () {
|
|
79
|
-
done();
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
}(i, workers);
|
|
83
|
-
fns.push(fn);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async.parallel(fns, function (err) {
|
|
87
|
-
|
|
88
|
-
// start up cluster locks
|
|
89
|
-
clusterlock.setup();
|
|
90
|
-
|
|
91
|
-
// tell the first worker to report
|
|
92
|
-
if (workers.length > 0)
|
|
93
|
-
{
|
|
94
|
-
workers[0].send("server-report");
|
|
41
|
+
var workerEnv = {};
|
|
42
|
+
if (i === 0) {
|
|
43
|
+
workerEnv["CLUSTER_REPORT"] = true;
|
|
95
44
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
45
|
+
|
|
46
|
+
var worker = cluster.fork(workerEnv);
|
|
47
|
+
|
|
48
|
+
worker.on('message', function (msg, c) {
|
|
49
|
+
if (msg === "worker-startup") {
|
|
50
|
+
startupCount++;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
workers.push(worker);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
cluster.on("exit", (worker) => {
|
|
58
|
+
console.log(`Worker ${worker.process.pid} died`);
|
|
59
|
+
cluster.fork();
|
|
100
60
|
});
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// listen for the "server-report" message and fire the callback
|
|
111
|
-
process.on('message', function (msg, msgData) {
|
|
112
|
-
|
|
113
|
-
if (msg === "server-report")
|
|
61
|
+
|
|
62
|
+
// wait for workers to start
|
|
63
|
+
var waitFn = function() {
|
|
64
|
+
setTimeout(function() {
|
|
65
|
+
if (startupCount >= numCPUs) {
|
|
66
|
+
return callback(null, workers);
|
|
67
|
+
}
|
|
68
|
+
else
|
|
114
69
|
{
|
|
115
|
-
|
|
70
|
+
waitFn();
|
|
116
71
|
}
|
|
117
|
-
|
|
118
|
-
|
|
72
|
+
}, 25);
|
|
73
|
+
};
|
|
74
|
+
waitFn();
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
r.afterStartServer = function(app, httpServer, callback)
|
|
78
|
+
{
|
|
79
|
+
// worker
|
|
80
|
+
|
|
81
|
+
const io = new Server(httpServer);
|
|
82
|
+
|
|
83
|
+
// use the cluster adapter
|
|
84
|
+
io.adapter(createAdapter());
|
|
85
|
+
|
|
86
|
+
// setup connection with the primary process
|
|
87
|
+
setupWorker(io);
|
|
88
|
+
|
|
89
|
+
io.on("connection", (socket) => {
|
|
90
|
+
// TODO
|
|
119
91
|
});
|
|
120
|
-
|
|
121
|
-
|
|
92
|
+
|
|
93
|
+
httpServer.io = io;
|
|
94
|
+
|
|
95
|
+
return callback();
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return r;
|
|
99
|
+
}
|