add task status endpoint
This commit is contained in:
@@ -12,8 +12,7 @@ from .routes import cars, health, tasks
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# Жизненный цикл.
|
# Жизненный цикл.
|
||||||
persistence: PersistenceService = app.state.persistence
|
# Таблицы создаются через Alembic-миграции (сервис migrate).
|
||||||
persistence.create_tables()
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from sqlalchemy import select, func
|
|||||||
from ..deps import get_persistence
|
from ..deps import get_persistence
|
||||||
from ...storage.db import PersistenceService
|
from ...storage.db import PersistenceService
|
||||||
from ...storage.models import SyncRun
|
from ...storage.models import SyncRun
|
||||||
|
from ...worker.celery_app import celery_app
|
||||||
from ...worker.tasks import sync_vehicle_task, sync_listing_task
|
from ...worker.tasks import sync_vehicle_task, sync_listing_task
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -64,6 +65,25 @@ def start_sync_listing(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/tasks/{task_id}")
|
||||||
|
def get_task_status(task_id: str):
|
||||||
|
result = celery_app.AsyncResult(task_id)
|
||||||
|
|
||||||
|
payload: dict = {
|
||||||
|
"task_id": task_id,
|
||||||
|
"state": result.state,
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.successful():
|
||||||
|
payload["result"] = result.result
|
||||||
|
elif result.failed():
|
||||||
|
payload["error"] = str(result.result)
|
||||||
|
elif result.info is not None:
|
||||||
|
payload["meta"] = result.info
|
||||||
|
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
@router.get("/sync-runs")
|
@router.get("/sync-runs")
|
||||||
def list_sync_runs(
|
def list_sync_runs(
|
||||||
page: int = Query(1, ge=1),
|
page: int = Query(1, ge=1),
|
||||||
|
|||||||
Reference in New Issue
Block a user