Commit 992b9d18d3236474e53865bd83fe3354d3292434
1 parent
a8b231e40c
Exists in
master
Cluster mode
Showing 1 changed file with 18 additions and 3 deletions Inline Diff
lib/webservice/index.js
1 | const cluster = require('cluster'); | ||
2 | const numCPUs = require('os').cpus().length; | ||
1 | const express = require('express'); | 3 | const express = require('express'); |
2 | 4 | ||
3 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
4 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
5 | 7 | ||
6 | const app = express(); | 8 | const app = express(); |
7 | const listenPort = config.webservice.port || config.webservice.listen_port; | 9 | const listenPort = config.webservice.port || config.webservice.listen_port; |
8 | 10 | ||
9 | const routerPriceplan = require('./router/priceplan'); | 11 | const routerPriceplan = require('./router/priceplan'); |
10 | 12 | ||
11 | app.use('/apikey/:apikey/priceplan', routerPriceplan); | 13 | app.use('/apikey/:apikey/priceplan', routerPriceplan); |
12 | 14 | ||
13 | app.listen(listenPort, () => { | 15 | if (cluster.isMaster) { |
14 | logger.info(`WEBSERVICE listen on ${listenPort}`); | 16 | logger.info(`Master ${process.pid} is running`); |
15 | }); | 17 | |
18 | // Fork workers. | ||
19 | for (let i = 0; i < numCPUs; i += 1) { | ||
20 | cluster.fork(); | ||
21 | } | ||
22 | |||
23 | cluster.on('exit', (worker) => { | ||
24 | logger.info(`worker ${worker.process.pid} died`); | ||
25 | }); | ||
26 | } else { | ||
27 | app.listen(listenPort, () => { | ||
28 | logger.info(`WEBSERVICE ${process.pid} listen on ${listenPort}`); | ||
29 | }); | ||
30 | } | ||
16 | 31 |