Skip to content

Commit

Permalink
Support for Hapi 17. node >= 8 required
Browse files Browse the repository at this point in the history
  • Loading branch information
martinj committed Dec 6, 2017
1 parent 615b0ac commit e2adac1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
package-lock.json
47 changes: 0 additions & 47 deletions .jshintrc

This file was deleted.

3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- 0.12
- 4
- 8

script:
- npm run ci
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,25 @@ This module is installed via npm:

```javascript

var Hapi = require('hapi');
const Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 3000 });
const server = new Hapi.Server();

server.register({register: require('@aptoma/hapi-graceful-stop'), options: {timeout: 2000}});

server.start(function () {
console.log('Server running at:', server.info.uri);
});
server.start();

```

Running function after `server.stop()` e.g for ending db connections.

```javascript

var Hapi = require('hapi');
const Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 3000 });
const server = new Hapi.Server();

var opts = {
const opts = {
timeout: 2000, // optional, defaults to 5000 ms
afterStopTimeout: 1000, // optional, defaults to 2000 ms
afterStop: function (done) {
Expand All @@ -45,8 +41,6 @@ var opts = {

server.register({register: require('@aptoma/hapi-graceful-stop'), options: opts});

server.start(function () {
console.log('Server running at:', server.info.uri);
});
server.start();

```
37 changes: 18 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
'use strict';

function exit(timeout, fn, server) {
if (typeof(fn) !== 'function') {
if (typeof (fn) !== 'function') {
return process.exit(0);
}

server.log('graceful-stop', 'Running options.afterStop function with timeout ' + timeout + 'ms');
server.log('graceful-stop', `Running options.afterStop function with timeout ${timeout} ms`);

setTimeout(function () {
const id = setTimeout(() => {
server.log('graceful-stop', 'options.afterStop function timedout');
process.exit(0);
}, timeout);

fn(function () {
fn(() => {
clearTimeout(id);
process.exit(0);
});
}

exports.register = function (server, options, next) {
var timeout = options.timeout || 5000;
var afterStopTimeout = options.afterStopTimeout || 2000;
module.exports = {
name: 'graceful-stop',
register(server, options) {
const timeout = options.timeout || 5000;
const afterStopTimeout = options.afterStopTimeout || 2000;

process.on('SIGINT', function () {
server.log('graceful-stop', 'Received SIGINT, initiating graceful stop with timeout ' + timeout + 'ms');
process.on('SIGINT', async () => {
server.log('graceful-stop', `Received SIGINT, initiating graceful stop with timeout ${timeout} ms`);

server.root.stop({timeout: timeout}, function () {
server.events.on('stop', () => {
exit(afterStopTimeout, options.afterStop, server);
});

await server.stop({timeout});
server.log('graceful-stop', 'Server stopped');
exit(afterStopTimeout, options.afterStop, server);
});
});

next();
};

exports.register.attributes = {
name: 'graceful-stop'
}
};

30 changes: 23 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@
"version": "1.1.0",
"description": "Hapi plugin for graceful stop",
"main": "index.js",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "jshint test/ index.js && istanbul test --preload-sources _mocha -- -u exports -R spec 'test/**/*.test.js'",
"lint": "eslint --ext '.js' test index.js",
"test": "npm run lint && istanbul test --preload-sources _mocha -- -u exports -R spec 'test/**/*.test.js'",
"ci": "npm test --coverage && istanbul report cobertura",
"release": "npm test && release-it -n -i patch",
"release:minor": "npm test && release-it -n -i minor",
"release:major": "npm test && release-it -n -i major"
},
"eslintConfig": {
"extends": "@aptoma/eslint-config",
"parserOptions": {
"ecmaVersion": "2017"
},
"env": {
"node": true,
"mocha": true,
"es6": true
}
},
"repository": {
"type": "git",
"url": "https://github.com/aptoma/hapi-graceful-stop"
Expand All @@ -23,11 +38,12 @@
"homepage": "https://github.com/aptoma/hapi-graceful-stop",
"dependencies": {},
"devDependencies": {
"hapi": "^9.3.1",
"istanbul": "^0.3.2",
"jshint": "^2.5.6",
"mocha": "^1.21.4",
"@aptoma/eslint-config": "^7.0.1",
"eslint": "^4.12.1",
"hapi": "^17.1.1",
"istanbul": "^0.4.5",
"mocha": "^4.0.1",
"release-it": "0.0.11",
"should": "^4.0.4"
"should": "^13.1.3"
}
}
}
119 changes: 56 additions & 63 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,86 @@
'use strict';
var Hapi = require('hapi');
var gracefulStop = require('..');
const Hapi = require('hapi');
const gracefulStop = require('..');
require('should');

describe('hapi-graceful-stop', function () {
beforeEach(function () {
describe('hapi-graceful-stop', () => {
const orgExit = process.exit;

beforeEach(() => {
process.removeAllListeners('SIGINT');
process.exit = () => {};
});

it('should call server.stop on SIGINT signal with configured timeout', function (done) {
var server = new Hapi.Server();
server.connection({port: 3000});
afterEach(() => {
process.exit = orgExit;
});

server.register({register: gracefulStop, options: {timeout: 500}}, function (err) {
if (err) {
return done(err);
}
it('should call server.stop on SIGINT signal', (done) => {
const server = new Hapi.Server();
server.register({plugin: gracefulStop, options: {timeout: 500}});

server.stop = function (opts) {
opts.timeout.should.equal(500);
done();
};

process.emit('SIGINT');
server.events.on('stop', () => {
done();
});

process.emit('SIGINT');
});

it('should call option.afterStop', function (done) {
var server = new Hapi.Server();
server.connection({port: 3000});
it('should call option.afterStop', (done) => {
const server = new Hapi.Server();

var opts = {
const opts = {
timeout: 500,
afterStop: function (cb) {
cb();
afterStop(cb) {
cb(); // eslint-disable-line callback-return
done();
}
};

server.register({register: gracefulStop, options: opts}, function (err) {
if (err) {
return done(err);
}

process.exit = function () {};

process.emit('SIGINT');
});
server
.register({plugin: gracefulStop, options: opts})
.then(() => {
process.emit('SIGINT');
})
.catch(done);
});

it('should call process.exit if option.afterStop times out', function (done) {
var server = new Hapi.Server();
server.connection({port: 3000});
it('should call process.exit if option.afterStop times out', (done) => {
const server = new Hapi.Server();

var opts = {
const opts = {
timeout: 500,
afterStopTimeout: 1,
afterStop: function () {}
afterStop: () => {}
};

server.register({register: gracefulStop, options: opts}, function (err) {
if (err) {
return done(err);
}

process.exit = function (code) {
code.should.equal(0);
done();
};

process.emit('SIGINT');
});
server
.register({plugin: gracefulStop, options: opts})
.then(() => {
process.exit = function (code) {
code.should.equal(0);
done();
};

process.emit('SIGINT');
})
.catch(done);
});

it('should call process.exit', function (done) {
var server = new Hapi.Server();
server.connection({port: 3000});
server.register({register: gracefulStop, options: {timeout: 1}}, function (err) {
if (err) {
return done(err);
}

process.exit = function (code) {
code.should.equal(0);
done();
};

process.emit('SIGINT');
});
it('should call process.exit', (done) => {
const server = new Hapi.Server();
server
.register({plugin: gracefulStop, options: {timeout: 1}})
.then(() => {
process.exit = function (code) {
code.should.equal(0);
done();
};

process.emit('SIGINT');
})
.catch(done);
});

});

0 comments on commit e2adac1

Please sign in to comment.