diff --git a/lib/webservice/index.js b/lib/webservice/index.js index 7f0ce49..7ca9f32 100644 --- a/lib/webservice/index.js +++ b/lib/webservice/index.js @@ -1,3 +1,5 @@ +const cluster = require('cluster'); +const numCPUs = require('os').cpus().length; const express = require('express'); const config = require('komodo-sdk/config'); @@ -10,6 +12,19 @@ const routerPriceplan = require('./router/priceplan'); app.use('/apikey/:apikey/priceplan', routerPriceplan); -app.listen(listenPort, () => { - logger.info(`WEBSERVICE listen on ${listenPort}`); -}); +if (cluster.isMaster) { + logger.info(`Master ${process.pid} is running`); + + // Fork workers. + for (let i = 0; i < numCPUs; i += 1) { + cluster.fork(); + } + + cluster.on('exit', (worker) => { + logger.info(`worker ${worker.process.pid} died`); + }); +} else { + app.listen(listenPort, () => { + logger.info(`WEBSERVICE ${process.pid} listen on ${listenPort}`); + }); +}