Skip to content

Commit 5c7ba63

Browse files
committed
clean codes, and improvefindBy performance
1 parent 2c394ab commit 5c7ba63

File tree

1 file changed

+14
-71
lines changed

1 file changed

+14
-71
lines changed

lib/forever.js

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ function stopOrRestart(action, event, format, target) {
171171
var caseElse = data && data.message ? 'ok' : 'error';
172172
// remove other events to avoid `listener over limit` bug.
173173
socket.undata([action, caseElse], onReceived.bind(null, action, socket, next));
174+
// FIX: actually, this message comes from `forever-monitor`, the process is marked as `STOPPED`.
175+
// message: Cannot stop process that is not running.
176+
if(data && data.message.search('is not running')){
177+
caseElse = 'error';
178+
}
174179
next(caseElse == 'ok' ? new Error(data.message) : null);
175180
socket.end();
176181
}
@@ -204,10 +209,15 @@ function stopOrRestart(action, event, format, target) {
204209
var procs = processes;
205210

206211
if (target !== undefined && target !== null) {
207-
procs = forever.findById(target, processes)
212+
if(isNaN(target)){
213+
procs = forever.findByScript(target, processes);
214+
}
215+
procs = procs
216+
|| forever.findById(target, processes)
208217
|| forever.findByIndex(target, processes)
209-
|| forever.findByScript(target, processes)
210-
|| forever.findByUid(target, processes);
218+
|| forever.findByUid(target, processes)
219+
|| forever.findByPid(target, processes)
220+
|| forever.findByScript(target, processes);
211221
}
212222

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

232-
//
233-
// ### function stopByPid(event, format, pid)
234-
// #### @event {string} Event that will be emitted on success.
235-
// #### @format {boolean} Indicated if we should CLI format the returned output.
236-
// #### @pid {string} Pid of the process to stop. Optional.
237-
// Returns emitter that you can use to handle events on failure or success (i.e 'error' or <event>)
238-
//
239-
function stopByPid(event, format, pid) {
240-
var emitter = new events.EventEmitter(),
241-
results = [];
242-
243-
function sendAction(proc, next) {
244-
var socket = new nssocket.NsSocket();
245-
246-
socket.connect(proc.socket, function (err) {
247-
if (err) {
248-
next(err);
249-
}
250-
251-
socket.dataOnce(['stop', 'ok'], function (data) {
252-
next();
253-
socket.end();
254-
});
255-
256-
socket.send(['stop']);
257-
});
258-
259-
socket.on('error', function (err) {
260-
next(err);
261-
});
262-
}
263-
264-
getAllProcesses(function (err, processes) {
265-
if (err) {
266-
return process.nextTick(function () {
267-
emitter.emit('error', err);
268-
});
269-
}
270-
271-
var procs = processes;
272-
273-
// if we specified pid -> send action only to this process
274-
if (pid!== undefined && pid!== null) {
275-
// find only by pid
276-
procs = forever.findByPid(pid, processes);
277-
}
278-
279-
// do 'sendAction' (see above) for all found processes
280-
if (procs && procs.length > 0) {
281-
async.map(procs, sendAction, function (err, results) {
282-
if (err) {
283-
emitter.emit('error', err);
284-
}
285-
286-
emitter.emit(event, forever.format(format, procs));
287-
});
288-
}
289-
else {
290-
process.nextTick(function () {
291-
emitter.emit('error', new Error('Cannot find forever process by pid: ' + pid));
292-
});
293-
}
294-
});
295-
296-
return emitter;
297-
}
298-
299242
//
300243
// ### function load (options, [callback])
301244
// #### @options {Object} Options to load into the forever module
@@ -592,7 +535,7 @@ forever.restart = function (target, format) {
592535
//
593536
forever.stopbypid = function (pid, format) {
594537
// stopByPid only capable of stopping, but can't restart
595-
return stopByPid('stop', format, pid);
538+
return stopOrRestart('stop', 'stopByPid', format, pid);
596539
};
597540

598541
//

0 commit comments

Comments
 (0)