30 lines
761 B
Python
30 lines
761 B
Python
"""Job routes (REQ-070, REQ-071)."""
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from backend import jobs as job_store
|
|
|
|
router = APIRouter(prefix="/api/jobs", tags=["jobs"])
|
|
|
|
|
|
@router.get("")
|
|
def list_jobs(project_id: Optional[int] = None, limit: int = 50) -> dict:
|
|
return {"jobs": [job.to_dict() for job in job_store.listing(project_id, limit)]}
|
|
|
|
|
|
@router.get("/{job_id}")
|
|
def get_job(job_id: int) -> dict:
|
|
job = job_store.get(job_id)
|
|
if job is None:
|
|
raise HTTPException(404, "No such job")
|
|
return job.to_dict()
|
|
|
|
|
|
@router.post("/{job_id}/cancel")
|
|
def cancel_job(job_id: int) -> dict:
|
|
if not job_store.cancel(job_id):
|
|
raise HTTPException(400, "Job is not cancellable")
|
|
return {"ok": True}
|