Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This script will boot app.js with the number of workers
  2. // specified in WORKER_COUNT.
  3. //
  4. // The master will respond to SIGHUP, which will trigger
  5. // restarting all the workers and reloading the app.
  6.  
  7. var cluster = require('cluster');
  8. var workerCount = 12;
  9. // var workerCount = process.env.WORKER_COUNT || 2;
  10.  
  11. // Defines what each worker needs to run
  12. // In this case, it's app.js a simple node http app
  13. cluster.setupMaster({ exec: 'server.js' });
  14.  
  15. // Gets the count of active workers
  16. function numWorkers() { return Object.keys(cluster.workers).length; }
  17.  
  18. var stopping = false;
  19.  
  20. // Forks off the workers unless the server is stopping
  21. function forkNewWorkers() {
  22.   if (!stopping) {
  23.     for (var i = numWorkers(); i < workerCount; i++) { cluster.fork(); }
  24.   }
  25. }
  26.  
  27. // A list of workers queued for a restart
  28. var workersToStop = [];
  29.  
  30. // Stops a single worker
  31. // Gives 60 seconds after disconnect before SIGTERM
  32. function stopWorker(worker) {
  33.   console.log('stopping', worker.process.pid);
  34.   worker.disconnect();
  35.   var killTimer = setTimeout(function() {
  36.     worker.kill();
  37.   }, 60000);
  38.  
  39.   // Ensure we don't stay up just for this setTimeout
  40.   killTimer.unref();
  41. }
  42.  
  43. // Tell the next worker queued to restart to disconnect
  44. // This will allow the process to finish it's work
  45. // for 60 seconds before sending SIGTERM
  46. function stopNextWorker() {
  47.   var i = workersToStop.pop();
  48.   var worker = cluster.workers[i];
  49.   if (worker) stopWorker(worker);
  50. }
  51.  
  52. // Stops all the works at once
  53. function stopAllWorkers() {
  54.   stopping = true;
  55.   console.log('stopping all workers');
  56.   for (var id in cluster.workers) {
  57.     stopWorker(cluster.workers[id]);
  58.   }
  59. }
  60.  
  61. // Worker is now listening on a port
  62. // Once it is ready, we can signal the next worker to restart
  63. cluster.on('listening', stopNextWorker);
  64.  
  65. // A worker has disconnected either because the process was killed
  66. // or we are processing the workersToStop array restarting each process
  67. // In either case, we will fork any workers needed
  68. cluster.on('disconnect', forkNewWorkers);
  69.  
  70. // HUP signal sent to the master process to start restarting all the workers sequentially
  71. process.on('SIGHUP', function() {
  72.   console.log('restarting all workers');
  73.   workersToStop = Object.keys(cluster.workers);
  74.   stopNextWorker();
  75. });
  76.  
  77. // Kill all the workers at once
  78. process.on('SIGTERM', stopAllWorkers);
  79.  
  80. // Fork off the initial workers
  81. forkNewWorkers();
  82. console.log('app master', process.pid, 'booted');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement