pm2
pm2 copied to clipboard
env_XXX in Process File should handle pm2 specific attributes
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,
}
}]
}
+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)
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) ✓
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