Skip to content

Commit

Permalink
feat: expose hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Nov 4, 2020
1 parent 0b05da8 commit 464826a
Show file tree
Hide file tree
Showing 6 changed files with 5,681 additions and 34 deletions.
42 changes: 42 additions & 0 deletions .gitignore
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
6 changes: 4 additions & 2 deletions README.md
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 -->
101 changes: 69 additions & 32 deletions index.js
100755 → 100644
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)
Loading

0 comments on commit 464826a

Please sign in to comment.