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

use npid for pid file support #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
//
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other paths in config (ex: ssl key), this should not be relative to HOME folder (+ it's a limitation).

// @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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you can see here @JocelynDelalande, although paths are relative to HOME, I use the resolve function, which respects absolute paths when given.

IE, if you specify pidFile: 'shout.pid' vs pidFile: '/shout.pid' the first will be resolved to the home path, and the second will be used verbatim


// 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