-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (54 loc) · 2 KB
/
server.js
File metadata and controls
92 lines (54 loc) · 2 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* */
const cluster = require('cluster');
const os = require('os');
const cors = require('cors')({ origin: true });
const express = require('express');
const winston = require('winston');
const environment = process.env.NODE_ENV || 'dev';
const { version } = require('./package');
const settings = require(`./environment/public/settings.${environment}`); // eslint-disable-line
/* ************************************************************ */
settings.port = process.env.PORT || settings.port;
const logger = new winston.Logger({
levels: winston.config.npm.levels,
colors: winston.config.npm.colors,
transports: [new winston.transports[settings.logger.transport](settings.logger.config)],
});
const workers = os.cpus().length;
/* ************************************************************ */
if (!/test/.test(environment) && cluster.isMaster) {
logger.profile(`${environment}.master.start workers=${workers}`);
cluster.on('online', worker => {
logger.info(`${environment}.master.worker.online id=${worker.id}`);
});
cluster.on('exit', (worker, code, signal) => {
logger.info(`${environment}.master.worker.exit id=${worker.id} code=${signal || code}`);
cluster.fork();
});
for (let i = 0; i < workers; i += 1) {
cluster.fork();
}
logger.profile(`${environment}.master.start workers=${workers}`);
} else {
const id = cluster.worker ? cluster.worker.id : process.pid;
logger.profile(`${environment}.worker.start id=${id}`);
const server = express();
server.get('/', (req, res) => {
cors(req, res, () => {
res.json({ environment, version, workers });
});
});
server.get('/kill', (req, res) => {
if (!/test/.test(environment)) {
cluster.worker.kill();
}
res.json({ worker: id });
});
server.get('/break', (req, res) => {
res.status(500).json({ status: 500, code: 'Error' });
});
server.listen(settings.port, () => {
logger.profile(`${environment}.worker.start id=${id}`);
});
module.exports = server;
}