From e57be634f673cd8157ff88a3431783ed35836643 Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <me@adhisimon.org> Date: Fri, 20 Mar 2020 14:05:21 +0700 Subject: [PATCH] Auto jumlah anak --- config.sample.json | 3 ++- index.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/config.sample.json b/config.sample.json index 55147fe..d6e8c4a 100644 --- a/config.sample.json +++ b/config.sample.json @@ -10,5 +10,6 @@ } }, - "cluster": 0 + "# cluster": "Diisi dengan boolean atau angka jumlah anak yang akan dibuat. Jika diisi boolean true, jumlah anak akan dihitung otomatis", + "cluster": false } \ No newline at end of file diff --git a/index.js b/index.js index 4170f81..a426e14 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ process.chdir(__dirname); + const fs = require('fs'); +const numCPUs = require('os').cpus().length; fs.writeFileSync('pid.txt', process.pid); @@ -14,8 +16,8 @@ const logger = require('komodo-sdk/logger'); if (config.cluster && cluster.isMaster) { logger.info(`Running on cluster mode. Master ${process.pid} is running`); - // eslint-disable-next-line no-plusplus - for (let i = 0; i < config.cluster; i++) { + const numberOfChildren = config.cluster === true ? numCPUs + 1 : config.cluster; + for (let i = 0; i < numberOfChildren; i += 1) { cluster.fork(); } } else { -- 1.9.0