From 61651a7a4d2dec799a5e06933ab773f13aa99dcf Mon Sep 17 00:00:00 2001 From: bradleymeck Date: Thu, 22 Sep 2011 10:23:26 -0500 Subject: [PATCH] [fix] daemonize ourselve on startup rather than rely on OS function (TODO exit codes) --- init.d/{forever-services => foreverd} | 11 ++--- lib/forever/cli.js | 70 +++++++++++++++++---------- 2 files changed, 48 insertions(+), 33 deletions(-) rename init.d/{forever-services => foreverd} (89%) diff --git a/init.d/forever-services b/init.d/foreverd similarity index 89% rename from init.d/forever-services rename to init.d/foreverd index 3d4b8025..b11b541f 100644 --- a/init.d/forever-services +++ b/init.d/foreverd @@ -13,7 +13,7 @@ . /lib/lsb/init-functions PATH=$PATH:/usr/local/bin -NAME=forever-services +NAME=foreverd pidfile=/var/run/$NAME.pid logfile=/var/log/$NAME.log forever_dir=/var/local/forever # Forever root directory. @@ -30,12 +30,9 @@ start() { # Create the log and pid files, making sure that the target use has access to them touch $logfile chown $USER $logfile - - touch $pidfile - chown $USER $pidfile - + # Launch the application - start_daemon -p $pidfile -- $forever service-start -p $forever_dir + $forever service-start -p $forever_dir else echo "Instance already running" fi @@ -76,7 +73,7 @@ case "$1" in stop ;; status) - status -p ${pidfile} + $forever list -p $forever_dir ;; restart) restart diff --git a/lib/forever/cli.js b/lib/forever/cli.js index 04e2bce2..5ac66c3f 100644 --- a/lib/forever/cli.js +++ b/lib/forever/cli.js @@ -10,6 +10,7 @@ var fs = require('fs'), path = require('path'), cliff = require('cliff'), tty = require('tty'); + daemon = require('daemon'); forever = require('../forever'); var cli = exports; @@ -257,9 +258,9 @@ cli['service-install'] = function () { // Copy the init.d script to the right location // TODO Distribution fixes? // - forever.config.set('root', path.join('/var', 'local', 'forever')); + forever.config.set('root', path.join('/var', 'local', 'foreverd')); forever.log.info('Creating service environment.'); - var initdPath = path.join('/etc', 'init.d', 'forever-services'); + var initdPath = path.join('/etc', 'init.d', 'foreverd'); try { fs.mkdirSync(forever.config.get('root'), 0777); fs.mkdirSync(path.join(forever.config.get('root'), 'services'), 0777); @@ -272,7 +273,7 @@ cli['service-install'] = function () { } } forever.log.info('Installing init.d script.'); - var script = fs.createReadStream(path.join(__dirname, '..', '..', 'init.d', 'forever-services')); + var script = fs.createReadStream(path.join(__dirname, '..', '..', 'init.d', 'foreverd')); var target = fs.createWriteStream(initdPath, { flags: 'w', mode: 0777 @@ -285,7 +286,7 @@ cli['service-install'] = function () { var match = directory.match(/^rc(\d+)\.d$/); if(match) { var kill_or_start = {0:true, 1:true, 6:true}[match[1]] ? 'K' : 'S'; - fs.symlinkSync(initdPath, path.join('/etc',directory,kill_or_start+'20forever-services')); + fs.symlinkSync(initdPath, path.join('/etc',directory,kill_or_start+'20foreverd')); } }); }); @@ -294,34 +295,51 @@ cli['service-start'] = function (file, options) { // // Start all of the scripts // - forever.config.set('root', path.join('/var', 'local', 'forever')); - var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services')); - if (serviceFiles.length === 0) { - forever.log.info('No services found.'); - return; - } - var monitors = []; - process.title = 'forever-services'; - serviceFiles.forEach(function (serviceFile, index) { - var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile); - var service = JSON.parse(fs.readFileSync(serviceFilePath)); - var file = service.file; - forever.log.info('Starting service for ' + file); - var options = service.options; - options.minUptime = 200; - var monitor = new forever.Monitor(file, options); - monitor.start(); - monitors.push(monitor); - if(index === serviceFiles.length - 1) { - forever.startServer.apply(forever, monitors); + var pidFilePath = path.join('/var','run', 'foreverd.pid'); + var logFilePath = path.join('/var','log','foreverd'); + process.on('exit', function() { + try{ + fs.unlinkSync(pidFilePath); } + catch(err) { + //we are exiting anyway. this may have some noexist error already + } + }) + forever.config.set('root', path.join('/var', 'local', 'foreverd')); + fs.open(logFilePath, 'w+', function (err, logFile) { + if(err) { + throw err; + } + daemon.start(logFile); + daemon.lock(pidFilePath); + + var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services')); + if (serviceFiles.length === 0) { + forever.log.info('No services found.'); + return; + } + var monitors = []; + serviceFiles.forEach(function (serviceFile, index) { + var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile); + var service = JSON.parse(fs.readFileSync(serviceFilePath)); + var file = service.file; + forever.log.info('Starting service for ' + file); + var options = service.options; + options.minUptime = 200; + var monitor = new forever.Monitor(file, options); + monitor.start(); + monitors.push(monitor); + if(index === serviceFiles.length - 1) { + forever.startServer.apply(forever, monitors); + } + }); }); } cli['service-stop'] = function (file, options) { // // Stop all the scripts // - forever.config.set('root', path.join('/var', 'local', 'forever')); + forever.config.set('root', path.join('/var', 'local', 'foreverd')); var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services')); if (serviceFiles.length === 0) { forever.log.info('No services found.'); @@ -338,7 +356,7 @@ cli['service-restart'] = function (file, options) { // // Restart all the scripts // - forever.config.set('root', path.join('/var', 'local', 'forever')); + forever.config.set('root', path.join('/var', 'local', 'foreverd')); var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services')); if (serviceFiles.length === 0) { forever.log.info('No services found.');