From b5ce548e6b32ae84836d6166a3fa7ace1a513845 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 4 Jul 2012 17:18:58 -0700 Subject: [PATCH] [refactor] Finish refactor of core Monitor functionality into `forever-monitor` --- bin/forever | 19 +---- bin/monitor | 2 - examples/chroot.js | 14 ---- examples/cli-multiple-start | 6 +- lib/forever.js | 77 ++++--------------- lib/forever/service/service.js | 40 ++++------ package.json | 4 +- test/cli-test | 2 +- test/fixtures/.foreverignore | 3 - .../fixtures}/log-on-interval.js | 0 test/fixtures/logs.js | 5 -- {examples => test/fixtures}/server.js | 0 test/fixtures/start-daemon.js | 2 +- test/fixtures/watch/.foreverignore | 2 - test/fixtures/watch/daemon.js | 3 - test/fixtures/watch/file | 1 - test/fixtures/watch/removeMe | 0 test/worker/multiple-workers-test.js | 2 +- 18 files changed, 39 insertions(+), 143 deletions(-) delete mode 100644 examples/chroot.js delete mode 100644 test/fixtures/.foreverignore rename {examples => test/fixtures}/log-on-interval.js (100%) delete mode 100644 test/fixtures/logs.js rename {examples => test/fixtures}/server.js (100%) delete mode 100644 test/fixtures/watch/.foreverignore delete mode 100644 test/fixtures/watch/daemon.js delete mode 100644 test/fixtures/watch/file delete mode 100644 test/fixtures/watch/removeMe diff --git a/bin/forever b/bin/forever index 95420470..5190d32f 100755 --- a/bin/forever +++ b/bin/forever @@ -1,20 +1,3 @@ #!/usr/bin/env node -var fs = require('fs'); - -if (process.send) { - // - // Supress `stdout` and `stderr` when `forever` is being run as a fork. - // We use `child_process.fork` to daemonize `forever` processes, so - // outputting anything isn't a desired situation. - // - var nullStream = fs.createWriteStream('/dev/null'); - var returner = function () { - return nullStream; - }; - process.__defineGetter__('stdout', returner); - process.__defineGetter__('stderr', returner); -} - -var forever = require('../lib/forever'); -forever.cli.start(); +require('../lib/forever').cli.start(); diff --git a/bin/monitor b/bin/monitor index 7dc0e144..3ef4ba9e 100755 --- a/bin/monitor +++ b/bin/monitor @@ -2,8 +2,6 @@ var path = require('path'), forever = require(path.resolve(__dirname, '..', 'lib', 'forever')), started; - - process.on('message', function (data) { // TODO: Find out if this data will ever get split into two message events. var options = JSON.parse(data.toString()); diff --git a/examples/chroot.js b/examples/chroot.js deleted file mode 100644 index c688ae2d..00000000 --- a/examples/chroot.js +++ /dev/null @@ -1,14 +0,0 @@ -var util = require('util'), - daemon = require('daemon'), - fs = require('fs'); - -daemon.chroot('/tmp/chroot'); -util.puts('Working in chroot'); - -setInterval(function () { - fs.readdir('./', function (err, files) { - util.puts('Current directory: ' + process.cwd()); - util.puts('err: ' + util.inspect(err)); - util.puts('files: ' + util.inspect(files)); - }); -}, 10000 ); diff --git a/examples/cli-multiple-start b/examples/cli-multiple-start index 9a3e6bfe..02e2ce93 100755 --- a/examples/cli-multiple-start +++ b/examples/cli-multiple-start @@ -1,4 +1,4 @@ -forever start examples/server.js -p 8080 -forever start examples/server.js -p 8081 -forever start examples/server.js -p 8082 +forever start test/fixtures/server.js -p 8080 +forever start test/fixtures/server.js -p 8081 +forever start test/fixtures/server.js -p 8082 forever list \ No newline at end of file diff --git a/lib/forever.js b/lib/forever.js index 165aad3d..e0f14481 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -14,7 +14,6 @@ var fs = require('fs'), cliff = require('cliff'), nconf = require('nconf'), nssocket = require('nssocket'), - portfinder = require('portfinder'), psTree = require('ps-tree'), timespan = require('timespan'), utile = require('utile'), @@ -39,13 +38,14 @@ forever.log.cli(); // ### Export Components / Settings // Export `version` and important Prototypes from `lib/forever/*` // -forever.initialized = false; -forever.plugins = utile.requireDirLazy(path.join(__dirname, '/forever/plugins')); -forever.root = path.join(process.env.HOME || '/root', '.forever'); -forever.config = new nconf.File({ file: path.join(forever.root, 'config.json') }); -forever.Forever = forever.Monitor = require('./forever/monitor').Monitor; -forever.Worker = require('./forever/worker').Worker; -forever.cli = require('./forever/cli'); +forever.initialized = false; +forever.kill = require('forever-monitor').kill; +forever.checkProcess = require('forever-monitor').checkProcess; +forever.root = path.join(process.env.HOME || '/root', '.forever'); +forever.config = new nconf.File({ file: path.join(forever.root, 'config.json') }); +forever.Forever = forever.Monitor = require('forever-monitor').Monitor; +forever.Worker = require('./forever/worker').Worker; +forever.cli = require('./forever/cli'); // // Expose version through `pkginfo` @@ -359,6 +359,7 @@ forever.start = function (script, options) { if (!options.logFile) { options.logFile = forever.logFilePath(options.uid + '.log'); } + return new forever.Monitor(script, options).start(); }; @@ -376,14 +377,12 @@ forever.startDaemon = function (script, options) { var monitor, outFD, errFD, workerPath; -/* - * This log file is forever's log file - the user's outFile and errFile - * options are not taken into account here. This will be an aggregate of all - * the app's output, as well as messages from the monitor process, where - * applicable. - * - */ - + // + // This log file is forever's log file - the user's outFile and errFile + // options are not taken into account here. This will be an aggregate of all + // the app's output, as well as messages from the monitor process, where + // applicable. + // outFD = fs.openSync(options.logFile, 'a'); errFD = fs.openSync(options.logFile, 'a'); monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor'); @@ -761,52 +760,6 @@ forever.pidFilePath = function (pidFile) { : path.join(forever.config.get('pidPath'), pidFile); }; -// -// ### function checkProcess (pid, callback) -// #### @pid {string} pid of the process to check -// #### @callback {function} Continuation to pass control backto. -// Utility function to check to see if a pid is running -// -forever.checkProcess = function (pid) { - if (!pid) { - return false; - } - - try { - // - // Trying to kill non-existent process here raises a ESRCH - no such - // process exception. Also, signal 0 doesn't do no harm to a process - it - // only checks if sending a singal to a given process is possible. - // - process.kill(pid, 0); - return true; - } - catch (err) { - return false; - } -}; - -forever.kill = function(pid, killTree, callback) { - if (killTree) { - psTree(pid, function (err, children) { - var pids = children.map(function (p) { - return p.PID; - }); - - pids.unshift(pid); - spawn('kill', ['-9'].concat(pids)).on('exit', callback || function() {}); - }); - } - else { - try { - process.kill(pid); - } - catch (ex) { } - callback && callback(); - } - -} - // // ### @columns {Object} // Property descriptors for accessing forever column information diff --git a/lib/forever/service/service.js b/lib/forever/service/service.js index 017c6c6c..2cc878dd 100644 --- a/lib/forever/service/service.js +++ b/lib/forever/service/service.js @@ -10,7 +10,6 @@ var fs = require('fs'), path = require('path'), util = require('util'), events = require('events'), - portfinder = require('portfinder'), forever = require('../../forever'), SystemVAdapter = require('./adapters/systemv'); @@ -49,33 +48,26 @@ Service.prototype.startServer = function startServer(callback) { server; server = dnode(this); + server.on('error', function onServerError() { + // + // TODO: This is really bad. + // + }); - portfinder.getSocket({ path: socket }, function onSocketFound(err, socket) { - if (err) { - return callback(err); - } - - server.on('error', function onServerError() { - // - // TODO: This is really bad. - // - }); - - server.on('ready', function onServerReady(err) { - self.listen(server); - if (callback) { - if (err) { - callback(err); - } - else { - callback(null, server, socket); - } + server.on('ready', function onServerReady(err) { + self.listen(server); + if (callback) { + if (err) { + callback(err); } - }); - - server.listen(socket); + else { + callback(null, server, socket); + } + } }); + server.listen(socket); + return this; }; diff --git a/package.json b/package.json index 5525948d..75624eee 100644 --- a/package.json +++ b/package.json @@ -20,22 +20,20 @@ "tools" ], "dependencies": { - "broadway": "0.1.x", "cliff": "0.x.x", "flatiron": "0.2.x", "forever-monitor": "x.x.x", - "minimatch": "0.2.x", "nconf": "0.5.x", "nssocket": "0.3.x", "optimist": "0.3.x", "pkginfo": "0.x.x", - "ps-tree": "0.0.x", "timespan": "2.0.x", "watch": "0.5.x", "utile": "0.1.x", "winston": "0.6.x" }, "devDependencies": { + "broadway": "0.2.x", "eventemitter2": "0.4.x", "request": "2.x.x", "vows": "0.6.x" diff --git a/test/cli-test b/test/cli-test index fa69f868..13cb8165 100755 --- a/test/cli-test +++ b/test/cli-test @@ -10,7 +10,7 @@ # Yes, we have tests in bash. How mad science is that? alias forever=bin/forever -script="examples/log-on-interval.js" +script="test/fixtures/log-on-interval.js" function fail { echo "\033[31m ✘ $1\033[0m" diff --git a/test/fixtures/.foreverignore b/test/fixtures/.foreverignore deleted file mode 100644 index aaa789d1..00000000 --- a/test/fixtures/.foreverignore +++ /dev/null @@ -1,3 +0,0 @@ -node_modules -ignore* - diff --git a/examples/log-on-interval.js b/test/fixtures/log-on-interval.js similarity index 100% rename from examples/log-on-interval.js rename to test/fixtures/log-on-interval.js diff --git a/test/fixtures/logs.js b/test/fixtures/logs.js deleted file mode 100644 index 2b74caa0..00000000 --- a/test/fixtures/logs.js +++ /dev/null @@ -1,5 +0,0 @@ - -for (var i = 0; i < 10; i++) { - console.log('stdout %d', i); - console.error('stderr %d', i); -} diff --git a/examples/server.js b/test/fixtures/server.js similarity index 100% rename from examples/server.js rename to test/fixtures/server.js diff --git a/test/fixtures/start-daemon.js b/test/fixtures/start-daemon.js index 29d64da9..9bf8eb64 100644 --- a/test/fixtures/start-daemon.js +++ b/test/fixtures/start-daemon.js @@ -9,7 +9,7 @@ var path = require('path'), forever = require('../../lib/forever'); -var monitor = forever.startDaemon(path.join(__dirname, '..', '..', 'examples', 'log-on-interval.js')); +var monitor = forever.startDaemon(path.join(__dirname, 'log-on-interval.js')); monitor.on('start', function () { forever.startServer(monitor); diff --git a/test/fixtures/watch/.foreverignore b/test/fixtures/watch/.foreverignore deleted file mode 100644 index af9c50f5..00000000 --- a/test/fixtures/watch/.foreverignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules -ignore* diff --git a/test/fixtures/watch/daemon.js b/test/fixtures/watch/daemon.js deleted file mode 100644 index 2f137555..00000000 --- a/test/fixtures/watch/daemon.js +++ /dev/null @@ -1,3 +0,0 @@ -console.log('Hello!'); -setTimeout(process.exit, 2000); - diff --git a/test/fixtures/watch/file b/test/fixtures/watch/file deleted file mode 100644 index 182a60a8..00000000 --- a/test/fixtures/watch/file +++ /dev/null @@ -1 +0,0 @@ -/* hello, I know nodejitsu. */ \ No newline at end of file diff --git a/test/fixtures/watch/removeMe b/test/fixtures/watch/removeMe deleted file mode 100644 index e69de29b..00000000 diff --git a/test/worker/multiple-workers-test.js b/test/worker/multiple-workers-test.js index 6cea38fb..dca1685a 100644 --- a/test/worker/multiple-workers-test.js +++ b/test/worker/multiple-workers-test.js @@ -40,7 +40,7 @@ vows.describe('forever/workers/multiple').addBatch({ "and spawning two processes using the same script": { topic: function () { var that = this, - script = path.join(__dirname, '..', '..', 'examples', 'server.js'); + script = path.join(__dirname, '..', 'fixtures', 'server.js'); children[0] = new (forever.Monitor)(script, { silent: true,