Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pidfile support #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions bin/spark
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -380,6 +392,11 @@ function start() {
// Ignore
}
});

if (env.pidfile) {
fs.unlinkSync(env.pidfile);
}

process.exit();
});
});
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down