Skip to content

Commit

Permalink
[fix] make services use hyphenated commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck authored and indexzero committed Oct 9, 2011
1 parent 93053d6 commit 60d4329
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 28 deletions.
17 changes: 15 additions & 2 deletions bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ var action, accepts = [
'clear',
'columns',
'list',
'service',
'service-install',
'service-add',
'service-start',
'service-stop',
'service-restart',
'start',
'stop',
'stopall',
Expand Down Expand Up @@ -78,7 +82,16 @@ var help = [
].join('\n');

function isSimpleAction () {
return ['config', 'list', 'stopall', 'cleanlogs'].indexOf(action) !== -1;
return [
'config',
'list',
'stopall',
'cleanlogs',
'service-install',
'service-start',
'service-stop',
'service-restart'
].indexOf(action) !== -1;
};

//
Expand Down
58 changes: 58 additions & 0 deletions init.d/forever-services
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
#
# forever-services Node init.d
#
# chkconfig: 345 80 20
# description: Node init.d for forever service files
# processname: node
# pidfile: /var/run/forever-services.pid
# logfile: /var/log/forever-services.log
#

# Source function library.

NAME=forever-services # Unique name for the application
SOURCE_NAME=main.js # Name os the applcation entry point script

forever_dir=/var/local/forever # Forever root directory.

node=node
forever=forever

start() {
echo "Starting $NAME: "
$forever service-start -p $forever_dir
RETVAL=0
}

restart() {
echo -n "Restarting $NAME: "
$forever service-restart -p $forever_dir
RETVAL=0
}

stop() {
echo -n "Shutting down $NAME: "
$forever service-stop -p $forever_dir
RETVAL=0
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile}
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
102 changes: 76 additions & 26 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*
*/

var path = require('path'),
var fs = require('fs'),
path = require('path'),
cliff = require('cliff'),
tty = require('tty');
forever = require('../forever');

var cli = exports;
Expand Down Expand Up @@ -232,53 +234,101 @@ cli.clear = function (key) {
//
// TODO - guard against user invocation
//
cli.service = function (action) {
cli['service-add'] = function (file, options) {
//
// Add descriptor to our service list
// this is just a json file in $root/services/*.json
//
if('add' == action) {
var service = options;
var file = path.join(forever.config.get('root'), 'services', options.uid + '.json');
fs.writeFileSync(file, JSON.stringify(service));
forever.config.set('root', path.join('/var', 'local', 'forever'));
var options;
function save() {
var service = {
file: file,
options: options
};
console.error(service);
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?
//
if('install' == action) {
var service = options;
var script = fs.createReadStream(path.join(__dirname, '..', '..', 'init.d', 'forever-services'));
var target = fs.createWriteStream(path.join('/etc', 'init.d', 'forever-services'));
file.pipe(target);
forever.config.set('root', path.join('/var', 'local', 'forever'));
forever.log.info('Creating service environment.');
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', 'forever-services'));
var target = fs.createWriteStream(path.join('/etc', 'init.d', 'forever-services'), {
flags: 'w',
mode: 0777
});
script.pipe(target);
}
cli['service-start'] = function (file, options) {
//
// Start all of the scripts
//
if('start' == action) {
var serviceFiles = readdirSync(path.join(forever.config.get('root'), 'services'));
serviceFiles.forEach(function(serviceFile) {
forever.start(fs.readFileSync(serviceFile));
});
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;
}
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;
var options = service.options;
cli.start(file, options, true);
});
}
cli['service-stop'] = function (file, options) {
//
// Stop all the scripts
//
if('stop' == action) {
var serviceFiles = readdirSync(path.join(forever.config.get('root'), 'services'));
serviceFiles.forEach(function(serviceFile) {
forever.stop(fs.readFileSync(serviceFile));
});
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;
}
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
//
if('restart' == action) {
var serviceFiles = readdirSync(path.join(forever.config.get('root'), 'services'));
serviceFiles.forEach(function(serviceFile) {
forever.restart(fs.readFileSync(serviceFile));
});
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;
}
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);
});
}

//
Expand Down

0 comments on commit 60d4329

Please sign in to comment.