Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with fastify #32

Open
cjroebuck opened this issue Nov 27, 2024 · 2 comments
Open

Not working with fastify #32

cjroebuck opened this issue Nov 27, 2024 · 2 comments

Comments

@cjroebuck
Copy link

cjroebuck commented Nov 27, 2024

It doesn't seem to work with fastify (v5) anymore - just shuts down even though there are inflight requests.

Example server :

import Fastify from "fastify";
import GracefulServer from "@gquittet/graceful-server";
import { setTimeout } from "timers/promises";
let fastify = Fastify({ logger: true });

const gracefulServer = GracefulServer(fastify.server);

gracefulServer.on(GracefulServer.READY, () => {
  fastify.log.info("Server is ready");
});

gracefulServer.on(GracefulServer.SHUTTING_DOWN, () => {
  console.log("Server is shutting down");
});

gracefulServer.on(GracefulServer.SHUTDOWN, (error) => {
  console.log("Server is down because of", error.message);
});

// Declare a route
fastify.get("/", async (request, reply) => {
  await setTimeout(10000);
  return { hello: "world" };
});

// Run the server!
const start = async () => {
  try {
    await fastify.listen({ port: 3000 });
    fastify.log.info(
      `server listening on ${fastify.server.address()}, pid: ${process.pid}`
    );
    gracefulServer.setReady();
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

How to recreate:

run the example server node example.js

run a curl to http://127.0.0.1:3000 curl -v http://127.0.0.1:3000/

note that the log shows an incoming request (and it should be waiting for a response for 10 seconds).

now immediately either ctrl+c on the server, or send a SIGTERM to the process' pid: kill -15 $PID

The server logs 'Server is shutting down', followed by 'Server is down because of SIGTERM' and it shuts down immediately.

The curl request gets cut and replies with curl: (52) Empty reply from server.

Expected - the server should wait for the inflight request to finish (after 10s) and then shutdown. The curl command should return 200.

@gquittet
Copy link
Owner

Hello @cjroebuck 👋

Thanks for opening the issue

I think there is a bit a misunderstanding about the goal of Graceful Server project.

Graceful Server does not wait for inflight requests.

It just stops the incoming calls, returns a 503 via readiness, runs the close promises that are setup and then stop the server.

The goal is to be sure to close all database pools (redis, postgres, etc).

If you want this feature, feel free to make a PR / I can develop it but it will not be the default behavior because in the provided example, the number of inflight calls can really be huge 😅

What do you think of this?

@cjroebuck
Copy link
Author

cjroebuck commented Nov 28, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants