最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

docker - FastAPI Container is flipping up & down when SQL Server is down - Stack Overflow

programmeradmin4浏览0评论

I have a critical FastAPI Python app that has one get endpoint /abc which gets certain data. When user requests this endpoint, it connects to the Microsoft SQL server to retrieve some info. The SQL Server sits on another VM, and in case if that VM is down, it should return some basic info instead of specific user_data.

Now problem is. when that VM is down, docker container starts flipping up & down, basic health endpoint is not reachable (as a whole app). Although I am catching exceptions, I cannot understand why it is happening the way it does. Prometheus shows container is down.

Below is just a high level code snippet (as it sits on company's PC, cannot get it)

from sqlalchemy import create_engine
@router.get(/abc)
async def serve_abc(request: Request):
    try:
        ip = request.client.host
        user_data = get_user_data(ip)   <---- **here I make a request to a db**
        return user_data
    
    except (HTTPException, Exception) as e:
        logger.error("an error occurred")
        return basic_data
         
def get_user_data(ip: str):
    sql_query="....."
    
    try:
        connection_string=f"mssql+pymssql://(creds)...."
        engine = create_engine(connection_string)
        with engine.connect() as conn:
             df = pd.read_sql(sql_query, conn)
             return df.to_dict(orient='records)
    except pymssql.OperationalError as e:       <-------this error happens when VM is down
        logger.error(blablabla)
        raise                  <--------- I have tried just returning [] as well

    except Exception as e:
        logger.error(blablalba)
        raise

There is a global exception handler as well.

Container is unreachable, load balancer is flipping up and down status all the time.

发布评论

评论列表(0)

  1. 暂无评论