Skip to content

Commit

Permalink
stoppid command added to stop running under forever process by PID
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyAkentiev authored and indexzero committed Nov 4, 2014
1 parent c568f89 commit 54194df
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
95 changes: 95 additions & 0 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ function getAllPids(processes) {
});
}

//
// ### function stopOrRestart (action, event, format, target)
// #### @action {string} Action that is to be sent to target(s).
// #### @event {string} Event that will be emitted on success.
// #### @format {boolean} Indicated if we should CLI format the returned output.
// #### @target {string} Index or script name to stop. Optional.
// #### If not provided -> action will be sent to all targets.
// 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 = [];
Expand Down Expand Up @@ -214,6 +223,67 @@ 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 (processes) {
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 @@ -502,6 +572,17 @@ forever.restart = function (target, format) {
return stopOrRestart('restart', 'restart', format, target);
};

//
// ### function stoppid (target, format)
// #### @pid {string} Pid of process to stop.
// #### @format {boolean} Indicated if we should CLI format the returned output.
// Stops the process with specified pid
//
forever.stoppid = function (pid, format) {
// stopByPid only capable of stopping, but can't restart
return stopByPid('stop', format, pid);
};

//
// ### function restartAll (format)
// #### @format {boolean} Value indicating if we should format output
Expand Down Expand Up @@ -688,6 +769,20 @@ forever.findByUid = function (script, processes) {
});
};

//
// ### function findByPid (pid, processes)
// #### @pid {string} The pid of the process to find.
// #### @processes {Array} Set of processes to find in.
// Finds the process with the specified pid.
//
forever.findByPid = function (pid, processes) {
return !processes
? null
: processes.filter(function (p) {
return p.pid === pid;
});
};

//
// ### function format (format, procs)
// #### @format {Boolean} Value indicating if processes should be formatted
Expand Down
20 changes: 20 additions & 0 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var help = [
' start Start SCRIPT as a daemon',
' stop Stop the daemon SCRIPT',
' stopall Stop all running forever scripts',
' stoppid Stop running forever script by pid',
' restart Restart the daemon SCRIPT',
' restartall Restart all running forever scripts',
' list List all running forever scripts',
Expand Down Expand Up @@ -87,6 +88,7 @@ var app = flatiron.app;
var actions = [
'start',
'stop',
'stoppid',
'stopall',
'restart',
'restartall',
Expand Down Expand Up @@ -287,6 +289,24 @@ app.cmd(/stop (.+)/, cli.stop = function (file) {
});
});

//
// ### function stoppid (pid)
// Stops running forever process by pid.
//
app.cmd(/stoppid (.+)/, cli.stoppid = function (pid) {
var runner = forever.stoppid(pid, true);

runner.on('stop', function (process) {
forever.log.info('Forever stopped process:');
forever.log.data(process);
});

runner.on('error', function (err) {
forever.log.error('Forever cannot find process with pid: ' + pid);
process.exit(1);
});
});

//
// ### function stopall ()
// Stops all currently running forever processes.
Expand Down

0 comments on commit 54194df

Please sign in to comment.