Cancel a task (pending or running). Returns True if cancelled, False if already done/failed.
(self, task_id: int)
| 287 | self._conn.commit() |
| 288 | |
| 289 | def cancel_task(self, task_id: int) -> bool: |
| 290 | """ |
| 291 | Cancel a task (pending or running). |
| 292 | Returns True if cancelled, False if already done/failed. |
| 293 | """ |
| 294 | task = self.get_task(task_id) |
| 295 | if not task or task["status"] in ("done", "failed"): |
| 296 | return False |
| 297 | # Mark cancellation flag so executor can detect it |
| 298 | self.set(f"task_cancelled_{task_id}", "1") |
| 299 | if task["status"] == "running": |
| 300 | with self._lock: |
| 301 | self._conn.execute( |
| 302 | "UPDATE task_queue SET status = 'failed', result = ?, completed_at = ? WHERE id = ?", |
| 303 | ("Cancelled by user", int(time.time()), task_id), |
| 304 | ) |
| 305 | self._conn.commit() |
| 306 | return True |
| 307 | |
| 308 | def get_tasks_by_status(self, status: str) -> List[Dict]: |
| 309 | """Get all tasks with a given status.""" |
no test coverage detected