diff --git a/api/apps/restful_apis/task_api.py b/api/apps/restful_apis/task_api.py index 69ff7dd405..2bd7a41802 100644 --- a/api/apps/restful_apis/task_api.py +++ b/api/apps/restful_apis/task_api.py @@ -19,7 +19,6 @@ from datetime import datetime from api.apps import login_required from api.db.services.task_service import TaskService, CANVAS_DEBUG_DOC_ID, GRAPH_RAPTOR_FAKE_DOC_ID from api.utils.api_utils import ( - get_data_error_result, get_json_result, get_request_json, validate_request, @@ -57,25 +56,6 @@ async def _cancel_task(task_id): Sets a Redis cancel flag, updates the task progress to -1 (cancelled), and marks the associated document's run status as CANCEL if applicable. """ - exists, task = TaskService.get_by_id(task_id) - if not exists: - return get_data_error_result( - code=RetCode.NOT_FOUND, - message=f"Task '{task_id}' not found.", - ) - - # A task is stoppable if it hasn't completed (progress < 1) and isn't already - # in a failed/cancelled state (progress >= 0). progress == -1 means the task - # previously failed or was cancelled. - if task.progress < 0: - return get_data_error_result( - message="Task is already in a cancelled or failed state.", - ) - if task.progress >= 1: - return get_data_error_result( - message="Task has already completed and cannot be stopped.", - ) - try: REDIS_CONN.set(f"{task_id}-cancel", "x") except Exception as e: @@ -85,6 +65,10 @@ async def _cancel_task(task_id): message="Failed to stop task", ) + exists, task = TaskService.get_by_id(task_id) + if not exists: + return get_json_result(data=True) + # Append a cancellation message so the user can see it in progress_msg. try: cancel_msg = f"\n{datetime.now().strftime('%H:%M:%S')} Task stopped by user."