graceful-shutdown
Advanced tools
Comparing version 0.0.4 to 0.1.0
0.1.0 / 2014-01-20 | ||
================== | ||
* changed; rename finally(cb) -> on("shutting-down", cb) | ||
* package; add helpful fields to package.json | ||
* docs; document the 2nd argument | ||
* docs; update close event style | ||
* docs; specify net server | ||
0.0.4 / 2013-12-13 | ||
@@ -3,0 +12,0 @@ ================== |
23
index.js
'use strict'; | ||
var onceUpon = require('once-upon'); | ||
var Emitter = require('events').EventEmitter; | ||
var inherits = require('util').inherits; | ||
var debug = require('debug')('graceful-shutdown'); | ||
@@ -23,5 +25,11 @@ | ||
this.server = server; | ||
this.cb = cb; | ||
if (cb) this.on('shutting-down', cb); | ||
}; | ||
/*! | ||
* Inherit from EventEmitter | ||
*/ | ||
inherits(GracefulShutdown, Emitter); | ||
/** | ||
@@ -39,13 +47,2 @@ * Listens for `signals` emitted on the `process` to | ||
/** | ||
* Sets the callback to call upon shutdown. | ||
* | ||
* @param {Function} cb | ||
*/ | ||
GracefulShutdown.prototype.finally = function (cb) { | ||
this.cb = cb; | ||
return this; | ||
}; | ||
/*! | ||
@@ -60,4 +57,4 @@ * Shuts down the server gracefully and executes our cb | ||
this.server.close(); | ||
if (this.cb) this.cb(); | ||
this.emit('shutting-down'); | ||
}; | ||
{ | ||
"name": "graceful-shutdown", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Gracefully shutdown a server upon receiving the specified signal(s)", | ||
@@ -17,3 +17,11 @@ "main": "index.js", | ||
"mocha": "~1.15.1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/recurly/graceful-shutdown.git" | ||
}, | ||
"homepage": "https://github.com/recurly/graceful-shutdown", | ||
"bugs": { | ||
"url":"https://github.com/recurly/graceful-shutdown/issues/new" | ||
} | ||
} |
#graceful-shutdown | ||
Shuts down an `HTTP{s}Server` gracefully upon the first specified signal received by the process and executes the optional callback. | ||
Shuts down a `Server` gracefully upon the first specified signal received by the process and executes the optional callback. | ||
@@ -12,4 +12,3 @@ ### example | ||
gracefullyShutdown(server).upon('SIGINT SIGTERM').finally(function() { | ||
console.log('I am an optional callback'); | ||
gracefullyShutdown(server).upon('SIGINT SIGTERM').on('shutting-down', function() { | ||
console.log('server#close() has been called'); | ||
@@ -19,6 +18,50 @@ }); | ||
### documentation | ||
#### constructor | ||
`graceful-shutdown` exports a single constructor. It may be called with or without the `new` keyword. | ||
```js | ||
var GracefulShutdown = require('graceful-shutdown'); | ||
// these are the same: | ||
var gs = GracefulShutdown(server); | ||
var gs = new GracefulShutdown(server); | ||
``` | ||
The constructor accepts a single `net.Server` instance. | ||
#### GracefulShutdown#upon() | ||
Accepts either a space delimited list of events or an array of event names. Listeners for these events will be added to the `process`. Once any one of these events are emitted on the `process`, `server.close()` will be executed, and all registered event listeners will be removed. | ||
```js | ||
// these are the same | ||
var gs = GracefulShutdown(server).upon('SIGTERM SIGINT'); | ||
var gs = GracefulShutdown(server).upon(['SIGTERM', 'SIGINT']); | ||
``` | ||
#### Events | ||
The GracefulShutdown instance will emit a `shutting-down` event when `server.close()` has been called. | ||
Note, this is not a listener for the [server close](http://nodejs.org/api/net.html#net_server_close_callback) event. If you want to listen for the server `close` event you must add the listener yourself. | ||
```js | ||
GracefulShutdown(server).upon('SIGTERM SIGINT').on('shutting-down', function() { | ||
console.log('server.close() has been called'); | ||
}); | ||
server.on('close', console.log.bind(console, 'the server is closed')); | ||
``` | ||
An alternative way to register a callback for the `shutting-down` event is to pass it as the second argument to GracefulShutdown. | ||
```js | ||
GracefulShutdown(server, callback).upon('SITERM SIGINT'); | ||
``` | ||
### install | ||
``` | ||
> npm install graceful-shutdown | ||
npm install graceful-shutdown | ||
``` | ||
@@ -25,0 +68,0 @@ |
@@ -68,6 +68,6 @@ | ||
describe('#finally', function() { | ||
it('assigns a callback to execute when the server is closed', function(done) { | ||
describe('shutting-down event', function() { | ||
it('is emitted when the server is being closed', function(done) { | ||
var s = server(); | ||
gs(s).upon('candy gum').finally(function() { | ||
gs(s).upon('candy gum').on('shutting-down', function() { | ||
done(); | ||
@@ -78,7 +78,7 @@ }); | ||
it('callback is only executed once regardless of how many times each event fires', function(done) { | ||
it('emits once regardless of how many times each source event fires', function(done) { | ||
var s = server(); | ||
var called = 0; | ||
gs(s).upon('SIGCAKE SIGPIE SIGCOOKIE').finally(function() { | ||
gs(s).upon('SIGCAKE SIGPIE SIGCOOKIE').on('shutting-down', function() { | ||
called++; | ||
@@ -85,0 +85,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
8041
1
1
70
0
134