-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-cluster.js
42 lines (35 loc) · 950 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
// Primary file for API
//dependencies
const server = require('./lib/server');
const workers = require('./lib/workers');
const cli = require('./lib/cli');
const os = require('os');
const cluster = require('cluster');
//declare the app
const app = {};
//init function
app.init = function(callback) {
//if we are on master threas start worker and cli
if(cluster.isMaster) {
//start the workers
workers.init();
//start the cli and make sure it starts at last
setTimeout(function() {
cli.init();
callback();
},50);
//fork the process
for(let i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
} else {
//if we are not in the master thread start server
server.init();
}
};
//self invoking only if required directly
if(require.main === module) {
app.init(function(){});
}
//export the app
module.exports = app;