Skip to content

Commit

Permalink
[minor api] Added forever.debug for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jun 22, 2011
1 parent abed353 commit 686d009
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var fs = require('fs'),
colors = require('colors'),
daemon = require('daemon'),
nconf = require('nconf'),
timespan = require('timespan');
timespan = require('timespan'),
winston = require('winston');

var forever = exports;

Expand All @@ -23,6 +24,7 @@ var forever = exports;
// 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');
Expand All @@ -43,7 +45,7 @@ forever.load = function (options) {
// Setup the incoming options with default options.
//
options = options || {};
options.root = options.root || forever.root,
options.root = options.root || forever.root;
options.pidPath = options.pidPath || path.join(options.root, 'pids');

//
Expand All @@ -67,6 +69,25 @@ forever.load = function (options) {
forever.config.set('root', options.root);
forever.config.set('pidPath', options.pidPath);

//
// Attempt to see if `forever` has been configured to
// run in debug mode.
//
options.debug = options.debug || forever.config.get('debug') || false;

if (options.debug) {
//
// 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') })
]
});
}

//
// Syncronously create the `root` directory
// and the `pid` directory for forever. Although there is
Expand Down Expand Up @@ -105,28 +126,22 @@ forever.load();
// the target script does exist before executing callback.
//
forever.stat = function (logFile, script, callback) {
var logAppend,
realCallback = callback;
var logAppend;

if (arguments.length === 4) {
logAppend = callback;
realCallback = arguments[3];
callback = arguments[3];
}

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

fs.stat(logFile, function (err, stats) {
if (!err) {
return realCallback(new Error('log file ' + logFile + ' exists.'));
}

realCallback(null);
return logAppend ? callback(null) : fs.stat(logFile, function (err, stats) {
return !err
? callback(new Error('log file ' + logFile + ' exists.'))
: callback(null);
});
});
};
Expand Down Expand Up @@ -546,6 +561,25 @@ 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 @@ -572,7 +606,8 @@ function formatProcess (proc, index, padding) {
// Returns all data for processes managed by forever.
//
function getAllProcesses (findDead) {
var results = [], processes = {},
var results = [],
processes = {},
files = fs.readdirSync(forever.config.get('pidPath'));

if (files.length === 0) {
Expand All @@ -589,7 +624,7 @@ function getAllProcesses (findDead) {
switch (ext) {
case '.pid':
var pid = parseInt(data);
if (!processes[uid]) processes[uid] = {
processes[uid] = processes[uid] || {
foreverPid: pid,
uid: uid
};
Expand Down

0 comments on commit 686d009

Please sign in to comment.