-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-cluster.js
47 lines (39 loc) · 931 Bytes
/
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
/*
* Primary file for the API
*
*
*/
// Dependencies
const server = require('./lib/server');
const workers = require('./lib/workers');
const cli = require('./lib/cli');
const cluster = require('cluster');
const os = require('os');
// Declare the app
var app = {};
// Init function
app.init = (callback) => {
// If we are 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 invoking only if required directly
if (require.main === module) {
app.init(() => {});
}
// Export the app
module.exports = app;