Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass instance id to app as environment variable #1143

Closed
shaharmor opened this issue Apr 1, 2015 · 20 comments
Closed

Pass instance id to app as environment variable #1143

shaharmor opened this issue Apr 1, 2015 · 20 comments

Comments

@shaharmor
Copy link
Contributor

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.

@jshkurti
Copy link
Contributor

jshkurti commented Apr 2, 2015

Hi,
process.env.pm_id does that :)

@shaharmor
Copy link
Contributor Author

Any chance to make it configurable?

@jshkurti
Copy link
Contributor

jshkurti commented Apr 2, 2015

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

@shaharmor
Copy link
Contributor Author

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?

@soyuka
Copy link
Collaborator

soyuka commented Apr 2, 2015

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 notifications@github.com 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
#1143 (comment).

@shaharmor
Copy link
Contributor Author

@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.

@shaharmor
Copy link
Contributor Author

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...

@jshkurti
Copy link
Contributor

jshkurti commented Apr 2, 2015

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/

@soyuka
Copy link
Collaborator

soyuka commented Apr 2, 2015

@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?

@shaharmor
Copy link
Contributor Author

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 notifications@github.com
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
#1143 (comment).

@jshkurti
Copy link
Contributor

jshkurti commented Apr 2, 2015

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

@jshkurti jshkurti self-assigned this Apr 2, 2015
@shaharmor
Copy link
Contributor Author

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 notifications@github.com 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
#1143 (comment).

@danieljuhl
Copy link

@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.

jshkurti added a commit that referenced this issue Apr 8, 2015
@jshkurti
Copy link
Contributor

jshkurti commented Apr 8, 2015

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!

@jshkurti jshkurti closed this as completed Apr 9, 2015
@shaharmor
Copy link
Contributor Author

Works great!
Thanks a lot for the quick fix.

When will this be in stable version?

@danieljuhl
Copy link

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

@XadillaX
Copy link

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

@jshkurti
Copy link
Contributor

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

@sarthakz9
Copy link

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

@smeijer
Copy link

smeijer commented Apr 10, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants