background_dispatch.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from fastapi import APIRouter, HTTPException
  2. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  3. from backend.app.core.permissions import Permission
  4. from backend.app.models.user import User
  5. from backend.app.services.background_dispatch import background_dispatch
  6. router = APIRouter(prefix="/background-dispatch", tags=["background-dispatch"])
  7. @router.delete("/{job_id}")
  8. async def cancel_dispatch_job(
  9. job_id: int,
  10. _: User | None = RequirePermissionIfAuthEnabled(Permission.PRINTERS_CONTROL),
  11. ):
  12. """Cancel a background-dispatch job.
  13. Queued jobs are cancelled immediately. Active jobs are marked for
  14. cooperative cancellation and will stop at the next cancellation checkpoint.
  15. """
  16. result = await background_dispatch.cancel_job(job_id)
  17. if not result["cancelled"]:
  18. raise HTTPException(status_code=404, detail="Dispatch job not found")
  19. return {
  20. "status": "cancelling" if result.get("pending") else "cancelled",
  21. "job_id": result["job_id"],
  22. "source_name": result["source_name"],
  23. "printer_id": result["printer_id"],
  24. "printer_name": result["printer_name"],
  25. }