1

I need to code a microservice on Fast-Api which communicates with Redis and other services. If it gets SIGTERM signal, it has to work for 15s more (GET\PUT handlers) and after that shutdown gracefully.

Here is my example

from fastapi import FastAPI
app = FastAPI()

@app.on_event("startup")
async def startup_event():
    # connections to db here

@app.get('/')
async def root():
    return {"some_answer": "answer"}

@app.on_event("shutdown")
async def shutdown_event():
    # sending msg to redis 
    await asyncio.sleep(15)

Everything works good, but if i kill this process with SIGTERM signal it stops to handle GET requests, sends msg to redis and doesnt wait for 15s before stopping.

I need another order: signal -> msg to redis -> 15s normal work -> shutdown

So, my question is - is it possible not to interrupt the current execution but handle signal, send msg to db, wait for 15s and after that shutdown Fast-API ?

0 Answers0