Skip to content

Commit

Permalink
[api] Added restart command to both forever.Monitor and CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Feb 14, 2011
1 parent c073c47 commit fd1b9a6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
29 changes: 23 additions & 6 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var help = [
" -l LOGFILE Logs the forever output to LOGFILE",
" -o OUTFILE Logs stdout from child script to OUTFILE",
" -e ERRFILE Logs stderr from child script to ERRFILE",
" -d SOURCEDIR The source directory for which SCRIPT is relative to",
" -p PATH Base path for all forever related files (pid files, etc.)",
" -c COMMAND COMMAND to execute (defaults to node)",
" -s, --silent Run the child script silencing stdout and stderr",
Expand All @@ -54,6 +55,7 @@ var mappings = {
'l': 'logFile',
'p': 'path',
'c': 'command',
'd': 'sourceDir',
's': 'silent',
'silent': 'silent',
'o': 'outFile',
Expand Down Expand Up @@ -82,7 +84,7 @@ var options = {};
options.options = process.argv.splice(process.argv.indexOf(file)).splice(1);

// Now that we've removed the target script options
// reparse the options and setup the forever settings
// reparse the options and configure the forever settings
argv = require('optimist')(process.argv).argv;
Object.keys(argv).forEach(function (key) {
if (mappings[key]) {
Expand All @@ -95,19 +97,24 @@ if (typeof options['max'] === 'undefined') {
options.forever = true;
}

// Set the sourceDir of the options for graceful
// restarting outside of the main directory
if (!options.sourceDir) options.sourceDir = process.cwd();

// Setup configurations for forever
var config = {
root: argv.p || forever.path
};

function tryStart (callback) {
var fullPath, uid = forever.randomString(16);
var fullLog, fullScript, uid = forever.randomString(16);
options.uid = uid;
options.pidFile = 'forever' + uid + '.pid';
options.logFile = argv.l || 'forever' + uid + '.log';
fullPath = path.join(forever.config.root, options.logFile);
fullLog = path.join(forever.config.root, options.logFile);
fullScript = path.join(options.sourceDir, file);

forever.stat(fullPath, file, function (err) {
forever.stat(fullLog, fullScript, function (err) {
if (err) {
sys.puts('Cannot start forever: ' + err.message);
process.exit(0);
Expand All @@ -123,11 +130,21 @@ eyes.inspect(config)

var loader = forever.load(config);
loader.on('load', function () {
sys.puts('Loaded forever. Cleaning logs...');
sys.puts('Loaded forever. Tidying /tmp/forever.');
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
sys.puts('Cleaned logs. Running action: ' + action.yellow);
sys.puts('/tmp/forever tidied.');
if (file) {
sys.puts('Working with file: ' + file);
}

if (options) {
sys.puts('Using forever options:');
eyes.inspect(options);
}

if (action) {
sys.puts('Running action: ' + action.yellow);
switch (action) {
case 'start':
tryStart(function () { forever.startDaemon(file, options); });
Expand Down
5 changes: 3 additions & 2 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ forever.restart = function (target, format) {
var restartCommand = [
'forever',
'start',
'-d', proc.sourceDir,
// Remark: Is using a new logfile good behavior?
//'-l', path.basename(proc.logFile),
proc.file,
proc.options.join(' ')
].join(' ');

exec(restartCommand, function (err, stdout, stderr) {
next();
});
Expand Down Expand Up @@ -277,7 +278,7 @@ forever.list = function (format, procs) {
//
// ### function cleanUp ()
// Utility function for removing excess pid and
// config files used by forever.
// config, and log files used by forever.
//
forever.cleanUp = function (cleanLogs) {
var emitter = new events.EventEmitter(),
Expand Down
27 changes: 18 additions & 9 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ var sys = require('sys'),
var Monitor = exports.Monitor = function (script, options) {
events.EventEmitter.call(this);

this.silent = options.silent || false;
this.forever = options.forever || false;
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;
this.silent = options.silent || false;
this.forever = options.forever || false;
this.command = options.command || 'node';
this.sourceDir = options.sourceDir;
this.options = options.options || [];
this.max = options.max;
this.logFile = options.logFile;
this.pidFile = options.pidFile;
this.outFile = options.outFile;
this.errFile = options.errFile;

this.childExists = false;

Expand All @@ -42,6 +43,10 @@ var Monitor = exports.Monitor = function (script, options) {
this.options.unshift(script);
}

if (this.sourceDir) {
this.options[0] = path.join(this.sourceDir, this.options[0]);
}

// If we should log stdout, open a file buffer
if (this.outFile) {
this.stdout = fs.createWriteStream(this.outFile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
Expand Down Expand Up @@ -177,6 +182,10 @@ Monitor.prototype.save = function () {
if (this.pidFile) childData.pidFile = this.pidFile;
if (this.outFile) childData.outFile = this.outFile;
if (this.errFile) childData.errFile = this.errFile;
if (this.sourceDir) {
childData.sourceDir = this.sourceDir;
childData.file = childData.file.replace(this.sourceDir + '/', '');
}

var childPath = path.join(forever.config.pidPath, childData.foreverPid + '.fvr');
fs.writeFile(childPath, JSON.stringify(childData), function (err) {
Expand Down

0 comments on commit fd1b9a6

Please sign in to comment.