Skip to content

Commit

Permalink
[fix] daemonize ourselve on startup rather than rely on OS function (…
Browse files Browse the repository at this point in the history
…TODO exit codes)
  • Loading branch information
bmeck authored and indexzero committed Oct 9, 2011
1 parent 782cca7 commit 61651a7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
11 changes: 4 additions & 7 deletions init.d/forever-services → init.d/foreverd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -76,7 +73,7 @@ case "$1" in
stop
;;
status)
status -p ${pidfile}
$forever list -p $forever_dir
;;
restart)
restart
Expand Down
70 changes: 44 additions & 26 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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'));
}
});
});
Expand All @@ -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.');
Expand All @@ -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.');
Expand Down

0 comments on commit 61651a7

Please sign in to comment.