slice_jobs.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Polling endpoint for the in-memory slice-job dispatcher.
  2. POST /library/files/{id}/slice and POST /archives/{id}/slice return a
  3. job_id and a status_url pointing here. The frontend polls this until
  4. status flips to `completed` or `failed`.
  5. """
  6. from fastapi import APIRouter, Depends, HTTPException
  7. from backend.app.core.auth import require_ownership_permission
  8. from backend.app.core.permissions import Permission
  9. from backend.app.models.user import User
  10. from backend.app.services.slice_dispatch import slice_dispatch
  11. router = APIRouter(prefix="/slice-jobs", tags=["slice-jobs"])
  12. @router.get("/{job_id}")
  13. async def get_slice_job(
  14. job_id: int,
  15. # Job IDs are sequential integers and the body leaks source filenames
  16. # plus the resulting library_file_id / archive_id. Gate on the library
  17. # read permission family (own/all). NOTE: SliceJob is in-memory with no
  18. # owner field, so we cannot per-row scope; callers with either OWN or
  19. # ALL can poll any job_id. Adding owner_id to SliceJob is the proper
  20. # follow-up (out of scope for the IDOR fix train).
  21. _: tuple[User | None, bool] = Depends(
  22. require_ownership_permission(
  23. Permission.LIBRARY_READ_ALL,
  24. Permission.LIBRARY_READ_OWN,
  25. )
  26. ),
  27. ):
  28. job = slice_dispatch.get(job_id)
  29. if job is None:
  30. raise HTTPException(status_code=404, detail="Slice job not found or expired")
  31. body: dict = {
  32. "job_id": job.id,
  33. "status": job.status,
  34. "kind": job.kind,
  35. "source_id": job.source_id,
  36. "source_name": job.source_name,
  37. "created_at": job.created_at.isoformat(),
  38. "started_at": job.started_at.isoformat() if job.started_at else None,
  39. "completed_at": job.completed_at.isoformat() if job.completed_at else None,
  40. # Live progress fed by the sidecar's --pipe channel. Null when
  41. # the slicer hasn't emitted yet (early "Initializing" phase) or
  42. # the sidecar doesn't support progress (older versions).
  43. "progress": job.progress,
  44. }
  45. if job.status == "completed":
  46. body["result"] = job.result
  47. elif job.status == "failed":
  48. body["error_status"] = job.error_status
  49. body["error_detail"] = job.error_detail
  50. return body