Skip to content

Commit 4fed919

Browse files
committed
[refactor] Refactor to remove daemon.node
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.
1 parent 485a18b commit 4fed919

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

bin/monitor

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#! /usr/bin/env node
2+
3+
var path = require('path'),
4+
forever = require(path.resolve(__dirname, '..', 'lib', 'forever')),
5+
started;
6+
7+
8+
9+
process.on('message', function (data) {
10+
// TODO: Find out if this data will ever get split into two message events.
11+
var options = JSON.parse(data.toString());
12+
if (!started) {
13+
started = true;
14+
start(options);
15+
}
16+
});
17+
18+
function start(options) {
19+
var script = process.argv[2],
20+
monitor = new forever.Monitor(script, options);
21+
22+
monitor.start();
23+
24+
monitor.on('start', function () {
25+
// This starts an nssocket server, which the forever CLI uses to
26+
// communicate with this monitor process after it's detached.
27+
// Without this, `forever list` won't show the process, even though it
28+
// would still be running in the background unaffected.
29+
forever.startServer(monitor);
30+
// Disconnect the IPC channel, letting this monitor's parent process know
31+
// that the child has started successfully.
32+
process.disconnect();
33+
});
34+
}

lib/forever.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var fs = require('fs'),
1212
exec = require('child_process').exec,
1313
spawn = require('child_process').spawn,
1414
cliff = require('cliff'),
15-
daemon = require('daemon'),
1615
nconf = require('nconf'),
1716
nssocket = require('nssocket'),
1817
portfinder = require('portfinder'),
@@ -375,20 +374,29 @@ forever.startDaemon = function (script, options) {
375374
options.logFile = forever.logFilePath(options.logFile || options.uid + '.log');
376375
options.pidFile = forever.pidFilePath(options.pidFile || options.uid + '.pid');
377376

378-
var monitor = new forever.Monitor(script, options),
379-
pid;
377+
var monitor, outFD, errFD, workerPath;
380378

381-
pid = daemon.start(options.logFile);
382-
daemon.lock(options.pidFile);
379+
/*
380+
* This log file is forever's log file - the user's outFile and errFile
381+
* options are not taken into account here. This will be an aggregate of all
382+
* the app's output, as well as messages from the monitor process, where
383+
* applicable.
384+
*
385+
*/
383386

384-
// process.on('exit', function () {
385-
// fs.unlinkSync(options.pidFile);
386-
// });
387+
outFD = fs.openSync(options.logFile, 'a');
388+
errFD = fs.openSync(options.logFile, 'a');
389+
monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor');
387390

388-
process.pid = pid;
389-
monitor.start();
390-
391-
return monitor;
391+
monitor = spawn(monitorPath, [ script ], {
392+
stdio: [ 'ipc', outFD, errFD ],
393+
detached: true
394+
});
395+
monitor.on('exit', function (code) {
396+
console.error('Monitor died unexpectedly with exit code %d', code);
397+
});
398+
monitor.send(JSON.stringify(options));
399+
monitor.unref();
392400
};
393401

394402
//

lib/forever/cli.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,7 @@ app.cmd(/start (.+)/, cli.startDaemon = function () {
226226

227227
forever.log.info('Forever processing file: ' + file.grey);
228228
tryStart(file, options, function () {
229-
var monitor = forever.startDaemon(file, options);
230-
monitor.on('start', function () {
231-
forever.startServer(monitor);
232-
});
229+
forever.startDaemon(file, options);
233230
});
234231
});
235232

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"dependencies": {
2323
"broadway": "0.1.x",
2424
"cliff": "0.x.x",
25-
"daemon": "0.5.x",
2625
"flatiron": "0.1.x",
2726
"minimatch": "0.2.x",
2827
"nconf": "0.5.x",
@@ -50,7 +49,7 @@
5049
"test": "vows test/**/*-test.js --spec -i"
5150
},
5251
"engines": {
53-
"node": ">= 0.6.0"
52+
"node": ">= 0.7.11"
5453
}
5554
}
5655

0 commit comments

Comments
 (0)