Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: increment_var does not increment when restarting/reloading app #5715

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 8 additions & 15 deletions lib/God.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,17 @@ God.prepare = function prepare (env, cb) {
env.instances = 1;
}

if (env.increment_var) {
env[env.increment_var + "_BKP"] = env.env[env.increment_var];
}

timesLimit(env.instances, 1, function (n, next) {
env.vizion_running = false;
if (env.env && env.env.vizion_running) {
env.env.vizion_running = false;
}

God.injectVariables(env, function inject (err, _env) {
God.injectVariables(env, n, function inject (err, _env) {
if (err) return next(err);
return God.executeApp(Utility.clone(_env), function (err, clu) {
if (err) return next(err);
Expand Down Expand Up @@ -522,7 +526,7 @@ God.finalizeProcedure = function finalizeProcedure(proc) {
* @param {Object} env environnement to be passed to the process
* @param {Function} cb invoked with <err, env>
*/
God.injectVariables = function injectVariables (env, cb) {
God.injectVariables = function injectVariables (env, id, cb) {
// allow to override the key of NODE_APP_INSTANCE if wanted
var instanceKey = process.env.PM2_PROCESS_INSTANCE_VAR || env.instance_var;

Expand Down Expand Up @@ -551,20 +555,9 @@ God.injectVariables = function injectVariables (env, cb) {

// if using increment_var, we need to increment it
if (env.increment_var) {
var lastIncrement = 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[env.increment_var] !== 'undefined';
}).map(function (proc) {
return Number(proc.pm2_env[env.increment_var]);
}).sort(function (a, b) {
return b - a;
})[0];
// inject a incremental variable
var defaut = Number(env.env[env.increment_var]) || 0;
env[env.increment_var] = typeof lastIncrement === 'undefined' ? defaut : lastIncrement + 1;
var defaut = Number(env[env.increment_var + "_BKP"]) || 0;
env[env.increment_var] = defaut + Number(id);
env.env[env.increment_var] = env[env.increment_var];
}

Expand Down
8 changes: 5 additions & 3 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module.exports = function(God) {
// generate a new unique id for new process
proc.unique_id = Utility.generateUUID()

God.injectVariables(proc, function inject (_err, proc) {
God.injectVariables(proc, id, function inject (_err, proc) {
return God.executeApp(Utility.clone(proc), function (err, clu) {
if (err) return cb(err);
God.notify('start', clu, true);
Expand Down Expand Up @@ -268,8 +268,10 @@ module.exports = function(God) {
if (proc.process && proc.process.pid)
return cb(God.logAndGenerateError('Process with pid ' + proc.process.pid + ' already exists'), {});

return God.executeApp(God.clusters_db[id].pm2_env, function(err, proc) {
return cb(err, Utility.clone(proc));
God.injectVariables(proc.pm2_env, id, function inject (_err, proc) {
return God.executeApp(God.clusters_db[id].pm2_env, function(err, proc) {
return cb(err, Utility.clone(proc));
});
});
};

Expand Down