Skip to content

Commit

Permalink
[refactor minor] Final integrations for forever-monitor@1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 6, 2012
1 parent f27cdaa commit 6921e6c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
29 changes: 20 additions & 9 deletions bin/monitor
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ 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());
if (!started) {
started = true;
start(options);
}
});

//
// ### @function (options)
// #### @options {Object} Options for the `forever.Monitor` instance.
//
function start(options) {
var script = process.argv[2],
monitor = new forever.Monitor(script, options);

forever.logEvents(monitor);
monitor.start();

monitor.on('start', function () {
Expand All @@ -28,3 +24,18 @@ function start(options) {
process.disconnect();
});
}

//
// When we receive the first message from the parent process, start
// an instance of `forever.Monitor` with the options supplied.
//
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());
if (!started) {
started = true;
start(options);
}
});
32 changes: 31 additions & 1 deletion lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,17 @@ forever.start = function (script, options) {
if (!options.uid) {
options.uid = options.uid || utile.randomString(4).replace(/^\-/, '_');
}

if (!options.logFile) {
options.logFile = forever.logFilePath(options.uid + '.log');
}

return new forever.Monitor(script, options).start();
//
// Create the monitor, log events, and start.
//
var monitor = new forever.Monitor(script, options);
forever.logEvents(monitor);
return monitor.start();
};

//
Expand Down Expand Up @@ -759,6 +765,30 @@ forever.pidFilePath = function (pidFile) {
: path.join(forever.config.get('pidPath'), pidFile);
};

//
// ### @function logEvents (monitor)
// #### @monitor {forever.Monitor} Monitor to log events for
// Logs important restart and error events to `console.error`
//
forever.logEvents = function (monitor) {
monitor.on('watch:error', function (info) {
console.error(info.message);
console.error(info.error);
});

monitor.on('watch:restart', function (info) {
console.error('restaring script because ' + info.file + ' changed')
});

monitor.on('restart', function () {
console.error('Forever restarting script for ' + monitor.times + ' time')
});

monitor.on('exit:code', function (code) {
console.error('Forever detected script exited with code: ' + code);
});
};

//
// ### @columns {Object}
// Property descriptors for accessing forever column information
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"cliff": "0.x.x",
"flatiron": "0.2.x",
"forever-monitor": "x.x.x",
"forever-monitor": "1.0.1",
"nconf": "0.5.x",
"nssocket": "0.3.x",
"optimist": "0.3.x",
Expand Down

0 comments on commit 6921e6c

Please sign in to comment.