Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we configure master process when using PM2

Tags:

node.js

pm2

ipc

I have an problem with PM2 in NodeJS. Without PM2, we always have some lines of code like below to configure master process

if(cluster.isMaster){
    //master process configuration
} else {
    //worker process configuration
}

Exactly, I want to send message from a worker to master, then, master will send back a message to all workers for notification an event.

Actually, I saw that, no lines of code in master process configuration runs when using PM2.

Many thanks for any idea about this issue !

like image 870
thelonglqd Avatar asked Dec 24 '15 03:12

thelonglqd


People also ask

What is process manager PM2?

PM2 is a production process manager for Node. js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Where is PM2 config file?

The default configuration file is ecosystem. core3. config. js , and is located in the root folder of lisk-service : It contains a configuration to connect to a local Lisk Core node.

How do I start a PM2 process with name?

Use pm2 start npm --no-automation --name {app name} -- run {script name} . It works.


3 Answers

With PM2, you usually don't have to use this constuction. Typically, it looks like the following:

var cluster = require('cluster');  
var http    = require('http');  
var os      = require('os');
var numCPUs = os.cpus().length;

if(cluster.isMaster){
  for (var i = 0; i < numCPUs; ++i) {
    cluster.fork();
  }
} else {
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world");
  }).listen(8080);
}

With PM2 the equivalent for the above is:

var http = require('http');

http.createServer(function(req, res) {  
  res.writeHead(200);
  res.end("hello world");
}).listen(8080);

pm2 start app.js -i <number of instances>

you can read up more on the topic here

Update: You can try to distinguish between master and slave by passing command line arguments. Here is an example ecosystem.json:

{
  "apps" : [
    {
      "name": "Master",
      "script": "app.js",
      "args": ["master"],
      "instances": "1",
    },
    {
      "name": "Slave",
      "script": "app.js",
      "args": ["slave"],
      "instances": "3"
    }
  ],
...

Then you can do the following:

argv = process.argv.slice(2) //stripe 'node', 'app.js' away

if (argv[0] === 'master'){
   // ...
} else {
  // ...
}
like image 93
stefkin Avatar answered Sep 22 '22 00:09

stefkin


If you're using PM2 >2.5 (i.e. after Jun 2017), you can use the NODE_APP_INSTANCE env variable:

if(process.env.NODE_APP_INSTANCE === "0") {
  // the code in here will only be executed on the first instance in the cluster
}

// the code out here will be executed on all instances

This variable is only available in cluster mode. It counts up from zero for each new instance added. I'm not sure why it's a string rather than an integer.

like image 21
joe Avatar answered Sep 23 '22 00:09

joe


PM2 internally uses "cluster" to create child processes, then itself acts as master, and does not provide any function to the outside. You can still use the following code to create the process, but cluster.isMaster has always been false.

if(cluster.isMaster){
  ...
  cluster.fork();
  ...
} else {
  ...
}

Unless you start like this: pm2 start -x app.js https://github.com/Unitech/pm2/issues/363

The design of PM2 is good, but it brings confusion to beginners. We do everything we can to look at documents and Google search, but it's hard to find any explanation about the design.

like image 30
user9547302 Avatar answered Sep 23 '22 00:09

user9547302