39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
import logging
|
|
import asyncio
|
|
from common.config.app_settings import app_settings
|
|
from beanie import init_beanie
|
|
from motor.motor_asyncio import AsyncIOMotorClient
|
|
from backend.models import backend_models
|
|
from common.probes import ProbeResult
|
|
|
|
client = AsyncIOMotorClient(
|
|
app_settings.MONGODB_URI,
|
|
serverSelectionTimeoutMS=60000,
|
|
minPoolSize=5, # Minimum number of connections in the pool
|
|
maxPoolSize=20, # Maximum number of connections in the pool
|
|
heartbeatFrequencyMS=20000, # Adjust heartbeat frequency to 20 seconds
|
|
)
|
|
|
|
def register(app):
|
|
app.debug = "auth_mongo_debug"
|
|
app.title = "auth_mongo_name"
|
|
|
|
# Configure logging for pymongo
|
|
logging.getLogger("pymongo").setLevel(logging.WARNING) # Suppress DEBUG logs
|
|
|
|
@app.on_event("startup")
|
|
async def start_database():
|
|
await initiate_database()
|
|
|
|
async def check_database_initialized() -> ProbeResult:
|
|
try:
|
|
await asyncio.wait_for(client.server_info(), timeout=5)
|
|
return ProbeResult(success=True, message="service has been initialized and ready to serve")
|
|
except Exception:
|
|
return ProbeResult(success=False, message="service is not initialized yet", data={"error": "database is not ready"})
|
|
|
|
async def initiate_database():
|
|
await init_beanie(
|
|
database=client[app_settings.MONGODB_NAME], document_models=backend_models
|
|
)
|