(body: CreateJobRequest)
| 86 | |
| 87 | @app.post("/api/jobs", status_code=status.HTTP_201_CREATED) |
| 88 | def enqueue_job(body: CreateJobRequest): |
| 89 | if body.task not in TASK_REGISTRY: |
| 90 | raise HTTPException(status_code=400, detail=f"Unknown task: {body.task}") |
| 91 | |
| 92 | celery_task, params_model = TASK_REGISTRY[body.task] |
| 93 | |
| 94 | try: |
| 95 | validated = params_model(**body.params) |
| 96 | except ValidationError as exc: |
| 97 | msg = exc.errors()[0].get("msg", "Invalid parameters") |
| 98 | raise HTTPException(status_code=400, detail=msg) from exc |
| 99 | |
| 100 | params = validated.model_dump() |
| 101 | job_id = uuid4().hex[:8] |
| 102 | create_job(job_id, task=body.task, params=params) |
| 103 | |
| 104 | try: |
| 105 | result = celery_task.apply_async( |
| 106 | args=[job_id], kwargs=params, task_id=job_id, queue=QUEUE_NAME, |
| 107 | ) |
| 108 | except Exception as exc: |
| 109 | update_job(job_id, status="failed", result={"error": str(exc)}) |
| 110 | raise HTTPException(status_code=500, detail="Failed to enqueue task.") from exc |
| 111 | |
| 112 | update_job(job_id, message_id=str(result.id), queue_name=QUEUE_NAME) |
| 113 | return { |
| 114 | "id": job_id, |
| 115 | "messageId": str(result.id), |
| 116 | "queueName": QUEUE_NAME, |
| 117 | "taskName": body.task, |
| 118 | } |
nothing calls this directly
no test coverage detected