-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcluster.js
More file actions
28 lines (25 loc) · 874 Bytes
/
cluster.js
File metadata and controls
28 lines (25 loc) · 874 Bytes
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
'use strict';
const cluster = require('cluster');
const os = require('os');
const logger = require('./libs/logger');
const CPUS = os.cpus();
if (cluster.isMaster) {
logger.info(`Master cluster setting up ${CPUS.length} workers...`);
CPUS.forEach(() => cluster.fork());
cluster.on('online', (worker) => {
logger.info(`Cluster ${worker.process.pid}is online`);
});
cluster.on('listening', (worker) => {
logger.info(`Cluster ${worker.process.pid} connected`);
});
cluster.on('disconnect', (worker) => {
logger.warn(`Cluster ${worker.process.pid} disconnected`);
});
cluster.on('exit', (worker) => {
logger.warn(`Cluster ${worker.process.pid} is dead`);
logger.info('Starting a new cluster...');
return cluster.fork();
});
} else {
require('./server');
}