hemerajs / fastify-graceful-shutdown Goto Github PK
View Code? Open in Web Editor NEWGracefully shutdown fastify
License: MIT License
Gracefully shutdown fastify
License: MIT License
Seeing issues when trying to register graceful shutdown, node is complaining that it's not a function
Steps to reproduce the behavior:
See attachments
const fastify = require('fastify')({
logger: true
})
fastify.register(require('fastify-graceful-shutdown'));
// fastify.register(require('./our-first-route'))
fastify.gracefulShutdown((signal, next) => {
console.log("fastify.gracefulShutdown")
console.log(signal)
next()
})
// Run the server!
fastify.listen(9000, '0.0.0.0', function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
})
I expect it to work
Could you please publish npm package version with fastify >=4 support 5be42c0)?
You might want to provide some feedback to the user that the signal is being handled internally
the handlers
array is a global
When running on Kubernetes, there is uncertainty regarding the order of processing between iptable changes and SIGTERM, which may lead to a situation where routing is closed immediately after receiving SIGTERM, causing new requests to receive a 503 error. We have confirmed that using preClose in fastify does not have any effect since all routing is already closed at that point.
I wanted to introduce a few seconds of wait time using fastify.gracefulShutdown, but it seems ineffective because the handlers using fastify.close are executed in parallel.
Alternatively, should I consider solving this issue by using the preStop container lifecycle hook in Kubernetes to introduce a sleep period?
fastify.gracefulShutdownで数秒の待機時間を入れたいと思ったのだが、fastify.closeをしているハンドラーが並列で実行されているためうまくいかない。
それとも単純にk8sのcontainer lifecycle hooksのpreStopでsleepさせることで解決するべきでしょうか?
Hi,
could you explain why it is not possible to register multiple handlers?
Monitoring-Tools like Dynatrace register handlers as well, so it is not possible to use them along with this plugin.
Is it a technical limitation of NodeJS or is it not possible because of the implementation details of the plugin?
Thanks
Björn
If you do not do this, and the application is using something else, you might get some very inconsistent behavior
I've tried adding this to my fastify v3 server like in the readme. However, I'm finding that this doesn't wait for any in-flight requests to terminate before shutting down the app and also does not wait for the timeout if the in flight requests do not finish in time. It seems that my fastify server just shuts down immediately even with this registered.
When testing this plugin, I find that it only works (fastify v4) when I start the server, then ctrl-c immediately to shut it down. It gracefully shuts down correctly and runs any onClose
hooks I have.
If I start the server, make a request hit to a route handler, and then ctrl-c to shut it down, it gets stuck and always waits for the 10s timeout.
This happens even if that route handler does nothing at all.
Hey Guys,
If possible can you please add support for Fastify 3.0
It's just a doubt. Is this code working? Not showing 'Server closed'. and 'Postgres closed.' in the console.
import FastifyGracefulShutdown from 'fastify-graceful-shutdown';
fastify.register(FastifyGracefulShutdown).after((err) => {
fastify.log.error(err);
fastify.gracefulShutdown(async (signal: string, next: (err?: Error | undefined) => void) => {
try {
fastify.log.info(signal + ' received...');
fastify.log.info('Closing postgres...');
await postgres.end();
fastify.log.info('Postgres closed.');
next();
} catch (err: any) {
fastify.log.error(err);
next(err);
}
});
});
{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"received signal"}
{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"triggering close hook"}
{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","msg":"SIGINT received..."}
{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","msg":"Closing postgres..."}
{"level":50,"time":1659923357654,"pid":6696,"hostname":"dubeux","plugin":"fastify-graceful-shutdown","signal":"SIGINT","timeout":10000,"msg":"terminate process after timeout"}
This would be a useful plugin to use in Fastify 4.0.0
Currently get a version error when trying to use it with the latest version.
With simple demo like below.
const app = Fastify({ logger: true });
app.register(FastifyGracefulShutdown).after(() => {
app.gracefulShutdown((code, cb) => {
console.log(code); // this line works
app.log.info(code); // this line not working
cb();
});
});
In logs I could find line like
{"level":30,"time": (time),"pid":(pid),"hostname":(hostname),"plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"triggering close hook"}
But in the graceful shutdown handler, it could not log into app.log. I think it would make more sense to log some shutdown process within it.
Hey guys,
Add fastify 2.0 support
a double Ctrl-C should kill the process.
As titled, in some bad situations the close sequence might be stuck
When setting Fastify's logger option to false
, an error is thrown when Fastify is terminated. The error message is TypeError: logger.flush is not a function
.
false
in the configuration.with codes in master, thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.