Skip to content

Commit

Permalink
[api fix] Improved the way forever manages pid / fvr files. Added cle…
Browse files Browse the repository at this point in the history
…anlogs command line option
  • Loading branch information
indexzero committed Dec 23, 2010
1 parent 070313e commit 57850e9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
32 changes: 20 additions & 12 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var path = require('path'),
fs = require('fs'),
sys = require('sys');

var accepts = ['start', 'stop', 'stopall', 'list'], action;
var accepts = ['start', 'stop', 'stopall', 'list', 'cleanlogs'], action;
if (accepts.indexOf(process.argv[2]) !== -1) {
action = process.argv.splice(2,1)[0];
}
Expand All @@ -16,13 +16,14 @@ require.paths.unshift(path.join(__dirname, '..', 'lib'));
var forever = require('forever');

var help = [
"usage: forever [start | stop | stopall | list] [options] SCRIPT [script options]",
"usage: forever [start | stop | stopall | list | cleanlogs] [options] SCRIPT [script options]",
"",
"options:",
" start start SCRIPT as a daemon",
" stop stop the daemon SCRIPT",
" stopall stop all running forever scripts",
" list list all running forever scripts",
" cleanlogs [CAREFUL] Deletes all historical forever log files",
"",
" -m MAX Only run the specified script MAX times",
" -l LOGFILE Logs the forever output to LOGFILE",
Expand Down Expand Up @@ -56,7 +57,7 @@ var mappings = {
};

function isSimpleAction () {
return ['list', 'stopall'].indexOf(action) !== -1;
return ['list', 'stopall', 'cleanlogs'].indexOf(action) !== -1;
};

// Show help prompt if requested or if the
Expand Down Expand Up @@ -94,26 +95,33 @@ if (typeof options['max'] === 'undefined') {
var config = {
root: argv.p
};

var loader = forever.load(config);
loader.on('load', function () {
var tidy = forever.cleanUp();
loader.on('load', function () {
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
// Run all of the files forever
if (action) {
switch (action) {
case 'start':
var uid = forever.randomString(16);
var fullPath, uid = forever.randomString(16);
options.uid = uid;
options.pidFile = 'forever' + uid + '.pid';
options.logFile = argv.l || 'forever' + uid + '.log'
options.logFile = argv.l || 'forever' + uid + '.log';
fullPath = path.join(forever.config.root, options.logFile);

fs.stat(file, function (err, stats) {
if (err) {
sys.puts('Cannot start forever: ' + file + ' does not exist.');
fs.stat(fullPath, function (err, stats) {
if (!err) {
sys.puts('Cannot start forever: ' + fullPath + ' exists.');
process.exit(0);
}
forever.startDaemon(file, options);

fs.stat(file, function (err, stats) {
if (err) {
sys.puts('Cannot start forever: ' + file + ' does not exist.');
process.exit(0);
}
forever.startDaemon(file, options);
});
});
break;

Expand Down
51 changes: 41 additions & 10 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ forever.load = function (options, callback) {
options = options || {};
options.root = options.root || path.join('/tmp', 'forever'),
options.pidPath = options.pidPath || path.join(options.root, 'pids');
config = options;
forever.config = config = options;

// Create the two directories, ignoring errors
fs.mkdir(config.root, 0755, function (err) {
Expand Down Expand Up @@ -180,14 +180,16 @@ forever.list = function (format, procs) {
//
forever.cleanUp = function (cleanLogs) {
var emitter = new events.EventEmitter(),
processes = getAllProcesses();
processes = getAllProcesses(true);

if (cleanLogs) forever.cleanLogsSync();

if (processes) {
var checked = 0;
processes.forEach(function (proc) {
checkProcess(proc.pid, function (child) {
checkProcess(proc.foreverPid, function (manager) {
if (!child && !manager) {
if (!child && !manager || proc.dead) {
if (proc.pidFile) {
fs.unlink(proc.pidFile, function () {
// Ignore errors
Expand Down Expand Up @@ -220,6 +222,18 @@ forever.cleanUp = function (cleanLogs) {
return emitter;
};

//
// function cleanLogsSync ()
// Removes all log files from the
// root forever directory
//
forever.cleanLogsSync = function () {
var files = fs.readdirSync(config.root);
files.forEach(function (file) {
if (/\.log$/.test(file)) fs.unlinkSync(path.join(config.root, file));
});
};

//
// function randomString (bits)
// randomString returns a pseude-random ASCII string which contains at least
Expand Down Expand Up @@ -272,21 +286,36 @@ function formatProcess (proc, index, padding) {
};

//
// function getAllProcess ()
// function getAllProcess ([findDead])
// Returns all data for processes managed by forever
// [findDead]: Optional parameter that indicates to return dead procs
//
function getAllProcesses () {
function getAllProcesses (findDead) {
var processes = [];
try {
var files = fs.readdirSync(config.pidPath);
if (files.length === 0) return null;

files = files.filter(function(file) { return /\.fvr$/.test(file) });
files.forEach(function (file) {
var child = JSON.parse(fs.readFileSync(path.join(config.pidPath, file)));
processes.push(child);
var pidFiles = files.filter(function(file) { return /\.pid$/.test(file) });
pidFiles.forEach(function (file) {
var pidFile = path.join(config.pidPath, file),
pid = parseInt(fs.readFileSync(pidFile).toString());

try {
var child = JSON.parse(fs.readFileSync(path.join(config.pidPath, pid + '.fvr')));
processes.push(child);
}
catch (ex) {
if (findDead) {
processes.push({
pidFile: pidFile,
dead: true,
foreverPid: pid
});
}
}
});

return processes;
}
catch (ex) {
Expand Down Expand Up @@ -328,6 +357,8 @@ var Forever = function (file, options) {
this.command = options.command || 'node';
this.options = options.options || [];
this.max = options.max;
this.logFile = options.logFile;
this.pidFile = options.pidFile;
this.outFile = options.outFile;
this.errFile = options.errFile;

Expand Down

0 comments on commit 57850e9

Please sign in to comment.