I have a FastAPI app running in Kubernetes.
I’m trying to accomplish the following:
When the app gets a SIGTERM, it should
- no longer except new requests i.e
health/ready
should not respond 200 (say425
/503
). - Allow running tasks to complete (or run for max 30 seconds before killing).
- Wait for N (say 10) seconds after step (2) to make completely sure that everything is finished
Step (1) is rather fine it is done by:
import signal
#import all other stuff
app = create_fast_api()
shutting_down = False
signal.signal(signal.SIGTERM, set_shutdown)
def set_shutdown():
shutting_down = True
@app.get("health/ready")
def ready_check():
if shutting_down:
return "Shutting down", 425
return "OK"
but step (2) and (3) is a bit difficult. I have found some examples here and there, but I simply cannot find a way to do it.
As far as I understand, putting it in the “shutdown” part of “lifespan” won’t work since that is triggered was the very last step after the app is shutdown.
I think it could be done with some asyncio
magic like checking for running tasks, until they are all done, but I don’t know if that is the best way or if it could be done “directly” in the FastAPI app.