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

Windows graceful shutdown fix #4470

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/API/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@
"docDefault": 1600,
"docDescription": "Time in ms before sending the final SIGKILL signal after SIGINT"
},
"shutdown_with_message": {
"type": "boolean",
"docDefault": false,
"docDescription": "Shutdown an application with process.send('shutdown') instead of process.kill(pid, SIGINT)"
},
"listen_timeout": {
"type": "number",
"docDescription": "Time in ms before forcing a reload if app is still not listening/has still note sent ready"
Expand Down
10 changes: 8 additions & 2 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ module.exports = function(God) {
return cb(null, { error : true, message : 'could not kill process w/o pid'});
}

God.killProcess(proc.process.pid, proc.pm2_env, function(err) {
var killCallback = function(err) {
proc.pm2_env.status = cst.STOPPED_STATUS;

God.notify('exit', proc);
Expand All @@ -352,7 +352,13 @@ module.exports = function(God) {

proc.process.pid = 0;
return cb(null, God.getFormatedProcess(id));
});
};

if(proc.pm2_env.shutdown_with_message == true && proc.process.send){
return God.softKillProcess(proc, killCallback);
}

God.killProcess(proc.process.pid, proc.pm2_env, killCallback);
};

God.resetMetaProcessId = function(id, cb) {
Expand Down
17 changes: 17 additions & 0 deletions lib/God/Methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ module.exports = function(God) {
}
};

/**
* Description
* @method softKillProcess
* @param Object proc
* @param function cb
* @return CallExpression
*/
God.softKillProcess = (proc, cb)=>{
if (!proc) return cb({msg: 'no proc object'});
if (!proc.process) return cb({msg: 'no process object'});
if (!proc.process.pid) return cb({msg : 'no pid for process or null'});

console.log('Send shutdown message for', proc.process.pid);
proc.process.send('shutdown');
God.processIsDead(proc.process.pid, proc.pm2_env, cb);
};

/**
* Description
* @method getNewId
Expand Down
1 change: 1 addition & 0 deletions lib/binaries/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ commander.version(pkg.version)
.option('--parallel <number>', 'number of parallel actions (for restart/reload)')
.option('-p --pid <pid>', 'specify pid file')
.option('-k --kill-timeout <delay>', 'delay before sending final SIGKILL signal to process')
.option('--shutdown-with-message', 'shutdown an application with process.send(\'shutdown\') instead of process.kill(pid, SIGINT)')
.option('--listen-timeout <delay>', 'listen timeout on application reload')
.option('--max-memory-restart <memory>', 'Restart the app if an amount of memory is exceeded (in bytes)')
.option('--restart-delay <delay>', 'specify a delay between restarts (in milliseconds)')
Expand Down