Skip to content

Commit

Permalink
[refactor] Finish refactor of core Monitor functionality into `foreve…
Browse files Browse the repository at this point in the history
…r-monitor`
  • Loading branch information
indexzero committed Jul 5, 2012
1 parent 5225d68 commit b5ce548
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 143 deletions.
19 changes: 1 addition & 18 deletions bin/forever
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 0 additions & 2 deletions bin/monitor
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
14 changes: 0 additions & 14 deletions examples/chroot.js

This file was deleted.

6 changes: 3 additions & 3 deletions examples/cli-multiple-start
Original file line number Diff line number Diff line change
@@ -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
77 changes: 15 additions & 62 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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`
Expand Down Expand Up @@ -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();
};

Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down
40 changes: 16 additions & 24 deletions lib/forever/service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
};

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/cli-test
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/.foreverignore

This file was deleted.

File renamed without changes.
5 changes: 0 additions & 5 deletions test/fixtures/logs.js

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion test/fixtures/start-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/watch/.foreverignore

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/watch/daemon.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/watch/file

This file was deleted.

Empty file removed test/fixtures/watch/removeMe
Empty file.
2 changes: 1 addition & 1 deletion test/worker/multiple-workers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5ce548

Please sign in to comment.