diff --git a/bin/monitor b/bin/monitor new file mode 100755 index 00000000..555c6a0f --- /dev/null +++ b/bin/monitor @@ -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(); + }); +} diff --git a/lib/forever.js b/lib/forever.js index 1f3d0c64..bbf2df43 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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'), @@ -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(); }; // diff --git a/lib/forever/cli.js b/lib/forever/cli.js index 7a32c72e..d22694f0 100644 --- a/lib/forever/cli.js +++ b/lib/forever/cli.js @@ -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); }); }); diff --git a/package.json b/package.json index 34e09c22..57e46332 100644 --- a/package.json +++ b/package.json @@ -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", @@ -50,7 +49,7 @@ "test": "vows test/**/*-test.js --spec -i" }, "engines": { - "node": ">= 0.6.0" + "node": ">= 0.7.11" } }