This commit is contained in:
27942
2026-02-26 01:27:35 +08:00
parent 5e4e987313
commit 83c8f72cc7
11 changed files with 102 additions and 79 deletions

View File

@@ -9,9 +9,9 @@ import logging
from asgiref.sync import async_to_sync
from rest_framework import status as http_status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from common.protocol import TaskStatus, TaskType
from server.core.response import api_success, api_error
from server.models import BossAccount, TaskCreate
from server.serializers import TaskCreateSerializer, TaskOutSerializer
from server.core.worker_manager import worker_manager
@@ -49,7 +49,7 @@ def task_list(request):
limit = int(request.query_params.get("limit", 50))
task_status = TaskStatus(st) if st else None
tasks = task_dispatcher.list_tasks(worker_id=wid, status=task_status, limit=limit)
return Response([_task_to_dict(t) for t in tasks])
return api_success([_task_to_dict(t) for t in tasks])
# POST: 提交新任务
data = request.data.copy()
@@ -70,16 +70,16 @@ def task_list(request):
if not target_worker_id and req.account_name:
target_worker_id = worker_manager.find_worker_by_account(req.account_name)
if not target_worker_id:
return Response(
{"detail": f"未找到拥有浏览器 '{req.account_name}' 的在线 Worker"},
status=http_status.HTTP_404_NOT_FOUND,
return api_error(
http_status.HTTP_404_NOT_FOUND,
f"未找到拥有浏览器 '{req.account_name}' 的在线 Worker",
)
if not target_worker_id:
return Response({"detail": "请指定 worker_id 或 account_name"}, status=http_status.HTTP_400_BAD_REQUEST)
return api_error(http_status.HTTP_400_BAD_REQUEST, "请指定 worker_id 或 account_name")
if not worker_manager.is_online(target_worker_id):
return Response({"detail": f"Worker {target_worker_id} 不在线"}, status=http_status.HTTP_503_SERVICE_UNAVAILABLE)
return api_error(http_status.HTTP_503_SERVICE_UNAVAILABLE, f"Worker {target_worker_id} 不在线")
req.worker_id = target_worker_id
task = task_dispatcher.create_task(req)
@@ -88,11 +88,11 @@ def task_list(request):
if not send_fn:
task.status = TaskStatus.FAILED
task.error = "Worker WebSocket 连接不存在"
return Response({"detail": "Worker WebSocket 连接不存在"}, status=http_status.HTTP_503_SERVICE_UNAVAILABLE)
return api_error(http_status.HTTP_503_SERVICE_UNAVAILABLE, "Worker WebSocket 连接不存在")
success = async_to_sync(task_dispatcher.dispatch)(task, send_fn)
if not success:
return Response({"detail": f"任务派发失败: {task.error}"}, status=http_status.HTTP_503_SERVICE_UNAVAILABLE)
return api_error(http_status.HTTP_503_SERVICE_UNAVAILABLE, f"任务派发失败: {task.error}")
worker_manager.set_current_task(target_worker_id, task.task_id)
@@ -109,7 +109,7 @@ def task_list(request):
except Exception as e:
logger.warning("关联账号任务状态失败: %s", e)
return Response(_task_to_dict(task), status=http_status.HTTP_201_CREATED)
return api_success(_task_to_dict(task), http_status=http_status.HTTP_201_CREATED)
@api_view(["GET"])
@@ -117,5 +117,5 @@ def task_detail(request, task_id):
"""查询指定任务的状态和结果。"""
task = task_dispatcher.get_task(task_id)
if not task:
return Response({"detail": f"任务 {task_id} 不存在"}, status=http_status.HTTP_404_NOT_FOUND)
return Response(_task_to_dict(task))
return api_error(http_status.HTTP_404_NOT_FOUND, f"任务 {task_id} 不存在")
return api_success(_task_to_dict(task))