Spaces:
Sleeping
Sleeping
Commit
·
9785201
1
Parent(s):
bb294dd
Upd safe GridFS loader
Browse files- app/db.py +10 -5
- app/routers/import_doc.py +5 -2
app/db.py
CHANGED
|
@@ -1,11 +1,16 @@
|
|
| 1 |
# app/db.py
|
| 2 |
-
import os
|
| 3 |
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
|
|
|
| 4 |
|
| 5 |
MONGO_URI = os.getenv("MONGODB_URI")
|
| 6 |
MONGO_DB_NAME = os.getenv("MONGODB_DB", "querysearcher")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app/db.py
|
|
|
|
| 2 |
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
MONGO_URI = os.getenv("MONGODB_URI")
|
| 6 |
MONGO_DB_NAME = os.getenv("MONGODB_DB", "querysearcher")
|
| 7 |
|
| 8 |
+
# Return a fresh client for current event loop
|
| 9 |
+
def get_client():
|
| 10 |
+
return AsyncIOMotorClient(MONGO_URI)
|
| 11 |
+
|
| 12 |
+
def get_db():
|
| 13 |
+
return get_client()[MONGO_DB_NAME]
|
| 14 |
+
|
| 15 |
+
def get_gridfs():
|
| 16 |
+
return AsyncIOMotorGridFSBucket(get_db())
|
app/routers/import_doc.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# app/routers/import.py
|
| 2 |
from fastapi import APIRouter, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from app.db import
|
| 5 |
from app.services import google_books, open_library, internet_archive, project_gutenberg
|
| 6 |
from app.services.ingest import parse_and_index
|
| 7 |
import aiofiles, uuid, os
|
|
@@ -60,11 +60,13 @@ async def import_book(req: ImportRequest):
|
|
| 60 |
logger.error(f"🚨 Failed to download or write PDF: {e}")
|
| 61 |
raise HTTPException(500, "Failed to download PDF")
|
| 62 |
|
| 63 |
-
# Save to bucket
|
| 64 |
try:
|
|
|
|
| 65 |
with open(file_path, "rb") as f:
|
| 66 |
await grid_fs_bucket.upload_from_stream(f"{req.candidate_id}.pdf", f)
|
| 67 |
os.remove(file_path)
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
logger.error(f"💥 Failed to upload to GridFS: {e}")
|
| 70 |
raise HTTPException(500, "Storage failed")
|
|
@@ -76,6 +78,7 @@ async def import_book(req: ImportRequest):
|
|
| 76 |
"status": "queued",
|
| 77 |
"metadata": result
|
| 78 |
}
|
|
|
|
| 79 |
await db.documents.insert_one(doc)
|
| 80 |
asyncio.create_task(parse_and_index(req.candidate_id))
|
| 81 |
logger.info(f"📚 Document {req.candidate_id} queued for indexing")
|
|
|
|
| 1 |
# app/routers/import.py
|
| 2 |
from fastapi import APIRouter, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
+
from app.db import get_db, get_gridfs
|
| 5 |
from app.services import google_books, open_library, internet_archive, project_gutenberg
|
| 6 |
from app.services.ingest import parse_and_index
|
| 7 |
import aiofiles, uuid, os
|
|
|
|
| 60 |
logger.error(f"🚨 Failed to download or write PDF: {e}")
|
| 61 |
raise HTTPException(500, "Failed to download PDF")
|
| 62 |
|
| 63 |
+
# Save to bucket using loop-safe GridFS
|
| 64 |
try:
|
| 65 |
+
grid_fs_bucket = get_gridfs()
|
| 66 |
with open(file_path, "rb") as f:
|
| 67 |
await grid_fs_bucket.upload_from_stream(f"{req.candidate_id}.pdf", f)
|
| 68 |
os.remove(file_path)
|
| 69 |
+
|
| 70 |
except Exception as e:
|
| 71 |
logger.error(f"💥 Failed to upload to GridFS: {e}")
|
| 72 |
raise HTTPException(500, "Storage failed")
|
|
|
|
| 78 |
"status": "queued",
|
| 79 |
"metadata": result
|
| 80 |
}
|
| 81 |
+
db = get_db()
|
| 82 |
await db.documents.insert_one(doc)
|
| 83 |
asyncio.create_task(parse_and_index(req.candidate_id))
|
| 84 |
logger.info(f"📚 Document {req.candidate_id} queued for indexing")
|