Permalink
Browse files

[wtf.node] BLACK VOODOO MAGIC. `daemon.node` somehow works even thoug…

…h libuv isnt fork(2)-safe
  • Loading branch information...
1 parent ebd80a2 commit 9cee33890e1e699a8358109cd5b2ad8746400c5d @indexzero indexzero committed Jan 5, 2012
Showing with 27 additions and 37 deletions.
  1. +23 −34 lib/forever.js
  2. +3 −2 lib/forever/cli.js
  3. +1 −1 package.json
View
@@ -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'),
@@ -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;
};
//
View
@@ -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);
});
});
});
View
@@ -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",
@@ -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"

0 comments on commit 9cee338

Please sign in to comment.