| 70 | |
| 71 | |
| 72 | def request_cancel(session: Session, job_id: str) -> bool: |
| 73 | job = get_job(session, job_id) |
| 74 | if not job: |
| 75 | return False |
| 76 | |
| 77 | if job.status in ("completed", "failed", "cancelled"): |
| 78 | return True |
| 79 | |
| 80 | job.cancel_requested = True |
| 81 | job.updated_at = utcnow() |
| 82 | append_event( |
| 83 | session, job.id, "cancel_requested", "warning", "Cancellation requested." |
| 84 | ) |
| 85 | |
| 86 | if job.status == "queued": |
| 87 | job.status = "cancelled" |
| 88 | job.completed_at = utcnow() |
| 89 | append_event( |
| 90 | session, job.id, "cancelled", "warning", "Job cancelled before execution." |
| 91 | ) |
| 92 | |
| 93 | session.commit() |
| 94 | return True |
| 95 | |
| 96 | |
| 97 | def claim_next_queued_job(session: Session) -> Optional[GenerationJob]: |