-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster.js
35 lines (30 loc) · 940 Bytes
/
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
/* eslint-disable no-console */
import cluster from 'cluster';
import Confidence from 'confidence';
import { createServer } from 'makeen-core';
import manifestConfig from './serverManifest.json';
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// eslint-disable-next-line no-plusplus
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('online', worker => {
console.log(
`Worker ${worker.process.pid} is online, expecting: ${numCPUs} workers.`,
);
});
// Get a new worker if one dies
cluster.on('exit', (worker, code, signal) => {
console.log(
`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`,
);
if (code !== 0 && !worker.suicide) {
console.log('Starting a new worker');
cluster.fork();
}
});
} else if (cluster.isWorker) {
const store = new Confidence.Store(manifestConfig);
createServer(store);
}