From e16f514b170e719848bb7fc8d8bf809d1d613625 Mon Sep 17 00:00:00 2001 From: Austin Morton Date: Wed, 16 Dec 2015 05:58:36 -0500 Subject: [PATCH] use npid for pid file support --- defaults/config.js | 13 +++++++++++++ package.json | 1 + src/command-line/start.js | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/defaults/config.js b/defaults/config.js index 1a342d1a..48ddfb98 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -34,6 +34,19 @@ module.exports = { // bind: undefined, + // + // Enable or disable pid file creation + // + // string values specify the file path to use + // boolean values result in the default file path being used + // + // paths are relative to the 'HOME' folder + // + // @type string | boolean + // @default true + // + pidFile: true, + // // Set the default theme. // diff --git a/package.json b/package.json index a015a547..eafca0c8 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "lodash": "~2.4.1", "mkdirp": "^0.5.0", "moment": "~2.7.0", + "npid": "^0.4.0", "read": "^1.0.5", "request": "^2.51.0", "slate-irc": "~0.7.3", diff --git a/src/command-line/start.js b/src/command-line/start.js index 676fccb1..f8c394f5 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -2,6 +2,7 @@ var ClientManager = new require("../clientManager"); var program = require("commander"); var shout = require("../server"); var Helper = require("../helper"); +var path = require("path"); program .option("-H, --host " , "host") @@ -9,12 +10,51 @@ program .option("-B, --bind " , "bind") .option(" --public" , "mode") .option(" --private" , "mode") + .option(" --silent" , "suppress errors about pid file creation") .command("start") .description("Start the server") .action(function() { var users = new ClientManager().getUsers(); var config = Helper.getConfig(); var mode = config.public; + + if (config.pidFile) { + // setup exception handler to cleanup pid file and log errors + process.on("uncaughtException", function(e) { + console.log("uncaughtException: " + e.stack || e); + process.exit(1); + }); + + // setup signal handlers to cleanly exit process + // which allows the pid file to be removed + process.on("SIGINT", process.exit.bind(process, 0)); + process.on("SIGTERM", process.exit.bind(process, 0)); + + // use the default pid file name if the configuration + // variable is not a string + if (typeof config.pidFile !== "string") { + config.pidFile = "shout.pid"; + } + + // get the absolute path of the pid file to use + var pidFilePath = path.resolve(Helper.HOME, config.pidFile); + + // attempt to create the pid file or die trying + try { + var pid = require("npid").create(pidFilePath); + pid.removeOnExit(); + } catch (e) { + // spit out an error message about pid file creation + if (!program.silent) { + console.log("unable to create pid file: " + pidFilePath); + console.log(e.stack || e); + } + + // process is already running, so bail out now + process.exit(1); + } + } + if (program.public) { mode = true; } else if (program.private) {