Skip to content

Commit

Permalink
[fix] handle monitor error, and make forever stopall peaceful
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjatse committed Nov 9, 2014
1 parent ab8bcb8 commit d506771
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 12 additions & 6 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,16 @@ function getAllPids(processes) {
// Returns emitter that you can use to handle events on failure or success (i.e 'error' or <event>)
//
function stopOrRestart(action, event, format, target) {
var emitter = new events.EventEmitter(),
results = [];
var emitter = new events.EventEmitter();

function onReceived(action, socket, next, data){
// if `message` exists, it's definitely an Error Object
var caseElse = data && data.message ? 'ok' : 'error';
// remove other events to avoid `listener over limit` bug.
socket.undata([action, caseElse], onReceived.bind(null, action, socket, next));
next(caseElse == 'ok' ? new Error(data.message) : null);
socket.end();
}

function sendAction(proc, next) {
var socket = new nssocket.NsSocket();
Expand All @@ -175,10 +183,8 @@ function stopOrRestart(action, event, format, target) {
next(err);
}

socket.dataOnce([action, 'ok'], function (data) {
next();
socket.end();
});
socket.dataOnce([action, 'ok'], onReceived.bind(null, action, socket, next));
socket.dataOnce([action, 'error'], onReceived.bind(null, action, socket, next));

socket.send([action]);
});
Expand Down
16 changes: 13 additions & 3 deletions lib/forever/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,22 @@ Worker.prototype.start = function (callback) {
});

socket.data(['stop'], function () {
self.monitor.once('stop', function () {
socket.send(['stop', 'ok']);
function onStop(err){
var args = [];
if(err && err instanceof Error){
args.push(['stop', 'error'], {message: err.message, stack: err.stack});
self.monitor.removeListener('stop', onStop);
}else{
args.push(['stop', 'ok']);
self.monitor.removeListener('error', onStop);
}
socket.send.apply(socket, args);
if (self.exitOnStop) {
process.exit();
}
});
}
self.monitor.once('stop', onStop);
self.monitor.once('error', onStop);

if (process.platform === 'win32') {
//
Expand Down

0 comments on commit d506771

Please sign in to comment.