From e0c3dcf9894f84c395358c9b466336390c385299 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 15 Jun 2011 07:15:31 -0400 Subject: [PATCH] [dist] Minor style updates. Update to use pkginfo --- inspect-emitters.js | 54 ++++++++++++++++++++++++ lib/forever.js | 94 ++++++++++++++++++++++++------------------ lib/forever/cli.js | 2 +- lib/forever/monitor.js | 2 +- package.json | 5 ++- 5 files changed, 113 insertions(+), 44 deletions(-) create mode 100644 inspect-emitters.js diff --git a/inspect-emitters.js b/inspect-emitters.js new file mode 100644 index 00000000..cd57bffd --- /dev/null +++ b/inspect-emitters.js @@ -0,0 +1,54 @@ +(function (global, undefined) { + "use strict"; + + var path = require('path'); + + console.log("nodejs module path: ", require.paths); + console.log(""); + + //get the forever module + var forever = require("./lib/forever"), + emitter = path.join(__dirname, 'examples', 'server.js'), + child; + + console.log(""); + console.log("get the list:"); + console.log("forever.list(true)", forever.list(true)); + console.log("forever.list(false).length", (forever.list(false) || []).length); + + console.log(""); + console.log("Start emitter.js"); + + child = new (forever.Forever)(emitter, { options: ['-p', '8080'] }); + + //extend + child.__old__emit = child.emit; + child.emit = function () { + var eventname = arguments[0], + list; + + if (eventname != "newListener") { + list = forever.list(false) || []; + console.log(":: emiting: " + arguments[0]); + console.log(":: processors: ", list.length); + } + + //console.dir(new Error().stack.split('\n')); + return this.__old__emit.apply(this, arguments); + }; + + child.on("start", function () { + console.log("> emitter.js emits start"); + console.log("> > forever.list(true)", forever.list(true)); + console.log("> > forever.list(false).length", (forever.list(false) || []).length); + console.log("> stop emmiter.js"); + child.stop(); + }); + child.on("stop", function () { + console.log("> emitter.js emits stop"); + console.log("> > forever.list(true)", forever.list(true)); + console.log("> > forever.list(false).length", (forever.list(false) || []).length); + }); + child.start(); + +})(global); \ No newline at end of file diff --git a/lib/forever.js b/lib/forever.js index 2fa5a06b..7f9fb1ed 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -6,17 +6,15 @@ * */ -require.paths.unshift(__dirname); - var fs = require('fs'), - colors = require('colors'), - async = require('async'), path = require('path'), events = require('events'), exec = require('child_process').exec, - timespan = require('timespan'), + async = require('async'), + colors = require('colors'), + daemon = require('daemon'), nconf = require('nconf'), - daemon = require('daemon'); + timespan = require('timespan'); var forever = exports; @@ -24,12 +22,16 @@ var forever = exports; // ### Export Components / Settings // Export `version` and important Prototypes from `lib/forever/*` // -forever.version = [0, 4, 0]; forever.initialized = 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.cli = require('./forever/cli'); +forever.Forever = forever.Monitor = require('./forever/monitor').Monitor; + +// +// Expose version through `pkginfo` +// +require('pkginfo')(module, 'version'); // // ### function load (options, [callback]) @@ -59,9 +61,7 @@ forever.load = function (options) { // Try to load the forever `config.json` from // the specified location. // - try { - forever.config.loadSync(); - } + try { forever.config.loadSync() } catch (ex) { } forever.config.set('root', options.root); @@ -74,7 +74,7 @@ forever.load = function (options) { // the setup of forever dramatically. // function tryCreate (dir) { - try { fs.mkdirSync(dir, 0755); } + try { fs.mkdirSync(dir, 0755) } catch (ex) { } } @@ -84,9 +84,7 @@ forever.load = function (options) { // // Attempt to save the new `config.json` for forever // - try { - forever.config.saveSync(); - } + try { forever.config.saveSync() } catch (ex) { } forever.initialized = true; @@ -116,15 +114,18 @@ forever.stat = function (logFile, script, callback) { } fs.stat(script, function (err, stats) { - if (err) return realCallback(new Error('script ' + script + ' does not exist.')); - - if (logAppend) { - realCallback(null); - return; + if (err) { + return realCallback(new Error('script ' + script + ' does not exist.')); + } + else if (logAppend) { + return realCallback(null); } fs.stat(logFile, function (err, stats) { - if (!err) return realCallback(new Error('log file ' + logFile + ' exists.')); + if (!err) { + return realCallback(new Error('log file ' + logFile + ' exists.')); + } + realCallback(null); }); }); @@ -149,10 +150,13 @@ forever.start = function (script, options) { forever.startDaemon = function (script, options) { options.logFile = forever.logFilePath(options.logFile); options.pidFile = forever.pidFilePath(options.pidFile); + var runner = new forever.Monitor(script, options); fs.open(options.logFile, options.appendLog ? 'a+' : 'w+', function (err, fd) { - if (err) return runner.emit('error', err); + if (err) { + return runner.emit('error', err); + } var pid = daemon.start(fd); daemon.lock(options.pidFile); @@ -184,8 +188,9 @@ forever.stop = function (target, format, restart) { processes = getAllProcesses(), results = []; - var procs = /(\d+)/.test(target) ? forever.findByIndex(target, processes) - : forever.findByScript(target, processes); + var procs = /(\d+)/.test(target) + ? forever.findByIndex(target, processes) + : forever.findByScript(target, processes); if (procs && procs.length > 0) { procs.forEach(function (proc) { @@ -336,7 +341,9 @@ forever.list = function (format, procs) { var formatted = []; procs = procs || getAllProcesses(); - if (!procs) return null; + if (!procs) { + return null; + } if (format) { var index = 0, maxLen = 0; @@ -454,6 +461,7 @@ forever.randomString = function (bits) { ret+=chars[0x3F & rand >>> i]; } } + return ret; }; @@ -463,11 +471,9 @@ forever.randomString = function (bits) { // Determines the full logfile path name // forever.logFilePath = function(logFile, uid) { - if (logFile && logFile[0] === '/') { - return logFile; - } else { - return path.join(forever.config.get('root'), logFile || (uid || 'forever') + '.log'); - } + return logFile && logFile[0] === '/' + ? logFile + : path.join(forever.config.get('root'), logFile || (uid || 'forever') + '.log'); }; // @@ -476,11 +482,9 @@ forever.logFilePath = function(logFile, uid) { // Determines the full pid file path name // forever.pidFilePath = function(pidFile) { - if (pidFile && pidFile[0] === '/') { - return pidFile; - } else { - return path.join(forever.config.get('pidPath'), pidFile); - } + return pidFile && pidFile[0] === '/' + ? pidFile + : path.join(forever.config.get('pidPath'), pidFile); }; // @@ -495,7 +499,10 @@ function checkProcess (pid, callback) { } exec('ps ' + pid + ' | grep -v PID', function (err, stdout, stderr) { - if (err) return callback(false); + if (err) { + return callback(false); + } + callback(stdout.indexOf(pid) !== -1); }); }; @@ -529,7 +536,9 @@ function getAllProcesses (findDead) { var results = [], processes = {}, files = fs.readdirSync(forever.config.get('pidPath')); - if (files.length === 0) return null; + if (files.length === 0) { + return null; + } files.forEach(function (file) { try { @@ -562,8 +571,13 @@ function getAllProcesses (findDead) { }); Object.keys(processes).forEach(function (key) { - if (!processes[key].pid && !findDead) return; - else if (!processes[key].pid) processes[key].dead = true; + if (!processes[key].pid && !findDead) { + return; + } + else if (!processes[key].pid) { + processes[key].dead = true; + } + results.push(processes[key]); }); diff --git a/lib/forever/cli.js b/lib/forever/cli.js index 917854e2..11376489 100644 --- a/lib/forever/cli.js +++ b/lib/forever/cli.js @@ -10,7 +10,7 @@ var sys = require('sys'), path = require('path'), eyes = require('eyes'), winston = require('winston'), - forever = require('forever'); + forever = require('../forever'); var cli = exports; diff --git a/lib/forever/monitor.js b/lib/forever/monitor.js index cb0c51ba..f2146498 100644 --- a/lib/forever/monitor.js +++ b/lib/forever/monitor.js @@ -12,7 +12,7 @@ var sys = require('sys'), events = require('events'), spawn = require('child_process').spawn, winston = require('winston'), - forever = require('forever'); + forever = require('../forever'); // // ### function Monitor (script, options) diff --git a/package.json b/package.json index a97f7117..cbca6252 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,11 @@ "colors": "0.x.x", "eyes": "0.1.x", "daemon": "0.3.x", + "nconf": "0.x.x", "optimist": "0.2.x", + "pkginfo": "0.x.x", "timespan": "2.0.x", - "winston": "0.3.x", - "nconf": "0.x.x" + "winston": "0.3.x" }, "devDependencies": { "vows": "0.5.x"