Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ var daemon = function(config) {
configurable: false,
value: config.group || 'root'
},

/**
* @cfg {Boolean} [stopparentfirst=false]
* Allow the service to shutdown cleanly.
*/
stopparentfirst: {
enumerable: true,
writable: false,
configurable: false,
value: config.stopparentfirst
},

/**
* @cfg {Number} [stoptimeout=30]
* How long to wait in seconds before force killing the application.
* This only takes effect when stopparentfirst is enabled.
*/
stoptimeout: {
enumerable: true,
writable: false,
configurable: false,
value: config.stoptimeout || 30
},

system: {
enumerable: false,
Expand Down
2 changes: 2 additions & 0 deletions lib/systemd.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ var init = function(config){
env: '',
created: new Date(),
execpath: process.execPath,
stopparentfirst: config.stopparentfirst,
stoptimeout: config.stoptimeout
};

var _env = [];
Expand Down
1 change: 1 addition & 0 deletions lib/systemv.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ var init = function(config){
env: '',
created: new Date(),
execpath: process.execPath,
stoptimeout: config.stoptimeout
};

// escape the double quotes in the wrappercode args here if we are on a redhat system
Expand Down
6 changes: 6 additions & 0 deletions lib/templates/systemd/service
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ SyslogIdentifier={{label}}
User={{user}}
Group={{group}}
Environment={{env}}
{{#stopparentfirst}}
KillMode=process
{{/stopparentfirst}}
{{#stoptimeout}}
TimeoutStopSec={{stoptimeout}}
{{/stoptimeout}}

[Install]
WantedBy=multi-user.target
3 changes: 2 additions & 1 deletion lib/templates/systemv/debian
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ INIT_SCRIPT_NAME=`basename $THIS_ARG`
INIT_SCRIPT_NAME_NOEXT=${INIT_SCRIPT_NAME%.*}
PIDFILE="$LOCAL_VAR_RUN/$INIT_SCRIPT_NAME_NOEXT.pid"
SCRIPTNAME=/etc/init.d/$INIT_SCRIPT_NAME
STOPTIMEOUT={{stoptimeout}}

# Exit if the package is not installed
[ -x "$DAEMON" ] || { echo "can't find Node.js ($DAEMON)" >&2; exit 0; }
Expand Down Expand Up @@ -147,7 +148,7 @@ do_stop()
# 2 if daemon could not be stopped
# other if a failure occurred

start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --name $LABEL
start-stop-daemon --stop --quiet --retry=TERM/$STOPTIMEOUT/KILL/5 --pidfile $PIDFILE --chuid $NODEUSER --name $LABEL
RETVAL="$?"

#[ "$VERBOSE" != no ] && [ "$RETVAL" = 1 ] && log_daemon_msg " ---> SIGKILL failed => hardkill $DESC" "$INIT_SCRIPT_NAME_NOEXT"
Expand Down
11 changes: 11 additions & 0 deletions lib/templates/systemv/redhat
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ do_stop()
echo -n "Stopping $LABEL:"
PID=`cat ${PIDFILE}`
kill $PID > /dev/null
TIMEOUT={{stoptimeout}}
# wait for process to exit
# if waiting longer than the timeout send a SIGKILL
while kill -0 $PID 2>/dev/null; do
if [ $TIMEOUT -le 0 ]; then
kill -9 $PID
echo "Process didn't exit before timeout, sent SIGKILL"
fi
sleep 1;
((TIMEOUT--));
done;
if [ -f ${LOCK_FILE} ] ; then
rm ${LOCK_FILE}
fi
Expand Down
5 changes: 3 additions & 2 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ var launch = function(){
console.error(argv.f+' exited with error code '+code);
process.exit();
server.unref();
} else if (forcekill) {
process.exit();
server.unref();
}

delete child.pid;
Expand All @@ -183,15 +186,13 @@ process.on('exit',function(){
console.log("Got exit signal, closing down");
forcekill = true;
child.kill();
process.exit();
});

// Killing the wrapper does not kill the child node process without this handler
process.on('SIGTERM',function(){
console.log("Got SIGTERM, closing down");
forcekill = true;
child.kill();
process.exit();
});

process.on('uncaughtException', function(err) {
Expand Down