pidfile support #11

Open
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+39 −0
Diff settings

Always

Just for now

View
@@ -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,13 +406,31 @@ 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
+ ' in ' + env.name + ' mode');
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);