-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for Hapi 17. node >= 8 required
- Loading branch information
Showing
7 changed files
with
106 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
coverage | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |