-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsc-server.js
104 lines (90 loc) · 4.82 KB
/
sc-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
This is the SocketCluster master controller file.
It is responsible for bootstrapping the SocketCluster master process.
Be careful when modifying the options object below.
If you plan to run SCC on Kubernetes or another orchestrator at some point
in the future, avoid changing the environment variable names below as
each one has a specific meaning within the SC ecosystem.
*/
const path = require('path')
const argv = require('minimist')(process.argv.slice(2))
const scHotReboot = require('sc-hot-reboot')
const fsUtil = require('socketcluster/fsutil')
const waitForFile = fsUtil.waitForFile
const SocketCluster = require('socketcluster')
console.log('SETUP SOCKET CLUSTER sc-server.js with engine:', process.env.SOCKETCLUSTER_WS_ENGINE || 'ws')
const workerControllerPath = argv.wc || process.env.SOCKETCLUSTER_WORKER_CONTROLLER
const brokerControllerPath = argv.bc || process.env.SOCKETCLUSTER_BROKER_CONTROLLER
const workerClusterControllerPath = argv.wcc || process.env.SOCKETCLUSTER_WORKERCLUSTER_CONTROLLER
const environment = process.env.ENV || 'dev'
const options = {
workers: Number(argv.w) || Number(process.env.SOCKETCLUSTER_WORKERS) || 1,
brokers: Number(argv.b) || Number(process.env.SOCKETCLUSTER_BROKERS) || 1,
port: Number(argv.p) || Number(process.env.SOCKETCLUSTER_PORT) || 8000,
path: '/api',
// You can switch from 'ws' to 'sc-uws' for improved performance.
wsEngine: process.env.SOCKETCLUSTER_WS_ENGINE || 'ws',
appName: argv.n || process.env.SOCKETCLUSTER_APP_NAME || null,
workerController: workerControllerPath || path.join(__dirname, 'sc-worker.js'),
brokerController: brokerControllerPath || path.join(__dirname, 'sc-broker.js'),
workerClusterController: workerClusterControllerPath || null,
socketChannelLimit: Number(process.env.SOCKETCLUSTER_SOCKET_CHANNEL_LIMIT) || 1000,
clusterStateServerHost: argv.cssh || process.env.SCC_STATE_SERVER_HOST || null,
clusterStateServerPort: process.env.SCC_STATE_SERVER_PORT || null,
clusterMappingEngine: process.env.SCC_MAPPING_ENGINE || null,
clusterClientPoolSize: process.env.SCC_CLIENT_POOL_SIZE || null,
clusterAuthKey: process.env.SCC_AUTH_KEY || null,
clusterInstanceIp: process.env.SCC_INSTANCE_IP || null,
clusterInstanceIpFamily: process.env.SCC_INSTANCE_IP_FAMILY || null,
clusterStateServerConnectTimeout: Number(process.env.SCC_STATE_SERVER_CONNECT_TIMEOUT) || null,
clusterStateServerAckTimeout: Number(process.env.SCC_STATE_SERVER_ACK_TIMEOUT) || null,
clusterStateServerReconnectRandomness: Number(process.env.SCC_STATE_SERVER_RECONNECT_RANDOMNESS) || null,
crashWorkerOnError: argv['auto-reboot'] != false,
// If using nodemon, set this to true, and make sure that environment is 'dev'.
killMasterOnSignal: false,
environment: environment,
serverPayload: global.__SERVER_PAYLOAD__
}
const bootTimeout = Number(process.env.SOCKETCLUSTER_CONTROLLER_BOOT_TIMEOUT) || 10000
let SOCKETCLUSTER_OPTIONS
if (process.env.SOCKETCLUSTER_OPTIONS) {
SOCKETCLUSTER_OPTIONS = JSON.parse(process.env.SOCKETCLUSTER_OPTIONS)
}
for (let i in SOCKETCLUSTER_OPTIONS) {
if (SOCKETCLUSTER_OPTIONS.hasOwnProperty(i)) {
options[i] = SOCKETCLUSTER_OPTIONS[i]
}
}
const start = function() {
const socketCluster = new SocketCluster(options)
socketCluster.on(socketCluster.EVENT_WORKER_CLUSTER_START, function(workerClusterInfo) {
console.log(' >> WorkerCluster PID:', workerClusterInfo.pid)
})
if (socketCluster.options.environment === 'dev') {
// This will cause SC workers to reboot when code changes anywhere in the app directory.
// The second options argument here is passed directly to chokidar.
// See https://github.com/paulmillr/chokidar#api for details.
console.log(` !! The sc-hot-reboot plugin is watching for code changes in the ${__dirname} directory`)
scHotReboot.attach(socketCluster, {
cwd: __dirname,
ignored: ['public', 'node_modules', 'README.md', 'Dockerfile', 'server.js', 'broker.js', /[\/\\]\./, '*.log']
})
}
return socketCluster
}
const bootCheckInterval = Number(process.env.SOCKETCLUSTER_BOOT_CHECK_INTERVAL) || 200
const bootStartTime = Date.now()
// Detect when Docker volumes are ready.
const startWhenFileIsReady = filePath => {
const errorMessage = `Failed to locate a controller file at path ${filePath} ` + `before SOCKETCLUSTER_CONTROLLER_BOOT_TIMEOUT`
return waitForFile(filePath, bootCheckInterval, bootStartTime, bootTimeout, errorMessage)
}
const filesReadyPromises = [startWhenFileIsReady(workerControllerPath), startWhenFileIsReady(brokerControllerPath), startWhenFileIsReady(workerClusterControllerPath)]
Promise.all(filesReadyPromises)
.then(() => {
return start()
})
.catch(err => {
console.error(err.stack)
process.exit(1)
})