-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
5,681 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Dependency directories | ||
node_modules/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
*.swp | ||
|
||
.clinic |
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,5 +1,7 @@ | ||
# fastify-traps | ||
|
||
> A Fastify plugin that adds process traps to exit gracefully | ||
> A fastify plugin to close gracefully the server on SIGINT and SIGTERM signals. | ||
**This is a WIP** | ||
<!-- toc --> | ||
|
||
<!-- tocstop --> |
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,36 +1,73 @@ | ||
'use strict'; | ||
|
||
function onCloseSignal(timeout, signal) { | ||
this.log.warn(`Received Signal: ${signal}`); | ||
this.log.warn('Closing'); | ||
|
||
let timer; | ||
if (timeout) { | ||
timer = setTimeout(() => { | ||
this.log.error( | ||
`Could not close before ${timeout} ms, forcing exit` | ||
); | ||
process.exit(1); | ||
}, timeout); | ||
} | ||
'use strict' | ||
|
||
const fp = require('fastify-plugin') | ||
|
||
function onSignal (signal) { | ||
this.log.warn(`Received Signal: ${signal}`) | ||
this.log.warn('Closing') | ||
} | ||
|
||
function onClose () { | ||
this.log.warn('Closed') | ||
} | ||
|
||
function onTimeout (timeout) { | ||
this.log.error(`Could not close before ${timeout} ms, forcing exit`) | ||
} | ||
|
||
// Try to close the fastify instance gracefully | ||
this.close() | ||
.then(() => { | ||
clearTimeout(timer); | ||
process.exit(); | ||
}) | ||
.catch((error) => { | ||
clearTimeout(timer); | ||
this.log.error(error); | ||
process.exit(1); | ||
}); | ||
function onError (error) { | ||
this.log.error(error) | ||
} | ||
|
||
module.exports = function (fastify, opts, next) { | ||
opts.timeout = opts.timeout | 0; | ||
for (const signal of ['SIGINT', 'SIGTERM']) { | ||
process.on(signal, onCloseSignal.bind(fastify, opts.timeout)); | ||
function onCloseSignal ({ onSignal, onClose, onTimeout, onError, timeout }, signal) { | ||
onSignal(signal) | ||
|
||
const timer = setTimeout(() => { | ||
onTimeout(timeout) | ||
process.exit(1) | ||
}, timeout) | ||
|
||
// Try to close the fastify instance gracefully | ||
this.close() | ||
.then(() => { | ||
clearTimeout(timer) | ||
onClose() | ||
process.exit() | ||
}) | ||
.catch((error) => { | ||
clearTimeout(timer) | ||
onError(error) | ||
process.exit(1) | ||
}) | ||
} | ||
|
||
function plugin (fastify, opts, next) { | ||
const DEFAULTS = { | ||
onSignal: onSignal.bind(fastify), | ||
onClose: onClose.bind(fastify), | ||
onTimeout: onTimeout.bind(fastify), | ||
onError: onError.bind(fastify), | ||
timeout: 2000 | ||
} | ||
const config = Object.assign({}, DEFAULTS, opts) | ||
|
||
for (const func of ['onSignal', 'onClose', 'onTimeout', 'onError']) { | ||
const f = config[func] | ||
if (typeof f !== 'function') { | ||
return next(new TypeError(`${func} must be a function, received ${f}`)) | ||
} | ||
next(); | ||
}; | ||
} | ||
if (typeof config.timeout !== 'number') { | ||
return next(new TypeError(`timeout must be a number, received ${config.timeout}`)) | ||
} | ||
if (config.timeout < 1) { | ||
return next(new RangeError(`timeout must be grather than 0, received ${config.timeout}`)) | ||
} | ||
|
||
for (const signal of ['SIGINT', 'SIGTERM']) { | ||
process.once(signal, onCloseSignal.bind(fastify, config)) | ||
} | ||
next() | ||
} | ||
|
||
module.exports = fp(plugin) |
Oops, something went wrong.