| 1 | # Copyright (C) 2025 AIDC-AI |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """ |
| 14 | Task management endpoints |
| 15 | |
| 16 | Endpoints for managing async tasks (checking status, canceling, etc.) |
| 17 | """ |
| 18 | |
| 19 | from typing import List, Optional |
| 20 | from fastapi import APIRouter, HTTPException, Query |
| 21 | from loguru import logger |
| 22 | |
| 23 | from api.tasks import task_manager, Task, TaskStatus |
| 24 | |
| 25 | router = APIRouter(prefix="/tasks", tags=["Tasks"]) |
| 26 | |
| 27 | |
| 28 | @router.get("", response_model=List[Task]) |
| 29 | async def list_tasks( |
| 30 | status: Optional[TaskStatus] = Query(None, description="Filter by status"), |
| 31 | limit: int = Query(100, ge=1, le=1000, description="Maximum number of tasks") |
| 32 | ): |
| 33 | """ |
| 34 | List tasks |
| 35 | |
| 36 | Retrieve list of tasks with optional filtering. |
| 37 | |
| 38 | - **status**: Optional filter by status (pending/running/completed/failed/cancelled) |
| 39 | - **limit**: Maximum number of tasks to return (default 100) |
| 40 | |
| 41 | Returns list of tasks sorted by creation time (newest first). |
| 42 | """ |
| 43 | try: |
| 44 | tasks = task_manager.list_tasks(status=status, limit=limit) |
| 45 | return tasks |
| 46 | |
| 47 | except Exception as e: |
| 48 | logger.error(f"List tasks error: {e}") |
| 49 | raise HTTPException(status_code=500, detail=str(e)) |
| 50 | |
| 51 | |
| 52 | @router.get("/{task_id}", response_model=Task) |
| 53 | async def get_task(task_id: str): |
| 54 | """ |
| 55 | Get task details |
| 56 | |
| 57 | Retrieve detailed information about a specific task. |
| 58 | |
| 59 | - **task_id**: Task ID |
| 60 | |
| 61 | Returns task details including status, progress, and result (if completed). |
| 62 | """ |
| 63 | try: |
| 64 | task = task_manager.get_task(task_id) |
| 65 | |
| 66 | if not task: |
| 67 | raise HTTPException(status_code=404, detail=f"Task {task_id} not found") |
| 68 | |
| 69 | return task |
| 70 | |
| 71 | except HTTPException: |
| 72 | raise |
| 73 | except Exception as e: |
| 74 | logger.error(f"Get task error: {e}") |
| 75 | raise HTTPException(status_code=500, detail=str(e)) |
| 76 | |
| 77 | |
| 78 | @router.delete("/{task_id}") |
| 79 | async def cancel_task(task_id: str): |
| 80 | """ |
| 81 | Cancel task |
| 82 | |
| 83 | Cancel a running or pending task. |
| 84 | |
| 85 | - **task_id**: Task ID |
| 86 | |
| 87 | Returns success status. |
| 88 | """ |
| 89 | try: |
| 90 | success = task_manager.cancel_task(task_id) |
| 91 | |
| 92 | if not success: |
| 93 | raise HTTPException(status_code=404, detail=f"Task {task_id} not found") |
| 94 | |
| 95 | return { |
| 96 | "success": True, |
| 97 | "message": f"Task {task_id} cancelled successfully" |
| 98 | } |
| 99 | |
| 100 | except HTTPException: |
| 101 | raise |
| 102 | except Exception as e: |
| 103 | logger.error(f"Cancel task error: {e}") |
| 104 | raise HTTPException(status_code=500, detail=str(e)) |
| 105 | |
| 106 |