Does fly know when my app decides to gracefully shutdown?

Say I have a HTTP server on fly.io. It decides by itself (not triggered by fly’s auto downscaling) that it wants to shut down. It stops listening, but still runs for 5 more minutes in order to finish its work, before finally the process shuts down.

My question is how do I tell fly.io to stop sending HTTP requests to that machine?

Have the server exit with an exit status code of 0. For example, with Node.js:

import { exit } from 'node:process';
exit(0);

Thanks for your answer. That isn’t what i asked though.

The server knows to eventually exit(0) when the cleanup process is done.

I need to know how to make fly.io stop forwarding HTTP requests to that machine since the HTTP server stopped listening anyway and entered a cleanup process and is going to exit(0) soon.

Hey @mdev

I think the easiest way would be to add a health check to your app. It should start failing as soon as you close the listening socket and the proxy will stop routing requests to this instance.

Thank you for your answer @pavel. This sounds more like the kind of solution I was looking for.

But how do immediately fail a health check? Looking at the docs it seems a health check can’t do the job because there’s still an interval to wait for. Setting the interval very low seems excessive.

Hmm, that’s not really possible.

But, once you close the listening socket, fly-proxy requests to this instance are gonna fail and the proxy that made the requests will mark the instance with “Passive health check failed” (even if you don’t configure active health checks) and retry the requests on another instance. Such a passive health check lasts for 10 seconds, so this particular edge proxy won’t attempt to contact the instance for another 10 seconds.

If you’re using the requests concurrency type, make sure to also close the existing connections. As in this case the proxy on the worker node where the instance is running is maintaining a connection pool to the instance and reusing the connections. In case you’re using the connections concurrency type, each HTTP request will create a new connection to the instance, so just closing the listening socket should be enough.

2 Likes

Amazing. Would be nice if this was documented. Thanks for your help.

One other possible solution: don’t actually shut down your web server until you exit, instead respond with a Fly-Replay: elsewhere=true header. See Dynamic Request Routing · Fly Docs

1 Like

I could use this to redirect webhooks to a bullmq queue machine when i do maintenance on my main servers :heart_eyes:. Thanks @rubys.

From How To to Questions / Help

Added machines, proxy

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.