Skip to content

Commit

Permalink
[#2958 and related] allow to delete attribute on restart by setting i…
Browse files Browse the repository at this point in the history
…t to null
  • Loading branch information
Unitech committed Jul 2, 2017
1 parent a5f31b4 commit fc050b0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- #2968 pm2 attach <pm_id> + pm2-runtime allows to attach to process stdin / stdout
- #2951 pm2 reload command locker via timestamped lock file
- #2977 pm2 reloadLogs protected
- #2958 Allow to delete attribute via --attribute null
- multiple pm2-docker enhacements
- Alias pm2.link and pm2.unlink to pm2.interact and pm2._pre_interact
- Allow to customize kill signal via PM2_KILL_SIGNAL
Expand Down
2 changes: 1 addition & 1 deletion lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ module.exports = function(God) {
* Same system in reloadProcessId and softReloadProcessId
*/
Utility.extendExtraConfig(proc, opts);
Utility.extend(proc.pm2_env.env, opts.env);
Utility.extend(proc.pm2_env.env, env);

if (God.pm2_being_killed) {
return cb(God.logAndGenerateError('[RestartProcessId] PM2 is being killed, stopping restart procedure...'));
Expand Down
26 changes: 20 additions & 6 deletions lib/Utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Utility = module.exports = {
},
extendExtraConfig : function(proc, opts) {
if (opts.env && opts.env.current_conf) {
Utility.extend(proc.pm2_env, opts.env.current_conf);
Utility.extendMix(proc.pm2_env, opts.env.current_conf);
delete opts.env.current_conf;
}
},
Expand All @@ -41,6 +41,20 @@ var Utility = module.exports = {

return destination;
},
// Same as extend but drop value with 'null'
extendMix : function(destination, source){
if (!source || typeof source != 'object') return destination;

Object.keys(source).forEach(function(new_key) {
if (source[new_key] == 'null')
delete destination[new_key];
else if (source[new_key] != '[object Object]')
destination[new_key] = source[new_key];
});

return destination;
},

whichFileExists : function(file_arr) {
var f = null;

Expand Down Expand Up @@ -153,12 +167,12 @@ var Utility = module.exports = {

async.waterfall(flows, callback);
},

/**
* Function parse the module name and returns it as canonic:
* - Makes the name based on installation filename.
* - Removes the Github author, module version and git branch from original name.
*
*
* @param {string} module_name
* @returns {string} Canonic module name (without trimed parts).
* @example Always returns 'pm2-slack' for inputs 'ma-zal/pm2-slack', 'ma-zal/pm2-slack#own-branch',
Expand All @@ -167,7 +181,7 @@ var Utility = module.exports = {
getCanonicModuleName: function(module_name) {
if (typeof module_name !== 'string') return null;
var canonic_module_name = module_name;

// Returns the module name from a .tgz package name (or the original name if it is not a valid pkg).
// Input: The package name (e.g. "foo.tgz", "foo-1.0.0.tgz", "folder/foo.tgz")
// Output: The module name
Expand All @@ -184,7 +198,7 @@ var Utility = module.exports = {
if(canonic_module_name.indexOf('git+') !== -1) {
canonic_module_name = canonic_module_name.split('/').pop();
}

//pm2 install username/module
else if(canonic_module_name.indexOf('/') !== -1) {
canonic_module_name = canonic_module_name.split('/')[1];
Expand All @@ -203,7 +217,7 @@ var Utility = module.exports = {
if (canonic_module_name.indexOf('.git') !== -1) {
canonic_module_name = canonic_module_name.replace('.git', '');
}

return canonic_module_name;
}

Expand Down
32 changes: 20 additions & 12 deletions test/bash/app-config-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $pm2 start app-config-update/echo.js
$pm2 prettylist | grep "node_args: \[\]"
spec "3 Should application have empty node argument list"

$pm2 restart app-config-update/echo.js --node-args="--harmony" --update-env
$pm2 restart app-config-update/echo.js --node-args="--harmony"
$pm2 prettylist | grep "node_args: \[ '--harmony' \]"
spec "4 Should application have one node argument"

Expand All @@ -36,12 +36,12 @@ spec "5 Should application have one node argument"
#
# Rename
#
$pm2 restart 0 --name="new-name" --update-env
$pm2 restart 0 --name="new-name"
$pm2 reset all
$pm2 restart new-name
should '6 should restart processes with new name' 'restart_time: 1' 1

$pm2 start 0 --name="new-name-2" --update-env
$pm2 start 0 --name="new-name-2"
$pm2 reset all
$pm2 restart new-name-2
should '7 should restart processes with new name' 'restart_time: 1' 1
Expand All @@ -52,22 +52,30 @@ $pm2 delete all

$pm2 start app-config-update/echo.js -i 1
$pm2 prettylist | grep "node_args: \[\]"
spec "8 Should application have empty node argument list"
spec "Should application have empty node argument list"

$pm2 reload app-config-update/echo.js --node-args="--harmony" --update-env
$pm2 reload app-config-update/echo.js --node-args="--harmony"
$pm2 prettylist | grep "node_args: \[ '--harmony' \]"
spec "9 Should application have one node argument"
spec "Should application have one node argument"

$pm2 gracefulReload app-config-update/echo.js --node-args="--harmony" --update-env
$pm2 gracefulReload app-config-update/echo.js --node-args="--harmony"
$pm2 prettylist | grep "node_args: \[ '--harmony' \]"
spec "10 Should application have two node arguments"
spec "Should application have two node arguments"

$pm2 reload echo --name="new-name" --update-env
$pm2 prettylist | grep "node_args"
spec "Should have found parameter"
# Now set node-args to null
$pm2 gracefulReload app-config-update/echo.js --node-args=null
# Should not find node_args anymore
$pm2 prettylist | grep "node_args"
ispec "Should have deleted cli parameter when passing null"

$pm2 reload echo --name="new-name"
$pm2 reset all
$pm2 restart new-name
should '11 should reload processes with new name' 'restart_time: 1' 1
should 'should reload processes with new name' 'restart_time: 1' 1

$pm2 gracefulReload new-name --name="new-name-2" --update-env
$pm2 gracefulReload new-name --name="new-name-2"
$pm2 reset all
$pm2 restart new-name-2
should '12 should graceful reload processes with new name' 'restart_time: 1' 1
should 'should graceful reload processes with new name' 'restart_time: 1' 1

0 comments on commit fc050b0

Please sign in to comment.