Skip to content

Commit

Permalink
[fix][WIP] basic working order, starting CLI cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck authored and indexzero committed Oct 9, 2011
1 parent 6f68823 commit bad47f6
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 418 deletions.
86 changes: 0 additions & 86 deletions init.d/foreverd

This file was deleted.

146 changes: 0 additions & 146 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,152 +225,6 @@ cli.clear = function (key) {
});
};

//
// ### function service(action)
// add
// install
// start
// stop
// restart
//
// TODO - guard against user invocation
//
cli['service-add'] = function (file, options) {
//
// Add descriptor to our service list
// this is just a json file in $root/services/*.json
//
forever.config.set('root', path.join('/var', 'local', 'forever'));
var options;
function save() {
var service = {
file: file,
options: options
};
options.appendLog = true;
var filePath = path.join(forever.config.get('root'), 'services', options.uid + '.json');
fs.writeFileSync(filePath, JSON.stringify(service));
}
save();
}
cli['service-install'] = function () {
//
// Copy the init.d script to the right location
// TODO Distribution fixes?
//
forever.config.set('root', path.join('/var', 'local', 'foreverd'));
forever.log.info('Creating service environment.');
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);
}
catch (e) {
if (e.code !== 'EEXIST') {
forever.log.error('Error creating service directory.');
forever.log.error(e.message);
return;
}
}
forever.log.info('Installing init.d script.');
var script = fs.createReadStream(path.join(__dirname, '..', '..', 'init.d', 'foreverd'));
var target = fs.createWriteStream(initdPath, {
flags: 'w',
mode: 0777
});
script.pipe(target);
script.on('end', function() {
forever.log.info('Adding init.d script to run levels');
var directories = fs.readdirSync('/etc');
directories.forEach(function (directory) {
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+'20foreverd'));
}
});
});
}
cli['service-start'] = function (file, options) {
//
// Start all of the scripts
//
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', 'foreverd'));
var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services'));
if (serviceFiles.length === 0) {
forever.log.info('No services found.');
return;
}
serviceFiles.forEach(function (serviceFile) {
var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
var service = JSON.parse(fs.readFileSync(serviceFilePath));
var file = service.file;
cli.stop(file);
});
}
cli['service-restart'] = function (file, options) {
//
// Restart all the scripts
//
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.');
return;
}

serviceFiles.forEach(function (serviceFile) {
var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile);
var service = JSON.parse(fs.readFileSync(serviceFilePath));
var file = service.file;
cli.restart(file);
});
}

//
// ### function columns (action, value)
// #### @action {string} The subaction to execute
Expand Down
2 changes: 2 additions & 0 deletions lib/foreverd/adapter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = ForeverServiceAdapter;

function ForeverServiceAdapter(service) {
this.service = service;
}
Expand Down
12 changes: 4 additions & 8 deletions lib/foreverd/adapter/systemv/foreverd
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ start() {
echo "Starting $NAME: "

if [ "$id" = "" ]; then
# Create the log and pid files, making sure that the target use has access to them
touch $logfile
chown $USER $logfile

# Launch the application
$foreverd -p $forever_dir
$foreverd -p $forever_dir run
else
echo "Instance already running"
fi
Expand All @@ -42,7 +38,7 @@ start() {
restart() {
echo -n "Restarting $NAME: "
if [ "$id" != "" ]; then
$foreverd -p $forever_dir
$foreverd -p $forever_dir restart
RETVAL=$?
else
start
Expand All @@ -52,7 +48,7 @@ restart() {
stop() {
echo -n "Shutting down $NAME: "
if [ "$id" != "" ]; then
$foreverd -p $forever_dir
$foreverd -p $forever_dir stop
else
echo "Instance is not running";
fi
Expand All @@ -73,7 +69,7 @@ case "$1" in
stop
;;
status)
$foreverd list -p $forever_dir
$foreverd -p $forever_dir list
;;
restart)
restart
Expand Down
Loading

0 comments on commit bad47f6

Please sign in to comment.