Skip to content

Commit

Permalink
Merge 86ae942 into a6a2ff6
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotttf committed Nov 1, 2016
2 parents a6a2ff6 + 86ae942 commit b1c7321
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 165 deletions.
80 changes: 0 additions & 80 deletions .eslintrc

This file was deleted.

28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "script"
},
"extends": "airbnb-base",
"rules": {
"brace-style": ["error", "stroustrup"],
"max-len": ["warn", {
"code": 100,
"comments": 80,
"ignoreUrls": true,
"ignorePattern": "(logger\\.|new Error\\)|new TypeError\\()",
"ignoreTrailingComments": true,
"tabWidth": 2
}],
"no-warning-comments": "warn",
"quotes": ["error", "single", {"avoidEscape": true, "allowTemplateLiterals": true}],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}],
"valid-jsdoc": "error"
}
}
28 changes: 22 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
language: node_js
sudo: false
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- "4"
- "node"
script: npm run lint && npm test && npm run coverage
after_success: npm run coveralls

- '6'
- 'node'
before_install:
- npm i -g npm@^2.0.0
before_script:
- npm prune
script: npm run lint && npm t
after_success:
- npm run coveralls
- 'curl -Lo travis_after_all.py https://git.io/travis_after_all'
- python travis_after_all.py
- export $(cat .to_export_back) &> /dev/null
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
9 changes: 7 additions & 2 deletions lib/adios.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
'use strict';

const master = require('./master');
const child = require('./child');

module.exports = {
master: require('./master'),
child: require('./child')
master,
child,
};
9 changes: 5 additions & 4 deletions lib/child.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const cluster = require('cluster');
const net = require('net');

Expand Down Expand Up @@ -34,13 +35,13 @@ module.exports = {

return new Promise((resolve, reject) => {
socket = net.connect(path || DEFAULT_PATH, () => {
socket.write('pid:' + process.pid);
socket.write(`pid:${process.pid}`);
resolve();
})
.setKeepAlive(true)
.on('data', (msg) => {
msg = msg.toString();
if (msg === 'SIGINT') {
const msgStr = msg.toString();
if (msgStr === 'SIGINT') {
cleanCb()
.then(() => {
socket.end();
Expand All @@ -56,6 +57,6 @@ module.exports = {
socket.destroy();
socket = null;
}
}
},
};

25 changes: 15 additions & 10 deletions lib/master.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const cluster = require('cluster');
const net = require('net');

Expand Down Expand Up @@ -29,10 +30,12 @@ function eachWorker(cb) {
*
* @param {int} killTimeout
* Time in milliseconds to wait before forcefully killing a child process.
*
* @return {undefined}
*/
function sigint(killTimeout) {
let shutdowns = eachWorker(worker => new Promise(resolve => {
let timeout = setTimeout(() => {
const shutdowns = eachWorker(worker => new Promise((resolve) => {
const timeout = setTimeout(() => {
if (workerSockets[worker.process.pid]) {
workerSockets[worker.process.pid].end();
}
Expand Down Expand Up @@ -65,6 +68,8 @@ function sigint(killTimeout) {

/**
* A SIGTERM listener. Force kills all workers and exits the master process.
*
* @return {undefined}
*/
function sigterm() {
eachWorker(worker => worker.kill());
Expand Down Expand Up @@ -99,13 +104,13 @@ module.exports = {

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

return new Promise(resolve => {
server = net.createServer(c => {
return new Promise((resolve) => {
server = net.createServer((c) => {
let pid;
c.on('data', msg => {
msg = msg.toString();
if (msg.indexOf('pid:') !== -1) {
pid = msg.split(':')[1];
c.on('data', (msg) => {
const msgStr = msg.toString();
if (msgStr.startsWith('pid:')) {
pid = msgStr.split(':')[1];
workerSockets[pid] = c;
}
});
Expand Down Expand Up @@ -136,7 +141,7 @@ module.exports = {
workerSockets = {};

if (server) {
return new Promise(resolve => {
return new Promise((resolve) => {
server.close(() => {
server = null;
resolve();
Expand All @@ -145,6 +150,6 @@ module.exports = {
}

return Promise.resolve();
}
},
};

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "adios",
"version": "1.1.1",
"description": "A simple module for handling shutdowns within a clustered application.",
"main": "lib/adios.js",
"scripts": {
"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"
"coveralls": "cat ./coverage/lcov.info | coveralls",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
Expand All @@ -20,15 +20,18 @@
"author": "Elliott Foster <elliottf@codebrews.com> (http://codebrews.com/)",
"license": "MIT",
"engines": {
"node": ">= 4.2"
"node": ">= 6"
},
"devDependencies": {
"coveralls": "^2.11.6",
"eslint": "^2.2.0",
"eslint": "^3.9.1",
"eslint-config-airbnb-base": "^9.0.0",
"eslint-plugin-import": "^2.0.0",
"ghooks": "^1.0.1",
"istanbul": "^0.4.2",
"nodeunit": "^0.9.1",
"sinon": "^1.17.2"
"sinon": "^1.17.2",
"semantic-release": "^4.3.5"
},
"config": {
"defaultPath": "/var/run/adios.sock",
Expand Down
9 changes: 9 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"rules": {
"class-methods-use-this": "off",
"global-require": "off",
"require-jsdoc": "off",
"import/no-extraneous-dependencies": "off"
}
}

28 changes: 18 additions & 10 deletions test/child.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
'use strict';

const cluster = require('cluster');
const net = require('net');
const sinon = require('sinon');
const Adios = require('../');

/**
* Noop
* @return {undefined}
*/
function noop() {
}

module.exports = {
tearDown(cb) {
if (this.server) {
Expand All @@ -22,7 +30,7 @@ module.exports = {
test.expect(1);
Adios.master.destroy();

Adios.child.init(console.log)
Adios.child.init(noop)
.then(test.done)
.catch(() => {
test.ok(true, 'Error connecting caught.');
Expand Down Expand Up @@ -52,36 +60,36 @@ module.exports = {
test.expect(2);

test.doesNotThrow(() => {
Adios.child.init(console.log, this.testSock);
Adios.child.init(noop, this.testSock);
}, 'Child not initialized.');

test.throws(() => {
Adios.child.init(console.log);
Adios.child.init(noop);
}, 'Child initialized twice.');

test.done();
}
},
},
comm: {
announcePid(test) {
test.expect(1);

this.server = net.createServer(c => {
c.on('data', msg => {
this.server = net.createServer((c) => {
c.on('data', (msg) => {
Adios.child.destroy();
test.equal(msg.toString().indexOf('pid:'), 0, 'Pid not announced.');
test.done();
});
});
this.server.listen(this.testSock, () => {
cluster.isMaster = false;
Adios.child.init(console.log, this.testSock);
Adios.child.init(noop, this.testSock);
});
},
sigint(test) {
test.expect(2);

this.stubs.push(sinon.stub(process, 'exit', code => {
this.stubs.push(sinon.stub(process, 'exit', (code) => {
test.equal(0, code, 'Process did not exit cleanly.');
test.done();
}));
Expand All @@ -94,7 +102,7 @@ module.exports = {
return Promise.resolve();
}, this.testSock);
});
}
}
},
},
};

3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const cluster = require('cluster');
const os = require('os');
const path = require('path');
Expand All @@ -25,6 +26,6 @@ module.exports = {
});
},
master: require('./master'),
child: require('./child')
child: require('./child'),
};

Loading

0 comments on commit b1c7321

Please sign in to comment.