SHARE
TWEET

Untitled

a guest Sep 15th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const pm2 = require('pm2')
  2. const pino = require('pino')
  3. const logger = pino()
  4.  
  5. pm2.connect(true, function(err) {
  6.   if (err) {
  7.     logger.fatal(err)
  8.     process.exit(2);
  9.   }
  10.   pm2.start([
  11.     {
  12.       script: "master.js",
  13.       output: 'dev/null',
  14.       error: 'dev/null',
  15.       maxRestarts: 2,
  16.       restartDelay: 1000,
  17.     },
  18.     {
  19.       script: "worker.js",
  20.       output: 'dev/null',
  21.       error: 'dev/null',
  22.       maxRestarts: 2,
  23.       restartDelay: 1000,
  24.     },
  25.   ],
  26.     function(err, procs) {
  27.       if(err) {
  28.         throw err
  29.       }
  30.     });
  31.  
  32. pm2.launchBus((err, bus) => {
  33.   bus.on('log:out', data => {
  34.     process.stdout.write(data.data);
  35.   });
  36.   bus.on('log:err', data => {
  37.     process.stderr.write(data.data);
  38.   });
  39.   bus.on('log:PM2', data => {
  40.     logger.debug(data.data);
  41.   });
  42.  })
  43. })
  44.  
  45.  
  46. process.stdin.resume();//so the program will not close instantly
  47.  
  48. function exitHandler(options, exitCode) {
  49.     pm2.killDaemon(function(err) {
  50.         if(err) {
  51.           throw err
  52.         }
  53.         pm2.disconnect()
  54.         if (options.exit) process.exit();
  55.     })
  56. }
  57.  
  58. //do something when app is closing
  59. process.on('exit', exitHandler.bind(null,{cleanup:true}));
  60.  
  61. //catches ctrl+c event
  62. process.on('SIGINT', exitHandler.bind(null, {exit:true}));
  63.  
  64. // catches "kill pid" (for example: nodemon restart)
  65. process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
  66. process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
  67.  
  68. //catches uncaught exceptions
  69. process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top