diff --git a/bin/spark b/bin/spark index 0ffa34a..98d68a1 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' @@ -299,6 +300,17 @@ function startWorker() { }); stdin.addListener('fd', function(fd){ 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 +392,11 @@ function start() { // Ignore } }); + + if (env.pidfile) { + fs.unlinkSync(env.pidfile); + } + process.exit(); }); }); @@ -389,6 +406,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 +425,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 +532,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);