Search code examples
javascriptnode.jspm2process-management

PM2 not restarting clusters on errors


When I use PM2 to run multiple processes (i.e. cluster mode) and one of those processes encounters an uncaught error, PM2 does not restart that process.
Why?
How do I make it restart workers in cluster mode?

Example code

// index.js
let counter = 0;
setInterval(function(){
  if(counter >= 5) {
    throw new Error('Worker crash. Why no restart?');
  }
  counter++;
  console.log('Worker alive: ' + Date.now() );
},500);

Run on the cmd line

pm2 start index.js -i 4
pm2 log

Eventually all the workers crash and never restart.
Whats the point of restarting if it can only be done on a single process.

pm2 logs (merged into single file)

    Worker alive: 1522937847186
Worker alive: 1522937847231
Worker alive: 1522937847276
Worker alive: 1522937847324
Worker alive: 1522937847691
Worker alive: 1522937847736
Worker alive: 1522937847781
Worker alive: 1522937847830
Worker alive: 1522937848193
Worker alive: 1522937848238
Worker alive: 1522937848283
Worker alive: 1522937848332
Worker alive: 1522937848693
Worker alive: 1522937848738
Worker alive: 1522937848783
Worker alive: 1522937848832
Worker alive: 1522937849194
Worker alive: 1522937849238
Worker alive: 1522937849284
Worker alive: 1522937849333
Error: Worker crash. Why no restart?
    at Timeout._onTimeout (/home/usrname/docs/Projects_NodeJS/project/app/index.js:49:11)
    at ontimeout (timers.js:466:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:267:5)
Error: Worker crash. Why no restart?
    at Timeout._onTimeout (/home/usrname/docs/Projects_NodeJS/project/app/index.js:49:11)
    at ontimeout (timers.js:466:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:267:5)
Error: Worker crash. Why no restart?
    at Timeout._onTimeout (/home/usrname/docs/Projects_NodeJS/project/app/index.js:49:11)
    at ontimeout (timers.js:466:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:267:5)
Error: Worker crash. Why no restart?
    at Timeout._onTimeout (/home/usrname/docs/Projects_NodeJS/project/app/index.js:49:11)
    at ontimeout (timers.js:466:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:267:5)

Solution

  • Downgrading to Node version 8 LTS seems to have fixed the problem.

    I had Node version 9 installed and the problem occurred on both Windows and Ubuntu but when I downgraded to version 8, it all worked.