From 686d009f332fb1e7e422ecb8d1efe95675f7de18 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 22 Jun 2011 00:46:40 -0400 Subject: [PATCH] [minor api] Added forever.debug for debugging purposes --- lib/forever.js | 69 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/lib/forever.js b/lib/forever.js index 515af7bc..141b5b5e 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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; @@ -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'); @@ -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'); // @@ -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 @@ -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); }); }); }; @@ -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 @@ -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) { @@ -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 };