pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

env_XXX in Process File should handle pm2 specific attributes

Open Unitech opened this issue 7 years ago • 11 comments

Allow to set PM2 attributes in the different env_XXX in Process files

module.exports = {
  apps : [{
    name      : 'HTTP-API',
    script    : 'http.js',
    exec_mode : 'fork',
    env_production : {
      NODE_ENV : 'production',
      max_memory_restart : '260M',
      trace : true,
      exec_mode      : 'cluster',
      instances         : 4,
    }
  }]
}

Unitech avatar Aug 30 '17 10:08 Unitech

+1 for this! @Unitech being able to specify different settings based upon the environment would be so helpful:

const config = require('../package.json');

module.exports = {
    name: config.name,
    script: 'index.js',
    exec_mode: 'cluster',
    node_args: '--max_old_space_size=512 --harmony --trace-deprecation',
    max_memory_restart: '512M',
    max_restarts: 3,
    restart_delay: 3000,
    min_uptime: 3000,
    error_file: 'logs/error.log',
    out_file: 'logs/output.log',
    watch: ['package.json', 'index.js', 'config/', 'api/', 'services/', 'models/'],
    env_development: {
        NODE_ENV: 'development',
        instances: 1,
    },
    env_staging: {
        NODE_ENV: 'staging',
        instances: 0,
    },
    env_production: {
        NODE_ENV: 'production',
        instances: 0,
    },
};

damianobarbati avatar Oct 29 '17 12:10 damianobarbati

I think it makes sense to leave env_* as it is, and maybe go with something like:

module.exports = {
  apps: [{
    name: 'HTTP-API',
    script: 'http.js',
    exec_mode: 'fork',
    env_production: {
      NODE_ENV: 'production',
    },
    overrides_production: { // or overrides: { production: { ... } }
      max_memory_restart: '260M',
      trace: true,
      exec_mode: 'cluster',
      instances: 4,
    },
  }],
};

dbkaplun avatar May 09 '18 18:05 dbkaplun

Is there likely to be any progress on this. It would be incredibly useful to be able to set config specific to the environment

ungrim97 avatar Jun 11 '18 12:06 ungrim97

Is there an update on this?

bobmulder avatar Aug 08 '18 18:08 bobmulder

Or something like:

module.exports = {
  apps: [{
    name: 'HTTP-API',
    script: 'http.js',
    exec_mode: 'cluster',
    instances: 4,
    instances_dev: 1
    env_production: {
      NODE_ENV: 'production',
    },
  }],
};

i.e. add support for _<env> suffix to other config keys like instances, exec_mode, etc.

LeartS avatar Jan 22 '19 16:01 LeartS

This would be awesome! :)

jsardev avatar Feb 17 '19 21:02 jsardev

+1

Extremely needed. especially when running multiple apps/services with the same echosystem.config.js file and while local files structure is not parallel to production structure, so script/cwd attribute path is different.

Workaround, if you start pm2 this way:

pm2 start ecosystem.config.js --env production

Then you add in your echosystem.config.js file :

const env = process.argv[process.argv.indexOf('--env') + 1];
const isProd = env === 'production';

And then change configuration as you wish. e.g

module.exports = {
    apps: [{
        name: 'web',
        script: isProd ? './path1' : './path2',
        ....
    }],
};

jony89 avatar Feb 27 '19 13:02 jony89

Thanks for sharing your WA @jony89, I very appreciate it.

Does it work even after a crash/reload/restart?

richie3366 avatar Feb 27 '19 13:02 richie3366

@richie3366 should work. just to make sure I've made few tests and it seems that the echosystem.config.js is not reloaded in these cases. and even if was the env should stay the same. (as it is used internally)

jony89 avatar Feb 27 '19 13:02 jony89

Managed to find a workaround for this problem (since I have to run different number of instances for each environment, say: 1 for development, 4 for testing, and 16 for production).

So, first I installed dotenv

npm install dotenv

Then, in my ecosystem.config.js file wrote the following lines to support reading parameters from environmental variables

require("dotenv").config();

module.exports = {
    apps: [{
        name: "koa-backend-pm2",
        exec_mode: "cluster",
        instances: process.env.CLUSTER_NUM_INSTANCES || 1,
        script: "server.js",
    }],
};

I specify number of instances in .env

CLUSTER_NUM_INSTANCES=3

And then I run pm2 start ecosystem.config.js, which produces the following output:

[PM2] Applying action restartProcessId on app [koa-backend-pm2](ids: [ 0, 1, 2 ])
[PM2] [koa-backend-pm2](0) ✓
[PM2] [koa-backend-pm2](1) ✓
[PM2] [koa-backend-pm2](2) ✓

borisevstratov avatar Dec 06 '21 15:12 borisevstratov

Make sure you run pm2 start ecosystem.config.js from the folder containg the env file All other commands (restart etc) don't need this requirement

younes0 avatar Mar 28 '24 03:03 younes0