tiangolo / uvicorn-gunicorn-fastapi-docker

Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@app.on_event('startup') does not appear to run in docker container - global created on startup should not be None

apteryxlabs opened this issue · comments

I am attempting to set up a motor AsyncIOMotorClient to talk to my mongo database. I'm using a docker-compose.yaml setup, which will be posted at the end of this issue. Here is the relevant main.py code snippet:

MONGO_CLIENT: AsyncIOMotorClient = None

@app.on_event("startup")
async def create_db_client():
    global MONGO_CLIENT #I get the same error if I omit this line.
    # start client here and reuse in future requests
    MONGO_CLIENT = AsyncIOMotorClient(settings.MONGO_URI)

@app.on_event("shutdown")
async def shutdown_db_client():
    MONGO_CLIENT.close()

def get_client():
    return MONGO_CLIENT

When later in my code, in another module database/validation.py, I run the following to validate a user's cryptographic master key. I get an error (posted after the code below):

async def get_validation_collection():
    from app.main import get_client
    client = get_client()
    database = client.get_database('authentication')
    collection = database.get_collection("validation")
    return client, database, collection


async def get_validation_check():
    _, _, collection = await get_validation_collection()
    check = await collection.find_one()
    return check

The error:

backend_1  |     raw_response = await run_endpoint_function(
backend_1  |   File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 148, in run_endpoint_function
backend_1  |     return await dependant.call(**values)
backend_1  |   File "/app/routes/validation.py", line 18, in validate_master_key
backend_1  |     validation_status = await validate_key(master_key_string.key_data)
backend_1  |   File "/app/database/crypto.py", line 100, in validate_key
backend_1  |     check_document = await get_validation_check()
backend_1  |   File "/app/database/validation.py", line 14, in get_validation_check
backend_1  |     _, _, collection = await get_validation_collection()
backend_1  |   File "/app/database/validation.py", line 8, in get_validation_collection
backend_1  |     database = client.get_database('authentication')
backend_1  | AttributeError: 'NoneType' object has no attribute 'get_database'

This would suggest that @app.on_event('startup') isn't being run, as that should set the MONGO_CLIENT global to be the client connection in our settings. Am I doing something wrong, or is there an issue with the underlying docker container?

The docker-compose.yaml file:

version: '3.3'

services:
  backend:
    build: ./backend
    environment:
      - BACKEND_PROJECT_NAME=${BACKEND_PROJECT_NAME}
      - BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS}
      - MONGO_USERNAME=${MONGO_USERNAME}
      - MONGO_PASSWORD=${MONGO_PASSWORD}
      - MONGO_URI=${MONGO_URI}
      - PORT=${BACKEND_PORT}
      - VALIDATION_TARGET=${VALIDATION_TARGET}
    ports:
      - ${BACKEND_PORT}:${BACKEND_PORT}
      - 27017:27017
    restart: always #always on deploy
  next:
    build: ./camaichc-next
    environment:
      - MASTER_KEY=${MASTER_KEY}
    ports:
      - 3000:3000
    depends_on:
      - backend
    restart: always #always on deploy
commented

This would suggest that @app.on_event('startup') isn't being run

I have a similar setting and the exact same issue. I have a function that must be triggered via @app.on_event('startup'), but within the docker container, that does not happen.

EDIT: I found this issue fastapi/fastapi#1480 and followed the discussion there and on the linked bug reports.

spent hours on the same issue wondering why my app was working locally when running without docker but not when deploying to test environment with docker. Any resolution for this issue ??