Skip to content

Commit

Permalink
[refactor] Refactor to remove daemon.node
Browse files Browse the repository at this point in the history
This replaces the addon with the new child process API in node core.  This
branch is for node 0.8.x only - 0.6.x support will be continued as a separate
release series.
  • Loading branch information
AvianFlu committed Jun 26, 2012
1 parent 485a18b commit 4fed919
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
34 changes: 34 additions & 0 deletions bin/monitor
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /usr/bin/env node

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 start(options) {
var script = process.argv[2],
monitor = new forever.Monitor(script, options);

monitor.start();

monitor.on('start', function () {
// This starts an nssocket server, which the forever CLI uses to
// communicate with this monitor process after it's detached.
// Without this, `forever list` won't show the process, even though it
// would still be running in the background unaffected.
forever.startServer(monitor);
// Disconnect the IPC channel, letting this monitor's parent process know
// that the child has started successfully.
process.disconnect();
});
}
32 changes: 20 additions & 12 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var fs = require('fs'),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
cliff = require('cliff'),
daemon = require('daemon'),
nconf = require('nconf'),
nssocket = require('nssocket'),
portfinder = require('portfinder'),
Expand Down Expand Up @@ -375,20 +374,29 @@ forever.startDaemon = function (script, options) {
options.logFile = forever.logFilePath(options.logFile || options.uid + '.log');
options.pidFile = forever.pidFilePath(options.pidFile || options.uid + '.pid');

var monitor = new forever.Monitor(script, options),
pid;
var monitor, outFD, errFD, workerPath;

pid = daemon.start(options.logFile);
daemon.lock(options.pidFile);
/*
* 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.
*
*/

// process.on('exit', function () {
// fs.unlinkSync(options.pidFile);
// });
outFD = fs.openSync(options.logFile, 'a');
errFD = fs.openSync(options.logFile, 'a');
monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor');

process.pid = pid;
monitor.start();

return monitor;
monitor = spawn(monitorPath, [ script ], {
stdio: [ 'ipc', outFD, errFD ],
detached: true
});
monitor.on('exit', function (code) {
console.error('Monitor died unexpectedly with exit code %d', code);
});
monitor.send(JSON.stringify(options));
monitor.unref();
};

//
Expand Down
5 changes: 1 addition & 4 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ app.cmd(/start (.+)/, cli.startDaemon = function () {

forever.log.info('Forever processing file: ' + file.grey);
tryStart(file, options, function () {
var monitor = forever.startDaemon(file, options);
monitor.on('start', function () {
forever.startServer(monitor);
});
forever.startDaemon(file, options);
});
});

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dependencies": {
"broadway": "0.1.x",
"cliff": "0.x.x",
"daemon": "0.5.x",
"flatiron": "0.1.x",
"minimatch": "0.2.x",
"nconf": "0.5.x",
Expand Down Expand Up @@ -50,7 +49,7 @@
"test": "vows test/**/*-test.js --spec -i"
},
"engines": {
"node": ">= 0.6.0"
"node": ">= 0.7.11"
}
}

0 comments on commit 4fed919

Please sign in to comment.