LiamKhoaLe commited on
Commit
9386ce8
·
1 Parent(s): 9effa4e

Silent Pymongo debug log. Upd db.py error solving Future attached to a different loop error

Browse files
Files changed (2) hide show
  1. app/db.py +13 -3
  2. app/main.py +3 -0
app/db.py CHANGED
@@ -1,9 +1,19 @@
1
  # app/db.py
2
- from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
3
  import os
 
 
4
 
 
5
  MONGO_URI = os.getenv("MONGODB_URI")
6
- client = AsyncIOMotorClient(MONGO_URI)
7
- MONGO_DB_NAME = os.getenv("MONGODB_DB", "querysearcher") # fallback default
 
 
 
 
 
 
 
 
8
  db = client[MONGO_DB_NAME]
9
  grid_fs_bucket = AsyncIOMotorGridFSBucket(db)
 
1
  # app/db.py
 
2
  import os
3
+ import asyncio
4
+ from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
5
 
6
+ _client: AsyncIOMotorClient | None = None
7
  MONGO_URI = os.getenv("MONGODB_URI")
8
+ MONGO_DB_NAME = os.getenv("MONGODB_DB", "querysearcher") # fallback default
9
+
10
+ def get_client() -> AsyncIOMotorClient:
11
+ global _client
12
+ if _client is None:
13
+ loop = asyncio.get_running_loop()
14
+ _client = AsyncIOMotorClient(MONGO_URI, io_loop=loop)
15
+ return _client
16
+
17
+ client = get_client()
18
  db = client[MONGO_DB_NAME]
19
  grid_fs_bucket = AsyncIOMotorGridFSBucket(db)
app/main.py CHANGED
@@ -31,6 +31,9 @@ logging.basicConfig(
31
  )
32
  logger = logging.getLogger("book-query")
33
  logger.setLevel(logging.DEBUG)
 
 
 
34
  logger.info("🚀 Starting Tutor Book Querier...")
35
 
36
 
 
31
  )
32
  logger = logging.getLogger("book-query")
33
  logger.setLevel(logging.DEBUG)
34
+ # Silence noisy pymongo logs
35
+ for noisy_module in ["pymongo", "pymongo.server_selection", "pymongo.topology", "pymongo.connection"]:
36
+ logging.getLogger(noisy_module).setLevel(logging.WARNING)
37
  logger.info("🚀 Starting Tutor Book Querier...")
38
 
39