feat: stream import progress over SSE
This commit is contained in:
parent
cc9129e7dd
commit
3b277cfded
1 changed files with 20 additions and 0 deletions
|
|
@ -1,10 +1,15 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from fastapi.responses import StreamingResponse
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from src.core.db import get_session
|
from src.core.db import get_session
|
||||||
|
from src.core.db import get_session_factory
|
||||||
from src.services import imports as svc
|
from src.services import imports as svc
|
||||||
|
|
||||||
router = APIRouter(prefix="/projects/{project_id}/imports", tags=["imports"])
|
router = APIRouter(prefix="/projects/{project_id}/imports", tags=["imports"])
|
||||||
|
|
@ -40,3 +45,18 @@ async def list_imports(project_id: str, db: AsyncSession = Depends(get_session))
|
||||||
@router.get("/{job_id}")
|
@router.get("/{job_id}")
|
||||||
async def get_import(project_id: str, job_id: str, db: AsyncSession = Depends(get_session)):
|
async def get_import(project_id: str, job_id: str, db: AsyncSession = Depends(get_session)):
|
||||||
return await svc.get_job(project_id, job_id, db)
|
return await svc.get_job(project_id, job_id, db)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{job_id}/events")
|
||||||
|
async def import_events(project_id: str, job_id: str):
|
||||||
|
async def stream():
|
||||||
|
factory = get_session_factory()
|
||||||
|
while True:
|
||||||
|
async with factory() as db:
|
||||||
|
data = await svc.get_job(project_id, job_id, db)
|
||||||
|
yield f"event: progress\ndata: {json.dumps(data)}\n\n"
|
||||||
|
if data["status"] in {"done", "error"}:
|
||||||
|
break
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
return StreamingResponse(stream(), media_type="text/event-stream",
|
||||||
|
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue