Permalink
Browse files

remove old code.

  • Loading branch information...
1 parent 0d9aef4 commit 219358ccfc67fe663380e605c6bdb10e3fc990d5 @chjj committed Apr 11, 2012
Showing with 21 additions and 136 deletions.
  1. +21 −136 lib/daemonic.js
View
@@ -1,6 +1,6 @@
/**
- * daemonic.js
- * Dead-simple daemonization for node.js
+ * Daemonic
+ * Dead-simple daemonization for node.js.
* Copyright (c) 2012, Christopher Jeffrey
*/
@@ -41,7 +41,6 @@ daemonic.daemonize = function(opt, func) {
process.exit(code || 0);
});
- // kill current stack
process.once('uncaughtException', function() {});
throw 'stop';
};
@@ -76,16 +75,6 @@ daemonic.fork = function(name, argv, opt, func) {
* Helpers
*/
-function env(env) {
- if (!env) return '';
- var out = [];
- Object.keys(env).forEach(function(key) {
- var val = env[key].replace(/(["$\\])/g, '\\$1');
- out.push(key + '="' + val + '"');
- });
- return 'env - ' + out.join(' ');
-}
-
function fork(argv, opt) {
var opt = opt || {}
, spawn = require('child_process').spawn
@@ -159,6 +148,16 @@ function fork(argv, opt) {
return ps;
}
+function env(env) {
+ if (!env) return '';
+ var out = [];
+ Object.keys(env).forEach(function(key) {
+ var val = env[key].replace(/(["$\\])/g, '\\$1');
+ out.push(key + '="' + val + '"');
+ });
+ return 'env - ' + out.join(' ');
+}
+
function badOptions(opt) {
var options = opt && opt.options;
@@ -181,137 +180,23 @@ function badOptions(opt) {
return !found;
}
-// =============================================================================
-
-/**
- * Daemonize
- */
-
-function daemonize(opt, func) {
- var argv = opt.process || process.argv.slice()
- , i;
-
- if (!opt.process) {
- if (exports.daemon) return;
-
- i = process.argv.indexOf(exports.arg);
- if (~i) {
- process.argv.splice(i, 1);
- exports.daemon = true;
- return;
- }
-
- argv.push(exports.arg);
- }
-
- var spawn = require('child_process').spawn
- , code = ''
- , pid = ''
- , ps;
-
- // escape arguments
- argv = argv.map(function(arg) {
- arg = arg.replace(/(["$\\])/g, '\\$1');
- return '"' + arg + '"';
- }).join(' ');
-
- // chdir
- if (opt.cd) code += 'cd / && ';
-
- // umask
- if (opt.umask) code += 'umask 0 && ';
-
- // dup2
- opt.stdin = opt.stdin != null
- ? ' 0 <& ' + opt.stdin
- : ' 0 < /dev/null';
- opt.stdout = opt.stdout != null
- ? ' 1 >& ' + opt.stdout
- : ' 1 > /dev/null';
- opt.stderr = opt.stderr != null
- ? ' 2 >& ' + opt.stderr
- : ' 2 > /dev/null';
-
- code = '('
- + code
- + 'setsid '
- + argv
- + opt.stdin
- + opt.stdout
- + opt.stderr
- + ' & echo $!)';
-
- // fork
- ps = spawn('/bin/sh', [ '-c', code ]);
-
- if (opt.process) {
- func = func || function() {};
-
- ps.stdout.setEncoding('utf8');
- ps.stdout.on('data', function(data) {
- pid += data;
- });
-
- ps.on('error', func);
- ps.on('exit', function() {
- pid = +pid;
- if (!isFinite(pid)) pid = -1;
- func(null, pid);
- });
-
- return;
- }
-
- ps.on('exit', function(code) {
- process.exit(code || 0);
- });
-
- // kill current stack
- process.once('uncaughtException', function() {});
- throw 'stop';
-};
-
-/**
- * Daemonic
- */
-
-function daemonic(opt, func) {
- var opt = opt || {}
- , options = opt.options;
-
- if (options && options.length) {
- var found = false
- , argv = process.argv
- , l = options.length
- , i = 0
- , j;
-
- for (; i < l; i++) {
- j = argv.indexOf(options[i]);
- if (~j) {
- argv.splice(j, 1);
- found = true;
- }
- }
-
- if (!found) return;
- }
-
- daemonize(opt, func);
-
- return daemonic;
-}
-
-// =============================================================================
-
/**
* Expose
*/
+// expose
exports = daemonic;
+
+// backup origina argv
exports.argv = process.argv.slice();
+
+// daemon notification argument
exports.arg = '--__daemonized__';
+// whether the process is daemonized
+exports.isDaemon = false;
+
+// ignore windows
module.exports = process.platform === 'win32'
? function() {}
: exports;

0 comments on commit 219358c

Please sign in to comment.