Pm2: Pass instance id to app as environment variable

Created on 1 Apr 2015  Â·  20Comments  Â·  Source: Unitech/pm2

When starting pm2 with multiple instances for a specific app, it would really help a lot if the app itself would get an Int representing its id in the series of instances.

For example, given a pm2 configuration that loads 3 instances:

app1 - instance id 0
app2 - instance id 1
app3 - instance id 2

In the node-config module they use the NODE_APP_INSTANCE env variable, but setting it in the pm2.json file itself can also work.

Most helpful comment

Hi,
process.env.pm_id does that :)

All 20 comments

Hi,
process.env.pm_id does that :)

Any chance to make it configurable?

I'm not sure I'm understanding this.
You want to set the id yourself when you launch your app, is that it ?

Let me explain:
For example, i want to start a 6 instance app that listens on ports starting from 8080 to 8085.
I need to know which instance is currently running within those 6 instances so i will know whether i should bind to 8080, 8081, etc...

I need this ID to get passed to the instance itself (probably through environment variables).
Also, this ID needs to be a zero-indexed (or one-indexed) ID, based on the current number of instances in PM2, and not the pm2-id (which after few starts/restarts can get to a much bigger number).

Does that make sense?

You can set a port indication to your processes, I think that it'll then
be available in the pm2 list.
Le jeu. 2 avr. 2015 à 17:17, Shahar Mor [email protected] a écrit :

Let me explain:
For example, i want to start a 6 instance app that listens on ports
starting from 8080 to 8085.
I need to know which instance is currently running within those 6
instances so i will know whether i should bind to 8080, 8081, etc...

I need this ID to get passed to the instance itself (probably through
environment variables).
Also, this ID needs to be a zero-indexed (or one-indexed) ID, based on the
current number of instances in PM2, and not the pm2-id (which after few
starts/restarts can get to a much bigger number).

Does that make sense?

—
Reply to this email directly or view it on GitHub
https://github.com/Unitech/PM2/issues/1143#issuecomment-88942015.

@soyuka not sure i understand... can you give me an example? Define the port where?
I prefer the port to be configured in my central config file, as obviously its not related to pm2... and i might want to use that instance ID for other things than the port.

Lets say i have the following pm2.json file:

{
  "apps": [
    {
      "name": "myapp",
      "script": "./index.js",
      "instances": 6,
      "instance_id_env": "NODE_APP_INSTANCE"
      "exec_mode": "fork",
      "env": {
        "NODE_ENV": "production"
      }
    }
  ]
}

I want to be able to do this in my index.js file:

var instanceId = process.env.NODE_APP_INSTANCE;
var express = require('express');
var app = express();

app.listen(8080 + parseInt(instanceId, 10));

and that PM2 will populate the NODE_APP_INSTANCE id by itself. (being one of 0 to 5 for this example)

right now i solve it using a different script to start the instances myself:

var instances = 6;
for (var i = 0; i < instances; i++) {
    // start a standard pm2 json file
    pm2.startJson(path.resolve(__dirname, 'pm2.json'), {
        additional_env: {
            NODE_APP_INSTANCE: i
        }
    }, function(err) {
        if (err) {
            throw new Error(err.msg);
        }

        // make sure we only disconnect after all instances were started
        if (++started === instances) {
            pm2.disconnect();
        }
    });
}

but that takes the point out of pm2's multi instance feature...

Ok I got it. we could implement this indeed.
But, why would you want the same app to listen on differents ports ?
Cluster module handles that for you already.
Take a look at this post : https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/

@shaharmor nvm about the port it was removed in an earlier release ^^.

@jshkurti Maybe he want's to use something like HAProxy in front of the web app to load-balance to one of his pre-defined ports?

Actually thats exactly what i want, but i can think of other usages for
that feature such as differentiate instance level logs

On Thursday, April 2, 2015, Antoine Bluchet [email protected]
wrote:

@shaharmor https://github.com/shaharmor nvm about the port it was
removed in an earlier release ^^.

@jshkurti https://github.com/jshkurti Maybe he want's to use something
like HAProxy in front of the web app to load-balance to one of his
pre-defined ports?

—
Reply to this email directly or view it on GitHub
https://github.com/Unitech/PM2/issues/1143#issuecomment-88959380.

Okay I'm going to implement this feature :)
Would you like to receive it via env such as process.env.instance_id ?

I think having it configureable in the json file would be best, but if you
ask me i'd go for process.env.NODE_APP_INSTANCE

On Thursday, April 2, 2015, Joni Shkurti [email protected] wrote:

Okay I'm going to implement this feature :)
Would you like to receive via env such as process.env.instance_id ?

—
Reply to this email directly or view it on GitHub
https://github.com/Unitech/PM2/issues/1143#issuecomment-88998236.

@jshkurti Depending on the setup, you might want to start multiple instances, in a port range, and have your webserver (eg. nginx) handle the distribution - instead of using the cluster mode.

It's done :)
process.env.NODE_APP_INSTANCE as requested

npm i -g git://github.com/Unitech/PM2#development
Tell me if it works please.
Thanks!

Works great!
Thanks a lot for the quick fix.

When will this be in stable version?

Seems to be working just fine here as well.. thanks.

But there's no process.env.NODE_APP_INSTANCE value. Has it been published to NPM? Or just in the branch develop?

Only works in cluster_mode.
Yes it has been published on npm :)

well I would like to use this feature ( process.env.NODE_APP_INSTANCE ) without the cluster mode. Does that make any difference ?

@sarthakz9, isn't there always just a single instance in that case?

Was this page helpful?
0 / 5 - 0 ratings