(instance_var) allow to overide the key NODE_APP_INSTANCE #2045

This commit is contained in:
vmarchaud 2017-06-06 15:57:32 +02:00
parent 35945e82d0
commit d43e44f8b2
2 changed files with 10 additions and 3 deletions

View File

@ -185,5 +185,9 @@
},
"increment_var": {
"type": "string"
},
"instance_var": {
"type": "string",
"default" : "NODE_INSTANCE_APP"
}
}

View File

@ -457,19 +457,22 @@ God.finalizeProcedure = function finalizeProcedure(proc) {
* @param {Function} cb invoked with <err, env>
*/
God.injectVariables = function injectVariables (env, cb) {
// allow to override the key of NODE_APP_INSTANCE if wanted
var instanceKey = process.env.PM2_PROCESS_INSTANCE_VAR || env.instance_var;
// we need to find the last NODE_APP_INSTANCE used
var lastInstance = Object.keys(God.clusters_db)
.map(function (procId) {
return God.clusters_db[procId];
}).filter(function (proc) {
return proc.pm2_env.name === env.name &&
typeof proc.pm2_env.NODE_APP_INSTANCE !== 'undefined';
typeof proc.pm2_env[instanceKey] !== 'undefined';
}).map(function (proc) {
return proc.pm2_env.NODE_APP_INSTANCE;
return proc.pm2_env[instanceKey];
}).sort(function (a, b) {
return b - a;
})[0];
env.NODE_APP_INSTANCE = typeof lastInstance === 'undefined' ? 0 : lastInstance + 1;
env[instanceKey] = typeof lastInstance === 'undefined' ? 0 : lastInstance + 1;
// if using increment_var, we need to increment it
if (env.increment_var) {