Commit 992b9d18d3236474e53865bd83fe3354d3292434

Authored by Adhidarma Hadiwinoto
1 parent a8b231e40c
Exists in master

Cluster mode

Showing 1 changed file with 18 additions and 3 deletions Side-by-side Diff

lib/webservice/index.js
  1 +const cluster = require('cluster');
  2 +const numCPUs = require('os').cpus().length;
1 3 const express = require('express');
2 4  
3 5 const config = require('komodo-sdk/config');
... ... @@ -10,6 +12,19 @@ const routerPriceplan = require('./router/priceplan');
10 12  
11 13 app.use('/apikey/:apikey/priceplan', routerPriceplan);
12 14  
13   -app.listen(listenPort, () => {
14   - logger.info(`WEBSERVICE listen on ${listenPort}`);
15   -});
  15 +if (cluster.isMaster) {
  16 + logger.info(`Master ${process.pid} is running`);
  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 +}