Coder Social home page Coder Social logo

fastify-graceful-shutdown's People

Contributors

almostagile avatar ddadaal avatar erfanium avatar kibertoad avatar ms10398 avatar starptech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fastify-graceful-shutdown's Issues

`gracefulShutdown` is not a function

🐛 Bug Report

Seeing issues when trying to register graceful shutdown, node is complaining that it's not a function

To Reproduce

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}`)
})

Expected behavior

I expect it to work

Your Environment

  • node version: 14.16.0
  • fastify version: >= 3.14.2
  • fastify graceful shutdown v: >= 3.1.0
  • os: Linux

4
3

Handling Shutdown on Kubernetes

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?

Translated from Japanese k8s上で動かす場合において、iptable変更とSIGTERMはどちらが先に処理されるか不明なため、SIGTERMが送られた直後にルーティングが閉じると新しいリクエストが503になる可能性がある。 fastifyのpreCloseはフックされた時点で全てのルーティングが閉じているため、preCloseに待機時間をいれても効果がないことは確認しています。

fastify.gracefulShutdownで数秒の待機時間を入れたいと思ったのだが、fastify.closeをしているハンドラーが並列で実行されているためうまくいかない。

それとも単純にk8sのcontainer lifecycle hooksのpreStopでsleepさせることで解決するべきでしょうか?

Question: Why is it not possible to register multiple handlers?

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

Doesn't seem to wait for timeout or in flight requests to terminate

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.

Graceful shutdown only works if fastify server started and terminates without processing any requests

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.

Just checking if this code is correct

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"}

Support Fastify 4.0.0

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.

fastify.log not functional in app.gracefulShutdown()

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.

logger.flush is not a function

Problem

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.

Steps to Reproduce

  1. Set Fastify's logger option to false in the configuration.
  2. Start the Fastify app and use ctrl + c to cancel it.
  3. Observe the error thrown referencing fastify-graceful-shutdown/index.js:45:14.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.