Permalink
Browse files

[refactor] Finish refactor of core Monitor functionality into `foreve…

…r-monitor`
  • Loading branch information...
1 parent 5225d68 commit b5ce548e6b32ae84836d6166a3fa7ace1a513845 @indexzero indexzero committed Jul 5, 2012
View
@@ -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();
View
@@ -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());
View
@@ -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 );
@@ -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
View
@@ -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
@@ -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;
};
View
@@ -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"
View
@@ -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"
@@ -1,3 +0,0 @@
-node_modules
-ignore*
-
File renamed without changes.
View
@@ -1,5 +0,0 @@
-
-for (var i = 0; i < 10; i++) {
- console.log('stdout %d', i);
- console.error('stderr %d', i);
-}
File renamed without changes.
@@ -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);
@@ -1,2 +0,0 @@
-node_modules
-ignore*
@@ -1,3 +0,0 @@
-console.log('Hello!');
-setTimeout(process.exit, 2000);
-
View
@@ -1 +0,0 @@
-/* hello, I know nodejitsu. */
No changes.
@@ -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,

0 comments on commit b5ce548

Please sign in to comment.