Start a background task. Args: task_id: Task ID to start Returns: True if started successfully
(self, task_id: str)
| 106 | return task_id |
| 107 | |
| 108 | async def start_task(self, task_id: str) -> bool: |
| 109 | """ |
| 110 | Start a background task. |
| 111 | |
| 112 | Args: |
| 113 | task_id: Task ID to start |
| 114 | |
| 115 | Returns: |
| 116 | True if started successfully |
| 117 | """ |
| 118 | task = self._tasks.get(task_id) |
| 119 | if not task: |
| 120 | error(f"Background: task {task_id} not found") |
| 121 | return False |
| 122 | |
| 123 | if task.status != BackgroundTaskStatus.PENDING: |
| 124 | warning(f"Background: task {task_id} is not pending") |
| 125 | return False |
| 126 | |
| 127 | task.status = BackgroundTaskStatus.RUNNING |
| 128 | task.started_at = time.time() |
| 129 | |
| 130 | info(f"Background: starting task '{task.name}'") |
| 131 | |
| 132 | # Store asyncio.Task handle so stop_task() can cancel it |
| 133 | task._asyncio_task = asyncio.create_task(self._run_task(task)) |
| 134 | |
| 135 | return True |
| 136 | |
| 137 | async def _run_task(self, task: BackgroundTask): |
| 138 | """Execute a background task with timeout and error handling.""" |