Skip to content

Commit

Permalink
several improvements for command line mode
Browse files Browse the repository at this point in the history
very important: now there is a daemon mode, start geddy with command
line arg -d / --daemon to start in background.
call geddy with command line arg --stop to stop the daemon.
long command line args, as --geddy-root i.e., dont need a '=' and
could use only a space for seperating their values.
fixed a bug at lib/log.js for fatal errors.
  • Loading branch information
scasei authored and mde committed Nov 10, 2010
1 parent c21c8e1 commit 585222e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 27 deletions.
2 changes: 1 addition & 1 deletion geddy-core/lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var log = new function () {

this.fatal = function (msg) {
this.flush()
sys.puts("\n\nFATAL ERROR:\n\n"+sanitize(msg))
sys.puts("\n\nFATAL ERROR:\n\n"+this.sanitize(msg))
return this; // not that it maters...
}

Expand Down
4 changes: 3 additions & 1 deletion geddy-core/lib/parseopts.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var parseopts = new function () {
argName = optsReverseMap[argItems[0]];
if (argName) {
// If there's no attached value, value is null
opts[argName] = argItems[1] || null;
opts[argName] = argItems[1] || (!args[0] || (args[0].indexOf('-') == 0)) ?
null : args.shift();
}
else {
throw new Error('Unknown option "' + argItems[0] + '"');
Expand Down Expand Up @@ -66,3 +67,4 @@ var parseopts = new function () {
};

exports.parse = parseopts.parse;

92 changes: 70 additions & 22 deletions geddy-core/scripts/geddy
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,80 @@
# limitations under the License.
#

SOURCE_DIR=""
USE_SOURCE=0
SOURCE_DIR=
ROOT_DIR=$PWD
DAEMON=0
COMMAND=
ARGS=
PIDPATH=$HOME
PIDFILE=
ACTION="start"

for x in $@
while [ "$1" != "" ]
do
if [ $USE_SOURCE = 1 ]
then
SOURCE_DIR="$x"
break
fi
if [ ${x:0:13} = "--server-root" ]
then
USE_SOURCE=1
# This doesn't expand ~/ paths, why not?
SOURCE_DIR="${x/--server-root=/}"
break
fi
if [ "$x" = "-x" ]
then
USE_SOURCE=1
fi
case $1 in
-r|--geddy-root)
ROOT_DIR=$2
ARGS="${ARGS}-r ${2} "
shift 2
;;
-x|--server-root)
SOURCE_DIR=$2
ARGS="${ARGS}-x ${2} "
shift 2
;;
-d|--daemon)
DAEMON=1
shift
;;
--stop)
ACTION="stop"
shift
;;
*)
ARGS="${ARGS}${1} "
shift
;;
esac

done

if [ $USE_SOURCE = 0 ]
if [ $ACTION = "start" ]
then
unset NODE_PATH; node ~/.node_libraries/geddy-core/scripts/startserv.js -r `pwd` $@
COMMAND="/usr/local/bin/node"

if [ -z $SOURCE_DIR ]
then
unset NODE_PATH;
COMMAND="${COMMAND} ${HOME}/.node_libraries"
else
export NODE_PATH=${SOURCE_DIR};
COMMAND="${COMMAND} ${SOURCE_DIR}"
fi

COMMAND="${COMMAND}/geddy-core/scripts/startserv.js ${ARGS}"

if [ $DAEMON = 1 ]
then
$COMMAND > /dev/null &
PIDFILE=`echo $ROOT_DIR|sed 's/\//\-/g'`
PIDFILE="${PIDPATH}/geddy${PIDFILE}.pid"
echo "$!" > $PIDFILE
else
$COMMAND
fi
else
export NODE_PATH=${SOURCE_DIR}; node ${SOURCE_DIR}/geddy-core/startserv.js -r `pwd` $@
PIDFILE=`echo $ROOT_DIR|sed 's/\//\-/g'`
PIDFILE="${PIDPATH}/geddy${PIDFILE}.pid"
if [ -s $PIDFILE ]
then
p=`cat $PIDFILE`
>$PIDFILE
if [ `kill -SIGINT "${p}"` ]
then
kill -9 "${p}"
exit 1
fi
fi
fi
exit 0
23 changes: 20 additions & 3 deletions geddy-core/scripts/startserv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var GEDDY_VERSION = '0.1.0';

global.geddy = require('geddy-core/lib/geddy');


var usage = ''
+ 'Geddy web framework for Node.js\n'
+ '********************************************************************************\n'
Expand All @@ -18,6 +19,10 @@ var usage = ''
+ ' -r, --geddy-root PATH Sets directory for Geddy application root\n'
+ ' -V, --version Outputs Geddy version\n'
+ ' -h, --help Outputs help information\n'
+ ' -x, --server-root PATH Sets directory for Geddy source, defaults to use path'
+ ' where Geddy installed'
+ ' -d, --daemon Start as daemon'
+ ' --stop Stop daemon'
+ '';

var args = process.argv.slice(2),
Expand Down Expand Up @@ -79,6 +84,8 @@ else {

args.unshift(serverRoot);

process.chdir(geddy.config.dirname);

log = require('geddy-core/lib/log');

var jsPat = /\.js$/;
Expand Down Expand Up @@ -150,16 +157,25 @@ var startServ = function (restart) {
else {
process.stdout.write(data);
}
});
child.addListener('exit', function (code) {
});

child.addListener('exit', function (code, signal) {
process.kill(child.pid);
process.exit();
});

pids.push(child.pid);

}

};

process.on('SIGINT',function(){
for (var i = 0, ii = pids.length; i < ii; i++) {
process.kill(pids[i]);
}
process.exit();
});

var restartServ = function (curr, prev) {
// Only if the file has been modified
if (curr.mtime.getTime() != prev.mtime.getTime()) {
Expand All @@ -184,3 +200,4 @@ if (geddy.config.environment == 'development') {
watchTree(geddy.config.dirname + '/app/models', restartServ);
}


0 comments on commit 585222e

Please sign in to comment.