Skip to content

Commit

Permalink
clean codes, and improvefindBy performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjatse committed Nov 9, 2014
1 parent 2c394ab commit 5c7ba63
Showing 1 changed file with 14 additions and 71 deletions.
85 changes: 14 additions & 71 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ function stopOrRestart(action, event, format, target) {
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));
// FIX: actually, this message comes from `forever-monitor`, the process is marked as `STOPPED`.
// message: Cannot stop process that is not running.
if(data && data.message.search('is not running')){
caseElse = 'error';
}
next(caseElse == 'ok' ? new Error(data.message) : null);
socket.end();
}
Expand Down Expand Up @@ -204,10 +209,15 @@ function stopOrRestart(action, event, format, target) {
var procs = processes;

if (target !== undefined && target !== null) {
procs = forever.findById(target, processes)
if(isNaN(target)){
procs = forever.findByScript(target, processes);
}
procs = procs
|| forever.findById(target, processes)
|| forever.findByIndex(target, processes)
|| forever.findByScript(target, processes)
|| forever.findByUid(target, processes);
|| forever.findByUid(target, processes)
|| forever.findByPid(target, processes)
|| forever.findByScript(target, processes);
}

if (procs && procs.length > 0) {
Expand All @@ -229,73 +239,6 @@ function stopOrRestart(action, event, format, target) {
return emitter;
}

//
// ### function stopByPid(event, format, pid)
// #### @event {string} Event that will be emitted on success.
// #### @format {boolean} Indicated if we should CLI format the returned output.
// #### @pid {string} Pid of the process to stop. Optional.
// Returns emitter that you can use to handle events on failure or success (i.e 'error' or <event>)
//
function stopByPid(event, format, pid) {
var emitter = new events.EventEmitter(),
results = [];

function sendAction(proc, next) {
var socket = new nssocket.NsSocket();

socket.connect(proc.socket, function (err) {
if (err) {
next(err);
}

socket.dataOnce(['stop', 'ok'], function (data) {
next();
socket.end();
});

socket.send(['stop']);
});

socket.on('error', function (err) {
next(err);
});
}

getAllProcesses(function (err, processes) {
if (err) {
return process.nextTick(function () {
emitter.emit('error', err);
});
}

var procs = processes;

// if we specified pid -> send action only to this process
if (pid!== undefined && pid!== null) {
// find only by pid
procs = forever.findByPid(pid, processes);
}

// do 'sendAction' (see above) for all found processes
if (procs && procs.length > 0) {
async.map(procs, sendAction, function (err, results) {
if (err) {
emitter.emit('error', err);
}

emitter.emit(event, forever.format(format, procs));
});
}
else {
process.nextTick(function () {
emitter.emit('error', new Error('Cannot find forever process by pid: ' + pid));
});
}
});

return emitter;
}

//
// ### function load (options, [callback])
// #### @options {Object} Options to load into the forever module
Expand Down Expand Up @@ -592,7 +535,7 @@ forever.restart = function (target, format) {
//
forever.stopbypid = function (pid, format) {
// stopByPid only capable of stopping, but can't restart
return stopByPid('stop', format, pid);
return stopOrRestart('stop', 'stopByPid', format, pid);
};

//
Expand Down

0 comments on commit 5c7ba63

Please sign in to comment.