Pm2: env_XXX in Process File should handle pm2 specific attributes

Created on 30 Aug 2017  路  9Comments  路  Source: Unitech/pm2

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,
    }
  }]
}
CLI Enhancement

Most helpful comment

+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',
        ....
    }],
};

All 9 comments

+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,
    },
};

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,
    },
  }],
};

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

Is there an update on this?

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.

This would be awesome! :)

+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',
        ....
    }],
};

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

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

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

Was this page helpful?
0 / 5 - 0 ratings