From 29e29049130818d2c0c322180fe9b5e5e49953fa Mon Sep 17 00:00:00 2001 From: Dinesh Sawant Date: Thu, 24 Mar 2022 14:29:49 +0530 Subject: [PATCH 1/4] Add graceful stop --- cluster.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cluster.js b/cluster.js index d1937b5..d74ee22 100644 --- a/cluster.js +++ b/cluster.js @@ -13,6 +13,8 @@ const WORKER_CNT = config.workerVal; const activeWorkers = []; const RESTART_FILE = path.join(codeDir, 'tmp/restart.txt'); +const STOP_FILE = path.join(codeDir, 'tmp/stop.txt'); +let stopCalled = false; const forceKill = (worker) => { if (!worker.isDead()) { @@ -52,6 +54,14 @@ if (cluster.isMaster) { logger.info( `worker ${worker.process.pid} died with signal ${signal} code ${code}` ); + if (stopCalled) { + if (activeWorkers.length == 0) { + logger.info( + `All workers stopped. Exiting master process ${process.pid}` + ); + process.exit(0); + } + } if (activeWorkers.length == 0) spawnNewWorkers(); }); @@ -65,6 +75,13 @@ if (cluster.isMaster) { spawnNewWorkers(); } }); + watch(STOP_FILE, () => { + stopCalled = true; + if (Date.now() > currTime) { + currTime = Date.now(); + disconnectOldWorkers(); + } + }); } else { const proxy = new Proxy(); cluster.worker.on('disconnect', () => { From a4a50cbc106def2a90ecd8873065ad2c41f7295f Mon Sep 17 00:00:00 2001 From: Dinesh Sawant Date: Thu, 24 Mar 2022 14:32:21 +0530 Subject: [PATCH 2/4] Add stop.tt --- tmp/stop.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tmp/stop.txt diff --git a/tmp/stop.txt b/tmp/stop.txt new file mode 100644 index 0000000..e69de29 From 31ec38f53decb81d3822f90ba54feaa8d1d1948b Mon Sep 17 00:00:00 2001 From: Dinesh Sawant Date: Thu, 24 Mar 2022 18:57:27 +0530 Subject: [PATCH 3/4] Fix spawn logic --- cluster.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cluster.js b/cluster.js index d74ee22..a862f04 100644 --- a/cluster.js +++ b/cluster.js @@ -62,7 +62,7 @@ if (cluster.isMaster) { process.exit(0); } } - if (activeWorkers.length == 0) spawnNewWorkers(); + if (activeWorkers.length == 0 && !stopCalled) spawnNewWorkers(); }); spawnNewWorkers(); @@ -76,6 +76,7 @@ if (cluster.isMaster) { } }); watch(STOP_FILE, () => { + logger.info(`Stopping cluster gracefully`) stopCalled = true; if (Date.now() > currTime) { currTime = Date.now(); From 7cba3ab6ce24f556e0c3e05474d5baf4fc455ec9 Mon Sep 17 00:00:00 2001 From: Dinesh Sawant Date: Fri, 25 Mar 2022 17:26:25 +0530 Subject: [PATCH 4/4] Add condition for graceful stop watch --- cluster.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cluster.js b/cluster.js index a862f04..444731e 100644 --- a/cluster.js +++ b/cluster.js @@ -1,7 +1,7 @@ 'use strict'; const cluster = require('cluster'); -const { watch } = require('fs'); +const { watch, fstat, existsSync } = require('fs'); const path = require('path'); const codeDir = process.env.CODE_DIR || path.join(__dirname, '/'); @@ -75,14 +75,18 @@ if (cluster.isMaster) { spawnNewWorkers(); } }); - watch(STOP_FILE, () => { - logger.info(`Stopping cluster gracefully`) - stopCalled = true; - if (Date.now() > currTime) { - currTime = Date.now(); - disconnectOldWorkers(); - } - }); + if (existsSync(STOP_FILE)) { + watch(STOP_FILE, () => { + logger.info(`Stopping cluster gracefully`) + stopCalled = true; + if (Date.now() > currTime) { + currTime = Date.now(); + disconnectOldWorkers(); + } + }); + } else { + logger.info('Graceful stop is not enabled') + } } else { const proxy = new Proxy(); cluster.worker.on('disconnect', () => {