|
@@ -2,7 +2,7 @@ from fastapi import FastAPI, BackgroundTasks, Request, Depends, HTTPException, H
|
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy.orm import Session
|
|
|
from typing import List, Optional
|
|
from typing import List, Optional
|
|
|
from app.config import settings
|
|
from app.config import settings
|
|
|
-from app.database import engine, Base, get_db
|
|
|
|
|
|
|
+from app.database import engine, Base, get_db, SessionLocal
|
|
|
from app.services.webhook_service import WebhookService
|
|
from app.services.webhook_service import WebhookService
|
|
|
from app.models import Project, DataVersion, DataFile
|
|
from app.models import Project, DataVersion, DataFile
|
|
|
from app import schemas
|
|
from app import schemas
|
|
@@ -20,6 +20,18 @@ logger = logging.getLogger(__name__)
|
|
|
app = FastAPI(title="Data Nexus", version="0.1.0")
|
|
app = FastAPI(title="Data Nexus", version="0.1.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+async def process_webhook_task(payload: dict):
|
|
|
|
|
+ """Background task that creates its own db session."""
|
|
|
|
|
+ db = SessionLocal()
|
|
|
|
|
+ try:
|
|
|
|
|
+ service = WebhookService(db)
|
|
|
|
|
+ await service.process_webhook(payload)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.error(f"Webhook processing failed: {e}", exc_info=True)
|
|
|
|
|
+ finally:
|
|
|
|
|
+ db.close()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def build_file_tree(files: List[DataFile]) -> list:
|
|
def build_file_tree(files: List[DataFile]) -> list:
|
|
|
"""Convert flat file list to tree structure."""
|
|
"""Convert flat file list to tree structure."""
|
|
|
tree = {}
|
|
tree = {}
|
|
@@ -87,7 +99,6 @@ def verify_webhook_signature(payload_body: bytes, signature: str) -> bool:
|
|
|
async def webhook_handler(
|
|
async def webhook_handler(
|
|
|
request: Request,
|
|
request: Request,
|
|
|
background_tasks: BackgroundTasks,
|
|
background_tasks: BackgroundTasks,
|
|
|
- db: Session = Depends(get_db),
|
|
|
|
|
x_gogs_signature: Optional[str] = Header(None)
|
|
x_gogs_signature: Optional[str] = Header(None)
|
|
|
):
|
|
):
|
|
|
body = await request.body()
|
|
body = await request.body()
|
|
@@ -102,9 +113,8 @@ async def webhook_handler(
|
|
|
except Exception:
|
|
except Exception:
|
|
|
raise HTTPException(status_code=400, detail="Invalid JSON")
|
|
raise HTTPException(status_code=400, detail="Invalid JSON")
|
|
|
|
|
|
|
|
- # Process in background
|
|
|
|
|
- service = WebhookService(db)
|
|
|
|
|
- background_tasks.add_task(service.process_webhook, payload)
|
|
|
|
|
|
|
+ # Process in background with its own db session
|
|
|
|
|
+ background_tasks.add_task(process_webhook_task, payload)
|
|
|
|
|
|
|
|
return {"status": "ok", "message": "Webhook received"}
|
|
return {"status": "ok", "message": "Webhook received"}
|
|
|
|
|
|