haha
This commit is contained in:
@@ -64,19 +64,36 @@ def task_list(request):
|
||||
ser = TaskCreateSerializer(data=data)
|
||||
ser.is_valid(raise_exception=True)
|
||||
|
||||
req = TaskCreate(**ser.validated_data)
|
||||
validated = ser.validated_data.copy()
|
||||
boss_id_raw = validated.pop("boss_id", "") or ""
|
||||
req = TaskCreate(**validated)
|
||||
target_worker_id = req.worker_id or ""
|
||||
account_name = req.account_name or ""
|
||||
|
||||
if not target_worker_id and req.account_name:
|
||||
target_worker_id = worker_manager.find_worker_by_account(req.account_name)
|
||||
# 未指定 worker_id/account_name 时,用 boss_id 自动解析
|
||||
if not target_worker_id and not account_name and boss_id_raw:
|
||||
account = BossAccount.objects.filter(boss_id=boss_id_raw.strip()).first()
|
||||
if not account:
|
||||
return api_error(
|
||||
http_status.HTTP_404_NOT_FOUND,
|
||||
f"未找到 boss_id 为 '{boss_id_raw}' 的账号",
|
||||
)
|
||||
target_worker_id = account.worker_id
|
||||
account_name = account.browser_name
|
||||
req.worker_id = target_worker_id
|
||||
req.account_name = account_name
|
||||
|
||||
if not target_worker_id and account_name:
|
||||
target_worker_id = worker_manager.find_worker_by_account(account_name)
|
||||
if not target_worker_id:
|
||||
return api_error(
|
||||
http_status.HTTP_404_NOT_FOUND,
|
||||
f"未找到拥有浏览器 '{req.account_name}' 的在线 Worker",
|
||||
f"未找到拥有浏览器 '{account_name}' 的在线 Worker",
|
||||
)
|
||||
req.worker_id = target_worker_id
|
||||
|
||||
if not target_worker_id:
|
||||
return api_error(http_status.HTTP_400_BAD_REQUEST, "请指定 worker_id 或 account_name")
|
||||
return api_error(http_status.HTTP_400_BAD_REQUEST, "请指定 worker_id、account_name 或 boss_id")
|
||||
|
||||
if not worker_manager.is_online(target_worker_id):
|
||||
return api_error(http_status.HTTP_503_SERVICE_UNAVAILABLE, f"Worker {target_worker_id} 不在线")
|
||||
|
||||
@@ -200,6 +200,7 @@ class TaskCreate(BaseModel):
|
||||
task_type: TaskType
|
||||
worker_id: Optional[str] = None
|
||||
account_name: Optional[str] = None
|
||||
boss_id: Optional[str] = None
|
||||
params: Dict[str, Any] = {}
|
||||
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ class TaskCreateSerializer(serializers.Serializer):
|
||||
task_type = serializers.CharField(max_length=64)
|
||||
worker_id = serializers.CharField(max_length=64, required=False, allow_blank=True, default="")
|
||||
account_name = serializers.CharField(max_length=128, required=False, allow_blank=True, default="")
|
||||
boss_id = serializers.CharField(max_length=128, required=False, allow_blank=True, default="")
|
||||
params = serializers.JSONField(required=False, default=dict)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user