Skip to content

Commit

Permalink
[wtf.node] BLACK VOODOO MAGIC. daemon.node somehow works even thoug…
Browse files Browse the repository at this point in the history
…h libuv isnt fork(2)-safe
  • Loading branch information
indexzero committed Jan 5, 2012
1 parent ebd80a2 commit 9cee338
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
57 changes: 23 additions & 34 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var fs = require('fs'),
fork = require('child_process').fork,
spawn = require('child_process').spawn,
cliff = require('cliff'),
daemon = require('daemon'),
nconf = require('nconf'),
nssocket = require('nssocket'),
portfinder = require('portfinder'),
Expand Down Expand Up @@ -309,43 +310,31 @@ forever.startDaemon = function (script, options, callback) {
options = options || {};
options.uid = options.uid || utile.randomString(4).replace(/^\-/, '_');
options.logFile = forever.logFilePath(options.logFile || options.uid + '.log');
options.pidFile = forever.pidFilePath(options.pidFile || options.uid + '.pid');

var worker = spawn(path.join(__dirname, '..', 'bin', 'forever-worker'));
worker.stdout.on('data', function (sockPath) {
var socket = new nssocket.NsSocket();

function onStart(data) {
socket.undata(['spawn', 'error'], onError);
callback(null, data);
socket.end();
}

function onError(data) {
socket.undata(['spawn', 'start'], onStart);
callback(data.error);
socket.end();
var monitor = new forever.Monitor(script, options);

fs.open(options.logFile, options.appendLog ? 'a+' : 'w+', function (err, fd) {
if (err) {
return monitor.emit('error', err);
}

socket.connect(sockPath.toString(), function (err) {
if (err) {
next(err);
}

socket.data(['spawn', 'start'], onStart);
socket.data(['spawn', 'error'], onError);

socket.send(['spawn'], {
script: script,
options: options
});
});

socket.on('error', function (err) {
socket.undata(['spawn', 'error'], onError);
socket.undata(['spawn', 'start'], onStart);
callback(err);
});

var pid = daemon.start(fd);
daemon.lock(options.pidFile);

//
// Remark: This should work, but the fd gets screwed up
// with the daemon process.
//
// process.on('exit', function () {
// fs.unlinkSync(options.pidFile);
// });

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

return monitor;
};

//
Expand Down
5 changes: 3 additions & 2 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ app.cmd(/start (.+)/, cli.startDaemon = function () {

forever.log.info('Forever processing file: ' + file.grey);
tryStart(file, options, function () {
forever.startDaemon(file, options, function () {
process.exit(0);
var monitor = forever.startDaemon(file, options);
monitor.on('start', function () {
forever.startServer(monitor);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"broadway": "0.1.x",
"cliff": "0.x.x",
"daemon": "0.4.x",
"flatiron": "0.1.x",
"minimatch": "0.0.x",
"nconf": "0.5.x",
Expand All @@ -38,7 +39,6 @@
"winston": "0.5.x"
},
"devDependencies": {
"daemon": "0.3.x >=0.3.2",
"eventemitter2": "0.4.x",
"request": "2.x.x",
"vows": "0.5.x"
Expand Down

0 comments on commit 9cee338

Please sign in to comment.