Uday Reddy
Uday Reddy

Reputation: 1471

pm2-runtime fails to start cluster

I've a very basic nodejs application with express server which does just this.

const express = require('express');
const PORT = 4000;
const app = express();
app.listen(PORT, function(err) {
  if(!err) {
    console.log("Connected on ${PORT}");
  }
});

My pm2 configuration looks like this:

module.exports = {
  apps : [{
    name      : 'node-webpack-babel-starter',
    script    : './dist/backend.js',
    env: {
     NODE_ENV: 'development'
    },
    env_production : {
      NODE_ENV: 'production'
    },
    instances: 'max',
    output: './logs/out.log',
    error: './logs/error.log',
    log: './logs/combined.outerr.log',
  }],
  deploy : {
    production : {
      user : 'node',
      host : '<>',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : './dist/backend.js',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

According to pm2-runtime, we can use the following command to start a server in production environment.

pm2-runtime start ecosystem.config.js --env production

I tried running on my local box, only one instance is up and all others failed. Below is the log:

[2018-08-22T07:50:13.285Z] PM2 log: Launching in no daemon mode
[2018-08-22T07:50:13.349Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:0
[2018-08-22T07:50:13.357Z] PM2 log: App name:node-webpack-babel-starter id:0 online
[2018-08-22T07:50:13.361Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:1
[2018-08-22T07:50:13.367Z] PM2 log: App name:node-webpack-babel-starter id:1 online
[2018-08-22T07:50:13.369Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:2
[2018-08-22T07:50:13.380Z] PM2 log: App name:node-webpack-babel-starter id:2 online
[2018-08-22T07:50:13.391Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:3
[2018-08-22T07:50:13.411Z] PM2 log: App name:node-webpack-babel-starter id:3 online
Example app listening on port 4000!
Example app listening on port 4000!
[2018-08-22T07:50:13.730Z] PM2 log: App [node-webpack-babel-starter] with id [2] and pid [22967], exited with code [1] via signal [SIGINT]
[2018-08-22T07:50:13.733Z] PM2 error: Cancelling versioning data parsing
[2018-08-22T07:50:13.734Z] PM2 log: Starting execution sequence in -fork mode- for app name:node-webpack-babel-starter id:2
Error: listen EADDRINUSE :::4000
    at Server.setupListenHandle [as _listen2] (net.js:1335:14)
    at listenInCluster (net.js:1383:12)
    at Server.listen (net.js:1470:7)
    at Function.listen (/home/box/mien/webpack-node/node_modules/express/lib/application.js:618:24)
    at Object.listen (/home/box/mien/webpack-node/dist/webpack:/src/main.js:9:5)
    at __webpack_require__ (/home/box/mien/webpack-node/dist/webpack:/webpack/bootstrap:22:1)
    at module.exports (/home/box/mien/webpack-node/dist/webpack:/webpack/bootstrap:74:1)
    at Object.<anonymous> (/home/box/mien/webpack-node/dist/backend.js:78:10)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
Example app listening on port 4000!
[2018-08-22T07:50:13.754Z] PM2 log: App name:node-webpack-babel-starter id:2 online
Error: listen EADDRINUSE :::4000

On running pm2 ps,I see this enter image description here

Interestingly, it works fine when I run pm2 start.


Update event on pm2 start only one instance is up all others are down tying to connet to 4000 port.

Upvotes: 0

Views: 2997

Answers (1)

Uday Reddy
Uday Reddy

Reputation: 1471

I forgot to mention the exec_mode attribute in my ecosystem.config.js.

If we want to run in cluster mode we need to exec_mode: 'cluster'.

See discussion.

Upvotes: 2

Related Questions