-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-cluster.js
54 lines (42 loc) · 1.13 KB
/
index-cluster.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
/*
* Primary file for the API
*
*/
// Utility commands available:
// SERVER >>> NODE_DEBUG=server node index.js
// WORKERS >>> NODE_DEBUG=workers node index.js
// CLI >>> NODE_DEBUG=cli node index.js
// Dependencies
const server = require('./lib/server')
const workers = require('./lib/workers')
const cli = require('./lib/cli')
const cluster = require('cluster')
const os = require('os')
// Delclare the app
const app = {}
// Init functions
app.init = (callback) => {
// If we're on the master thread, start the background workers and the CLI
if (cluster.isMaster) {
// Start the workers
workers.init()
// Start the CLI, but make sure it starts last
setTimeout(() => {
cli.init()
callback()
}, 50)
// Fork the process
for (let i = 0; i < os.cpus().length; i++) {
cluster.fork()
}
} else {
// If we're not on the master thread, start the HTTP server
server.init()
}
}
// Self executing (invoking) only if required directly
if (require.main === module) {
app.init(() => {})
}
// Export the app
module.exports = app