From 3a8fc2b2cc42c06ca1ef8740b1ad672ca565103e Mon Sep 17 00:00:00 2001 From: Justin Tulloss Date: Wed, 6 Oct 2010 19:08:58 -0700 Subject: [PATCH 1/2] support passing in a pidfile --- bin/spark | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/bin/spark b/bin/spark index 0ffa34a..e1902f8 100755 --- a/bin/spark +++ b/bin/spark @@ -74,6 +74,7 @@ var usage = '' + ' --ssl-key PATH SSL key file\n' + ' --ssl-crt PATH SSL certificate file\n' + ' -n, --workers NUM Number of worker processes to spawn\n' + + ' -p, --pidfile PATH File to store PID of server and any child PIDs\n' + ' -I, --include PATH Unshift the given path to require.paths\n' + ' -E, --env NAME Set environment, defaults to "development"\n' + ' -M, --mode NAME Alias of -E, --env\n' @@ -298,7 +299,19 @@ function startWorker() { process.sparkEnv = env = JSON.parse(json.toString()); }); stdin.addListener('fd', function(fd){ + var stream; var app = requireApp(getAppPath()); + if (env.pidfile) { + fs.open(env.pidfile, 'a+', function(err, fd) { + if (err) { + sys.error(err) + return; + } + else { + fs.write(fd, process.pid + '\n'); + } + }); + } sys.error('Spark server(' + process.pid + ') listening on ' + 'http' + (env.sslKey ? 's' : '') + '://' + (env.host || '*') + ':' + env.port @@ -380,6 +393,11 @@ function start() { // Ignore } }); + + if (env.pidfile) { + fs.unlinkSync(env.pidfile); + } + process.exit(); }); }); @@ -389,6 +407,18 @@ function start() { // Load the app module var app = requireApp(path); + if (env.pidfile) { + console.log('writing pidfile'); + fs.open(env.pidfile, 'w', function(err, fd) { + if (err) { + sys.error(err) + return; + } + else { + fs.write(fd, process.pid + '\n'); + } + }); + } sys.error('Spark server(' + process.pid + ') listening on ' + 'http' + (env.sslKey ? 's' : '') + '://' + (env.host || '*') + ':' + env.port @@ -396,6 +426,12 @@ function start() { enableSSL(app, env); app.listen(env.port, env.host); + process.on('exit', function() { + if (env.pidfile) { + fs.unlinkSync(env.pidfile); + } + }); + changeUser(); } @@ -497,6 +533,10 @@ function parseArguments(args, cmd) { case '--ssl-crt': env.sslCrt = fs.readFileSync(requireArg('--ssl-crt'), 'ascii'); break; + case '-p': + case '--pidfile': + env.pidfile = requireArg('--pidfile'); + break; default: if (arg[0] === '-') { arg = arg.substr(2); From 467546a8b55cbe241251af0162138e9bd02f0c48 Mon Sep 17 00:00:00 2001 From: Justin Tulloss Date: Wed, 6 Oct 2010 19:11:19 -0700 Subject: [PATCH 2/2] removed unused variable --- bin/spark | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/spark b/bin/spark index e1902f8..98d68a1 100755 --- a/bin/spark +++ b/bin/spark @@ -299,7 +299,6 @@ function startWorker() { process.sparkEnv = env = JSON.parse(json.toString()); }); stdin.addListener('fd', function(fd){ - var stream; var app = requireApp(getAppPath()); if (env.pidfile) { fs.open(env.pidfile, 'a+', function(err, fd) {