Skip to content

Commit

Permalink
Merge pull request #1 from elliotttf/config
Browse files Browse the repository at this point in the history
Config
  • Loading branch information
elliotttf committed Feb 22, 2016
2 parents fd6511e + e865b9e commit 6be65a2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
newrelic.js
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"block-scoped-var": 2,
"camelcase": 2,
"comma-dangle": [2, "never"],
"comma-dangle": 2,
"complexity": 2,
"consistent-return": 2,
"curly": 2,
Expand All @@ -19,6 +18,7 @@
"eqeqeq": 2,
"guard-for-in": 2,
"indent": [2, 2],
"keyword-spacing": 2,
"no-alert": 2,
"no-caller": 2,
"no-div-regex": 2,
Expand Down Expand Up @@ -64,7 +64,6 @@
"no-warning-comments": 1,
"quotes": [2, "single"],
"radix": 2,
"space-after-keywords": [2, "always"],
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
"space-infix-ops": 2,
"object-curly-spacing": [2, "never"],
Expand Down
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
sudo: false
node_js:
- "4.2"
- "4"
- "node"
after_success: gulp coveralls
script: npm run lint && npm test && npm run coverage
after_success: npm run coveralls

36 changes: 0 additions & 36 deletions gulpfile.js

This file was deleted.

21 changes: 15 additions & 6 deletions lib/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ function eachWorker(cb) {
* SIGINT and allows them tiem to shut down. If a work has not shut down after
* 10 seconds it will be forcekilled. After all workers have disconnected or
* been killed the process will exit.
*
* @param {int} killTimeout
* Time in milliseconds to wait before forcefully killing a child process.
*/
function sigint() {
function sigint(killTimeout) {
let shutdowns = eachWorker(worker => new Promise(resolve => {
let timeout = setTimeout(() => {
if (workerSockets[worker.process.pid]) {
workerSockets[worker.process.pid].end();
}
worker.kill();
resolve();
}, 10000);
}, killTimeout);
worker.on('disconnect', () => {
if (workerSockets[worker.process.pid]) {
workerSockets[worker.process.pid].end();
Expand Down Expand Up @@ -78,18 +81,24 @@ module.exports = {
*
* @param {string} path
* (optional) The socket path to use. Defaults to /var/run/adios.sock
* @param {object} config
* (optional) A configuration object for the master process. Contains:
* - timeout: time in milliseconds before a child will be force closed.
* Default: 10000, 10 seconds.
*
* @return {Promise}
* Resolves when the server is listening.
*/
init(path) {
init(path, config) {
if (!cluster.isMaster) {
throw new Error('Adios master must be initialized from a master process');
}
else if (server) {
throw new Error('Adios can only be initialized once per process');
}

const timeout = (config && config.timeout) || 10000;

return new Promise(resolve => {
server = net.createServer(c => {
let pid;
Expand All @@ -109,7 +118,7 @@ module.exports = {
});
server.listen(path || DEFAULT_PATH, resolve);

process.on('SIGINT', sigint);
process.on('SIGINT', () => sigint(timeout));
process.on('SIGTERM', sigterm);
});
},
Expand All @@ -122,8 +131,8 @@ module.exports = {
* Resolves when the server has been destroyed.
*/
destroy() {
process.removeListener('SIGINT', sigint);
process.removeListener('SIGTERM', sigterm);
process.removeAllListeners('SIGINT');
process.removeAllListeners('SIGTERM');
workerSockets = {};

if (server) {
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "adios",
"version": "1.0.1",
"version": "1.0.2",
"description": "A simple module for handling shutdowns within a clustered application.",
"main": "lib/adios.js",
"scripts": {
"test": "gulp test",
"lint": "gulp lint"
"test": "istanbul cover --print both nodeunit ./test/index.js",
"lint": "eslint .",
"coverage": "istanbul check-coverage --statements 100 --lines 100 --branches 81 --functions 100",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
"repository": {
"type": "git",
Expand All @@ -21,12 +23,11 @@
"node": ">= 4.2"
},
"devDependencies": {
"coveralls": "^2.11.6",
"eslint": "^2.2.0",
"ghooks": "^1.0.1",
"gulp": "^3.9.0",
"gulp-coveralls": "^0.1.4",
"gulp-eslint": "^1.1.1",
"gulp-istanbul": "^0.10.3",
"gulp-nodeunit": "0.0.5",
"istanbul": "^0.4.2",
"nodeunit": "^0.9.1",
"sinon": "^1.17.2"
},
"config": {
Expand Down
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const Adios = require('../');
module.exports = {
setUp(cb) {
this.stubs = [];
this.testSock = path.join(os.tmpDir(), `${Date.now()}-adios.sock`);

this.clock = sinon.useFakeTimers();
this.origMaster = cluster.isMaster;
this.testSock = path.join(os.tmpDir(), `${Date.now()}-adios.sock`);
cb();
},
tearDown(cb) {
Expand Down
4 changes: 2 additions & 2 deletions test/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ module.exports = {
util.inherits(DummyWorker, EventEmitter);
DummyWorker.prototype.disconnect = function () {
test.ok(true, 'Disconnect called');
this.clock.tick(10000);
this.clock.tick(1);
}.bind(this);
DummyWorker.prototype.kill = function () {
test.ok(true, 'Kill called');
};
cluster.workers.foo = new DummyWorker();

Adios.master.init(this.testSock)
Adios.master.init(this.testSock, {timeout: 1})
.then(() => {
let conn = net.connect(this.testSock, () => {
conn.write('pid:foo', () => {
Expand Down

0 comments on commit 6be65a2

Please sign in to comment.