Increment_var for PM2 together with Doppler

Hi,

I am using PM2 increment_var for PORT that is incrementing the port for a PM2 cluster. Without Doppler it is working, but with Doppler it just ignores the Port in the config (below). The expected behaviour would be to have 2 instances (one running at Port 8080 and one other at Port 8081).

Thank you

// ecosystem.config.js
module.exports = {
  "apps": [
    {
      "name": "pm2-test",
      "script": "Backend/index.js",
      "instances": 2,
      "exec_mode": "cluster",
      "watch": true,
      "increment_var": "PORT",
      "dev": {
        "PORT": 8080
      },
      "stage": {
        "PORT": 8080
      },
      "prod": {
        "PORT": 8080
      }
    }
  ]
}

I did it differently, by returning back to the PM2-JSON-config-file (Below) and getting the secrets from code afterwards (Below).

Best regards

// PM2.json
{
  "apps": [
    {
      "name": "pm2-test",
      "script": "Backend/index.js",
      "instances": 2,
      "exec_mode": "cluster",
      "increment_var" : "PORT",      
      "env_dev": {
        "PORT" : 8080,
        "NODE_ENV": "development"
      },
      "env_prod": {
        "PORT": 8080,
        "NODE_ENV": "production"
      }
    }
  ]
}
// Getting secrets in index.js and extending environment-variables
const subProcess = require('child_process')
subProcess.exec(`doppler setup -c ${process.env.NODE_ENV} -p testproject`)

const _ = require('underscore');
const doppler = require('../doppler')
const secrets = doppler.fetchSecrets()
_.extend(process.env, secrets)

Starting PM2 cluster by:

pm2 start PM2.json --env dev

Hi @Peer!

Welcome to the Doppler Community!

Doing this is actually pretty easy out of the box. The issue you’re running into is that you’re defining the environment variable in your config file. Using an ecosystem file that looks like this will work:

module.exports = {
  apps : [
      {
        name: "myapp",
        script: "app.js",
        instances: 2,
        exec_mode: "cluster",
        watch: true,
        increment_var : "PORT"
      }
  ]
}

Then simply run pm2 using Doppler when starting it initially:

doppler run -- pm2 start ecosystem.config.js

That will spawn your cluster basing its incremented PORT values from what it got from Doppler!

Regards,
-Joel

Hi @watsonian

Thats the same that I had, when I understand correctly what you mean. You can see my ecosystem.config.js in the original question. It did not work for me or did I forget to consider anything that you ment in your answer.

Thank you for your time and effort. :slight_smile:

Hi @watsonian

I found a difference in your answer in how you start the cluster. When I do that it increments in a strage way.

When I run a cluster with PM2 with 3 instances, then
the first instance has Port: 8080
the second instance has Port 80801
the third instance has Port 808011, which leads to an error like this:

uncaughtException RangeError: options.port should be >= 0 and < 65536. Received 808011.

Yep, you’re right. It looks like what’s happening here is that it’s not handling when PORT is set to a string properly. If you set it to an integer in the config file like this:

env: {
  "PORT": 3000
}

Then it works as expected. However, if you instead set it to this:

env: {
  "PORT": "3000"
}

to emulate the fact that all environment variables start as strings in process.env then you see the same behavior where port 3000 works fine but the incremented port is on 30001.

From my perspective, this seems like a bug in pm2. It looks like this bug was reported back in Jan 2021:

I recommend chiming in on that issue to show your support for it.

@Peer As an aside, I did take a look at the code surrounding this. I’m not an expert when it comes to PM2 or node, but it looked easy enough to solve, so I submitted a PR to fix this:

We’ll see if it gets accepted!

@Peer It looks like the change was merged, so the next PM2 release should have that fixed!

Hi @watsonian

Awesome. Thanks for your effort.

When I tried your approach I tried with a number and also with a string, so in my case 8080 and also “8080”, but it did not help, meaning the incremented-port stayed strange, like 80801 and 808011.

I am curious how it will run with the new PM2 version, with your changes inside :slight_smile: . In the meantime I will stick to my workaround.

Thank you :slight_smile: