Created
April 17, 2019 10:26
-
-
Save darshitvvora/7d49d7243ca1351ff4ecc5d7ca0adf2c to your computer and use it in GitHub Desktop.
Boot Node app.js with number of workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This script will boot app.js with the number of workers | |
| // specified in WORKER_COUNT. | |
| // | |
| // The master will respond to SIGHUP, which will trigger | |
| // restarting all the workers and reloading the app. | |
| const cluster = require('cluster'); | |
| const log = require('debug')('boot:master'); | |
| const workerCount = process.env.WORKER_COUNT || 2; | |
| // Defines what each worker needs to run | |
| // In this case, it's app.js a simple node http app | |
| cluster.setupMaster({ exec: process.env.APP_MAIN || 'app.js' }); | |
| // Gets the count of active workers | |
| const numWorkers = () => Object.keys(cluster.workers).length; | |
| let stopping = false; | |
| // Forks off the workers unless the server is stopping | |
| function forkNewWorkers() { | |
| if (!stopping) { | |
| for (let i = numWorkers(); i < workerCount; i++) cluster.fork(); | |
| } | |
| } | |
| // A list of workers queued for a restart | |
| let workersToStop = []; | |
| // Stops a single worker | |
| // Gives 60 seconds after disconnect before SIGTERM | |
| function stopWorker(worker) { | |
| log('stopping', worker.process.pid); | |
| worker.disconnect(); | |
| const killTimer = setTimeout(function() { | |
| worker.kill(); | |
| }, 60000); | |
| // Ensure we don't stay up just for this setTimeout | |
| killTimer.unref(); | |
| } | |
| // Tell the next worker queued to restart to disconnect | |
| // This will allow the process to finish it's work | |
| // for 60 seconds before sending SIGTERM | |
| function stopNextWorker() { | |
| const i = workersToStop.pop(); | |
| const worker = cluster.workers[i]; | |
| if (worker) stopWorker(worker); | |
| } | |
| // Stops all the works at once | |
| function stopAllWorkers() { | |
| stopping = true; | |
| log('stopping all workers'); | |
| for (var id in cluster.workers) stopWorker(cluster.workers[id]); | |
| } | |
| // Worker is now listening on a port | |
| // Once it is ready, we can signal the next worker to restart | |
| cluster.on('listening', stopNextWorker); | |
| // A worker has disconnected either because the process was killed | |
| // or we are processing the workersToStop array restarting each process | |
| // In either case, we will fork any workers needed | |
| cluster.on('disconnect', forkNewWorkers); | |
| // HUP signal sent to the master process to start restarting all the workers sequentially | |
| process.on('SIGHUP', () => { | |
| log('restarting all workers'); | |
| workersToStop = Object.keys(cluster.workers); | |
| stopNextWorker(); | |
| }); | |
| // Kill all the workers at once | |
| process.on('SIGTERM', stopAllWorkers); | |
| // Fork off the initial workers | |
| forkNewWorkers(); | |
| log('app master', process.pid, 'booted'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment