Added PID support

This commit is contained in:
Wyatt Johnson
2017-01-25 11:11:33 -07:00
parent f023d1de08
commit edf9ced454
6 changed files with 78 additions and 13 deletions
+48
View File
@@ -7,6 +7,8 @@
const program = require('commander');
const pkg = require('../package.json');
const dotenv = require('dotenv');
const fs = require('fs');
const util = require('../util');
//==============================================================================
// Setting up the program command line arguments.
@@ -15,6 +17,7 @@ const dotenv = require('dotenv');
program
.version(pkg.version)
.option('-c, --config [path]', 'Specify the configuration file to load')
.option('--pid [path]', 'Specify a path to output the current PID to')
.parse(process.argv);
if (program.config) {
@@ -27,6 +30,40 @@ if (program.config) {
}
}
// If the `--pid` flag is used, put the current pid there.
if (program.pid) {
let pidPath = program.pid;
if (!/\//.test(pidPath)) {
if (!/\.pid/.test(pidPath)) {
pidPath += '.pid';
}
pidPath = `/tmp/${pidPath}`;
}
fs.writeFile(pidPath, `${process.pid.toString()}\n`, (err) => {
if (err) {
console.error(`Can't write PID file: ${err}`);
throw err;
}
// Add the cleanup for the fs onto the shutdown.
util.onshutdown([
() => new Promise((resolve, reject) => {
// Remove the pid file.
fs.unlink(pidPath, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
})
]);
});
}
// Perform rewrites to the runtime environment variables based on the contents
// of the process.env.REWRITE_ENV if it exists. This is done here as it is the
// entrypoint for the entire application.
@@ -43,4 +80,15 @@ program
// If there is no command listed, output help.
if (!process.argv.slice(2).length) {
program.outputHelp();
return;
}
// The ensures that the child process that is created here is always cleaned up
// properly when the parent process dies.
util.onshutdown([
(signal) => {
if ((program.runningCommand.killed === false) && (program.runningCommand.exitCode === null)) {
program.runningCommand.kill(signal);
}
}
]);