Skip to content

Commit

Permalink
[minor api] Update to optional debugging. Various small style updates
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jun 22, 2011
1 parent 686d009 commit f11610e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 83 deletions.
56 changes: 40 additions & 16 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ var path = require('path'),
sys = require('sys'),
forever = require('./../lib/forever');

var accepts = ['start', 'stop', 'stopall', 'list', 'config', 'clear', 'set', 'cleanlogs', 'restart'], action;
var action, accepts = [
'start',
'stop',
'stopall',
'list',
'config',
'clear',
'set',
'cleanlogs',
'restart'
];

if (accepts.indexOf(process.argv[2]) !== -1) {
action = process.argv.splice(2,1)[0];
}
Expand Down Expand Up @@ -34,13 +45,14 @@ 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)',
' --pidfile The pid file',
' -a, --append Append logs',
' --pidfile The pid file',
' --sourceDir The source directory for which SCRIPT is relative to',
' --minUptime Minimum uptime (millis) for a script to not be considered "spinning"',
' --spinSleepTime Time to wait (millis) between launches of a spinning script. (defaults to killing a spinning script).',
' --spinSleepTime Time to wait (millis) between launches of a spinning script.',
' -d, --debug Forces forever to log debug output',
' -v, --verbose Turns on the verbose messages from Forever',
' -s, --silent Run the child script silencing stdout and stderr',
' -h, --help You\'re staring at it',
Expand Down Expand Up @@ -68,14 +80,12 @@ function isSimpleAction () {
//
if (argv.h || argv.help || (argv._.length === 0 && !isSimpleAction())
&& (!argv.c && !argv.command)) {
sys.puts(help);
return;
return sys.puts(help);
}

var mappings = {
'c': 'command',
'e': 'errFile',
'd': 'sourceDir',
'l': 'logFile',
'a': 'appendLog',
'append': 'appendLog',
Expand All @@ -85,10 +95,13 @@ var mappings = {
'pidfile': 'pidFile',
's': 'silent',
'silent': 'silent',
'sourceDir': 'sourceDir',
'minUptime': 'minUptime',
'spinSleepTime': 'spinSleepTime',
'v': 'verbose',
'verbose': 'verbose'
'verbose': 'verbose',
'd': 'debug',
'debug': 'debug'
};

//
Expand Down Expand Up @@ -127,10 +140,10 @@ if (typeof options['max'] === 'undefined') {
}

if (typeof options['minUptime'] !== 'undefined') {
options['minUptime'] = parseFloat(options['minUptime']);
options['minUptime'] = parseFloat(options['minUptime']);
}
if (typeof options['spinSleepTime'] !== 'undefined') {
options['spinSleepTime'] = parseFloat(options['spinSleepTime']);
options['spinSleepTime'] = parseFloat(options['spinSleepTime']);
}

if (!options.sourceDir) {
Expand Down Expand Up @@ -169,10 +182,8 @@ options.spawnWith = {
//
// Configure winston for forever based on the CLI options
//
winston.defaultTransports().console.timestamp = false;
winston.defaultTransports().console.colorize = true;
if (options.verbose) {
winston.defaultTransports().console.level = 'silly';
forever.log.transports.console.level = 'silly';
}

//
Expand All @@ -186,12 +197,25 @@ var config = {
// Only call `forever.load()` if the root path is different than
// the default root exposed by forever.
//
if (config.root && config.root !== forever.root) {
winston.silly('Loading forever with config: ', config);
if ((config.root && config.root !== forever.root)) {
forever.log.silly('Loading forever with config: ', config);
forever.load(config);
winston.silly('Loaded forever successfully.');
forever.log.silly('Loaded forever successfully.');
}

//
// If we should debug this forever process then
// configure it to do so.
//
if (argv.d || argv.debug) {
forever.log.warn('Forever debugging enabled');
forever._debug();
}

//
// If this is a set action then get the first
// value from the options and continue.
//
if (action === 'set') {
options = options.options[0];
}
Expand Down
66 changes: 32 additions & 34 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@ var fs = require('fs'),

var forever = exports;

//
// Setup `forever.log` to be a custom `winston` logger.
//
forever.log = new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
});

forever.log.cli();

//
// ### Export Components / Settings
// Export `version` and important Prototypes from `lib/forever/*`
//
forever.initialized = false;
forever._debug = false;
forever.root = path.join(process.env.HOME, '.forever');
forever.config = new nconf.stores.File({ file: path.join(forever.root, 'config.json') });
forever.cli = require('./forever/cli');
forever.Forever = forever.Monitor = require('./forever/monitor').Monitor;
forever.Forever = forever.Monitor = require('./forever/monitor').Monitor;
forever.cli = require('./forever/cli');

//
// Expose version through `pkginfo`
Expand Down Expand Up @@ -80,12 +90,7 @@ forever.load = function (options) {
// If we have been indicated to debug this forever process
// then setup `forever._debug` to be an instance of `winston.Logger`.
//
forever.config.set('debug', options.debug);
forever._debug = new (winston.Logger)({
transports: [
new (winston.transports.File)({ file: path.join(options.root, 'forever.debug.log') })
]
});
forever._debug();
}

//
Expand All @@ -111,6 +116,17 @@ forever.load = function (options) {
forever.initialized = true;
};

//
// ### @private function _debug ()
// Sets up debugging for this forever process
//
forever._debug = function () {
forever.config.set('debug', true);
forever.log.add(winston.transports.File, {
filename: path.join(forever.config.get('root'), 'forever.debug.log')
});
}

//
// Ensure forever will always be loaded the first time it is required.
//
Expand All @@ -135,12 +151,12 @@ forever.stat = function (logFile, script, callback) {

fs.stat(script, function (err, stats) {
if (err) {
return callback(new Error('script ' + script + ' does not exist.'));
return callback(new Error('Script ' + script + ' does not exist.'));
}

return logAppend ? callback(null) : fs.stat(logFile, function (err, stats) {
return !err
? callback(new Error('log file ' + logFile + ' exists.'))
? callback(new Error('Log file ' + logFile + ' exists.'))
: callback(null);
});
});
Expand Down Expand Up @@ -367,7 +383,9 @@ forever.list = function (format, procs) {
// Iterate over the procs to see which has the longest options string
procs.forEach(function (proc) {
proc.length = [proc.command || 'node', proc.file].concat(proc.options).join(' ').length;
if (proc.length > maxLen) maxLen = proc.length;
if (proc.length > maxLen) {
maxLen = proc.length;
}
});

procs.forEach(function (proc) {
Expand Down Expand Up @@ -463,8 +481,7 @@ forever.cleanUp = function (cleanLogs, allowManager) {
? cleanBatch(processes.splice(0, 10))
: emitter.emit('cleanUp');
});
})(processes.splice(0, 10));

})(processes.splice(0, 10));
}
else {
process.nextTick(function () {
Expand Down Expand Up @@ -561,25 +578,6 @@ forever.checkProcess = function (pid, callback) {
});
};

//
// ### function debug (msg, meta, callback)
// #### @msg {string} Message to log
// #### @meta {Object} **Optional** Additional metadata to log.
// #### @callback
//
forever.debug = function (msg, meta, callback) {
if (!callback && typeof meta === 'function') {
callback = meta;
meta = {};
}

if (!forever._debug) {
return callback && callback();
}

forever._debug.info(msg, meta, callback);
};

//
// ### function formatProcess (proc index, padding)
// #### @proc {Object} Process to format
Expand All @@ -592,7 +590,7 @@ function formatProcess (proc, index, padding) {
var command = proc.command || 'node';

// Create an array of the output we can later join
return [' [' + index + ']', command.grey, proc.file.grey]
return ['[' + index + ']', command.grey, proc.file.grey]
.concat(proc.options.map(function (opt) { return opt.grey }))
.concat([padding + '[' + proc.pid + ',', proc.foreverPid + ']'])
.concat(proc.logFile ? proc.logFile.magenta : '')
Expand Down
Loading

0 comments on commit f11610e

Please sign in to comment.