-
Notifications
You must be signed in to change notification settings - Fork 1
/
index-cluster.js
45 lines (36 loc) · 895 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
/*
* Primary file for Clustered API Server
*
* @author Faiz A. Farooqui <[email protected]>
*/
// 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 = () => {
// If we're on the master thread, start Workers & CLI
if (cluster.isMaster) {
// Start the workers
workers.init();
// Start the CLI, but make sure it starts last
// setTimeout(() => {
// cli.init();
// }, 500);
// Fork the process, the number of times we have CPUs available
for (var i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
} else {
// If we're not on the master thread, Start the server
server.init();
}
};
// Self executing
app.init();
// Export the app
module.exports = app;