Skip to content

Commit

Permalink
[fix] Better handling of bookkeeping of *.fvr and *.pid files. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Apr 29, 2011
1 parent 864b1d1 commit 9788748
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
22 changes: 18 additions & 4 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,43 @@ var mappings = {
'verbose': 'verbose'
};

//
// If we are passed more than one non-hyphenated
// options, only use the first one. Assume the
// rest are pass-through for the child process
//
var file = argv._[0], options = {};

// Setup pass-thru options for child-process
if (file) {
//
// Setup pass-thru options for child-process
//
options.options = process.argv.splice(process.argv.indexOf(file)).splice(1);
}

//
// Now that we've removed the target script options
// reparse the options and configure the forever settings
//
argv = require('optimist')(process.argv).boolean(['v', 'verbose', 'a', 'append', 's', 'silent']).argv;
Object.keys(argv).forEach(function (key) {
if (mappings[key] && argv[key]) {
options[mappings[key]] = argv[key];
}
});

// If max isn't specified set it to run forever
if (typeof options['max'] === 'undefined') {
//
// If max isn't specified set it to run forever
//
options.forever = true;
}

// Set the sourceDir of the options for graceful
// restarting outside of the main directory
if (!options.sourceDir) {
//
// Set the sourceDir of the options for graceful
// restarting outside of the main directory
//
options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/';
}

Expand All @@ -137,7 +147,9 @@ options.logFile = argv.l || uid + '.log';
}
});

//
// Pass the source dir to spawn
//
options.spawnWith = {
cwd: options.sourceDir
};
Expand All @@ -151,7 +163,9 @@ if (options.verbose) {
winston.defaultTransports().console.level = 'silly';
}

//
// Setup configurations for forever
//
var config = {
root: argv.p
};
Expand Down
49 changes: 30 additions & 19 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,20 @@ forever.restart = function (target, format) {
'forever',
'start',
'-d', proc.sourceDir,
// Remark: Is using a new logfile good behavior?
//'-l', path.basename(proc.logFile),
proc.file,
proc.options.join(' ')
].join(' ');
'-l', proc.logFile,
'--append'
];

exec(restartCommand, function (err, stdout, stderr) {
if (proc.outFile) {
restartCommand.push('-o', path.join(proc.sourceDir, proc.outFile));
}

if (proc.errFile) {
restartCommand.push('-e', path.join(proc.sourceDir, proc.outFile));
}

restartCommand.push(proc.file, proc.options.join(' '));
exec(restartCommand.join(' '), function (err, stdout, stderr) {
next();
});
}, function () {
Expand Down Expand Up @@ -365,17 +372,13 @@ forever.cleanUp = function (cleanLogs) {
checkProcess(proc.pid, function (child) {
checkProcess(proc.foreverPid, function (manager) {
if (!child && !manager || proc.dead) {
if (proc.pidFile) {
fs.unlink(proc.pidFile, function () {
fs.unlink(path.join(forever.config.get('pidPath'), proc.uid + '.fvr'), function () {
fs.unlink(path.join(forever.config.get('pidPath'), proc.uid + '.pid'), function () {
// Ignore errors
});
}

fs.unlink(path.join(forever.config.get('pidPath'), proc.uid + '.fvr'), function () {
// Ignore errors
});

if (cleanLogs) {
if (cleanLogs && proc.logFile) {
fs.unlink(proc.logFile, function () { /* Ignore Errors */ });
}
}
Expand Down Expand Up @@ -510,22 +513,30 @@ function getAllProcesses (findDead) {
files.forEach(function (file) {
try {
var fullPath = path.join(forever.config.get('pidPath'), file),
ext = path.extname(file),
uid = file.replace(ext, ''),
data = fs.readFileSync(fullPath).toString();

switch (path.extname(file)) {
//case '.pid':
// var pid = parseInt(data);
// if (!processes[pid]) processes[pid] = { foreverPid: pid };
// break;
switch (ext) {
case '.pid':
var pid = parseInt(data);
if (!processes[uid]) processes[uid] = {
foreverPid: pid,
uid: uid
};
break;

case '.fvr':
var child = JSON.parse(data);
processes[child.pid] = child;
processes[uid] = child;
break;
}
}
catch (ex) {
// Ignore errors
processes[uid] = {
uid: uid
};
}
});

Expand Down
16 changes: 13 additions & 3 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,27 @@ Monitor.prototype.start = function (restart) {

// Hook all stream data and process it
function listenTo (stream) {
child[stream].on('data', function (data) {
child[stream].on('data', function ldata (data) {
if (!self.silent && !self[stream]) {
//
// If we haven't been silenced, and we don't have a file stream
// to output to write to the process stdout stream
//
process.stdout.write(data);
}
else if (self[stream]) {
//
// If we have been given an output file for the stream, write to it
//
self[stream].write(data);
}

self.emit(stream, data);
});

child.on('exit', function () {
child[stream].removeListener('data', ldata);
});
}

// Listen to stdout and stderr
Expand All @@ -129,7 +137,7 @@ Monitor.prototype.start = function (restart) {

child.on('exit', function (code) {
var spinning = Date.now() - self.ctime < self.minUptime;
self.error('Forever detected script exited with code: ' + code);
self.warn('Forever detected script exited with code: ' + code);
self.times++;

if ((self.forever || self.times < self.max) && !self.forceStop && !spinning) {
Expand Down Expand Up @@ -269,7 +277,9 @@ Monitor.prototype.kill = function (forceStop) {
// fires in `Monitor.prototype.start` we can short circuit
// and prevent auto-restart
//
if (forceStop) this.forceStop = true;
if (forceStop) {
this.forceStop = true;
}

this.child.kill();
this.emit('stop', this.childData);
Expand Down
8 changes: 0 additions & 8 deletions out.log

This file was deleted.

0 comments on commit 9788748

Please sign in to comment.