Spaces:
Running
Running
feat: job store and GET /status/{job_id} endpoint
Browse files- main.py +9 -0
- processor.py +21 -0
- tests/test_main.py +20 -0
main.py
CHANGED
|
@@ -5,6 +5,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
|
| 7 |
from models import JobState, JobStatus, UploadResponse
|
|
|
|
| 8 |
|
| 9 |
BASE_DIR = Path(__file__).parent
|
| 10 |
UPLOAD_DIR = BASE_DIR / "tmp" / "uploads"
|
|
@@ -29,3 +30,11 @@ async def startup():
|
|
| 29 |
@app.get("/health")
|
| 30 |
async def health():
|
| 31 |
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
|
| 7 |
from models import JobState, JobStatus, UploadResponse
|
| 8 |
+
from processor import enqueue_job, get_job_status
|
| 9 |
|
| 10 |
BASE_DIR = Path(__file__).parent
|
| 11 |
UPLOAD_DIR = BASE_DIR / "tmp" / "uploads"
|
|
|
|
| 30 |
@app.get("/health")
|
| 31 |
async def health():
|
| 32 |
return {"status": "ok"}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@app.get("/status/{job_id}", response_model=JobStatus)
|
| 36 |
+
async def status(job_id: str):
|
| 37 |
+
job = get_job_status(job_id)
|
| 38 |
+
if job is None:
|
| 39 |
+
raise HTTPException(status_code=404, detail="Job not found")
|
| 40 |
+
return job
|
processor.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from typing import Dict
|
| 3 |
+
from models import JobState, JobStatus
|
| 4 |
+
|
| 5 |
+
_jobs: Dict[str, JobStatus] = {}
|
| 6 |
+
_queue: asyncio.Queue = asyncio.Queue()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_job_status(job_id: str) -> JobStatus | None:
|
| 10 |
+
return _jobs.get(job_id)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def create_job(job_id: str) -> JobStatus:
|
| 14 |
+
status = JobStatus(job_id=job_id, state=JobState.pending, progress=0)
|
| 15 |
+
_jobs[job_id] = status
|
| 16 |
+
return status
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
async def enqueue_job(job_id: str, input_path: str, output_path: str):
|
| 20 |
+
create_job(job_id)
|
| 21 |
+
await _queue.put((job_id, input_path, output_path))
|
tests/test_main.py
CHANGED
|
@@ -7,3 +7,23 @@ async def test_health(client):
|
|
| 7 |
response = await client.get("/health")
|
| 8 |
assert response.status_code == 200
|
| 9 |
assert response.json() == {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
response = await client.get("/health")
|
| 8 |
assert response.status_code == 200
|
| 9 |
assert response.json() == {"status": "ok"}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
async def test_status_unknown_job(client):
|
| 13 |
+
response = await client.get("/status/nonexistent-job-id")
|
| 14 |
+
assert response.status_code == 404
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
async def test_status_known_job(client, monkeypatch):
|
| 18 |
+
import processor
|
| 19 |
+
from models import JobState, JobStatus
|
| 20 |
+
|
| 21 |
+
fake_status = JobStatus(job_id="abc", state=JobState.pending, progress=0)
|
| 22 |
+
monkeypatch.setattr(processor, "_jobs", {"abc": fake_status})
|
| 23 |
+
|
| 24 |
+
response = await client.get("/status/abc")
|
| 25 |
+
assert response.status_code == 200
|
| 26 |
+
data = response.json()
|
| 27 |
+
assert data["job_id"] == "abc"
|
| 28 |
+
assert data["state"] == "pending"
|
| 29 |
+
assert data["progress"] == 0
|