From d43e44f8b282a120b4b46fe71182eb030d970b97 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Tue, 6 Jun 2017 15:57:32 +0200 Subject: [PATCH] (instance_var) allow to overide the key NODE_APP_INSTANCE #2045 --- lib/API/schema.json | 4 ++++ lib/God.js | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/API/schema.json b/lib/API/schema.json index a5bb59bd..63a8bc9e 100644 --- a/lib/API/schema.json +++ b/lib/API/schema.json @@ -185,5 +185,9 @@ }, "increment_var": { "type": "string" + }, + "instance_var": { + "type": "string", + "default" : "NODE_INSTANCE_APP" } } diff --git a/lib/God.js b/lib/God.js index afbc4bf4..dfb9ae5c 100644 --- a/lib/God.js +++ b/lib/God.js @@ -457,19 +457,22 @@ God.finalizeProcedure = function finalizeProcedure(proc) { * @param {Function} cb invoked with */ 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) {