Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
use npid for pid file support
Browse files Browse the repository at this point in the history
  • Loading branch information
apmorton committed Dec 16, 2015
1 parent 1943b0a commit 5a36c12
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions src/command-line/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,59 @@ 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 <ip>" , "host")
.option("-P, --port <port>" , "port")
.option("-B, --bind <ip>" , "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) {
Expand Down

0 comments on commit 5a36c12

Please sign in to comment.