修复虎牙任务台卡死与轮询过重,并调整默认端口
默认端口改为后端 8800 / 前端 5174,避免多项目冲突。 清理无执行器的残留 running 任务,支持停止无效批次;绑定二维码结果页不再被假活跃批次锁死。 任务列表默认剥离 base64 小程序码,按需拉取详情,空闲轮询降频,显著降低 tasks 请求体积。
This commit is contained in:
@@ -51,6 +51,7 @@ from ..services.huya_service import (
|
||||
HUYA_CONFIG_FIELDS,
|
||||
SUPPORTED_TASK_TYPES,
|
||||
apply_huya_config_defaults,
|
||||
cleanup_orphan_huya_tasks,
|
||||
create_planned_tasks,
|
||||
ensure_huya_config,
|
||||
huya_config_value,
|
||||
@@ -133,7 +134,23 @@ def _account_out(account: HuyaAccount, include_cookie: bool = True) -> HuyaAccou
|
||||
)
|
||||
|
||||
|
||||
def _task_out(task: HuyaTask) -> HuyaTaskOut:
|
||||
def _sanitize_task_result(result: dict | None, *, include_images: bool = False) -> dict | None:
|
||||
"""列表接口默认剥离 base64 图片,避免轮询每次传 1MB+ 数据。"""
|
||||
if not isinstance(result, dict):
|
||||
return result
|
||||
|
||||
data = dict(result)
|
||||
image = data.get("mini_qrcode_image")
|
||||
if isinstance(image, str) and image:
|
||||
data["has_mini_qrcode"] = True
|
||||
if not include_images:
|
||||
data.pop("mini_qrcode_image", None)
|
||||
elif data.get("has_mini_qrcode"):
|
||||
data["has_mini_qrcode"] = True
|
||||
return data
|
||||
|
||||
|
||||
def _task_out(task: HuyaTask, *, include_images: bool = False) -> HuyaTaskOut:
|
||||
account = task.account
|
||||
return HuyaTaskOut(
|
||||
id=task.id,
|
||||
@@ -144,7 +161,7 @@ def _task_out(task: HuyaTask) -> HuyaTaskOut:
|
||||
task_type=task.task_type,
|
||||
status=task.status or "",
|
||||
message=task.message or "",
|
||||
result=task.result,
|
||||
result=_sanitize_task_result(task.result if isinstance(task.result, dict) else None, include_images=include_images),
|
||||
created_by=task.created_by,
|
||||
created_at=task.created_at,
|
||||
finished_at=task.finished_at,
|
||||
@@ -952,6 +969,16 @@ async def create_task_batch(
|
||||
"""创建虎牙任务记录并启动后台执行器。"""
|
||||
if not req.account_ids:
|
||||
raise HTTPException(status_code=400, detail="请选择虎牙账号")
|
||||
|
||||
# 先清掉没有执行器的历史 running/pending,避免前端被假活跃批次锁死。
|
||||
# 不清理 planned:避免与刚创建的新任务产生竞态。
|
||||
cleanup_orphan_huya_tasks(
|
||||
db,
|
||||
active_batch_ids=huya_batch_registry.active_ids(),
|
||||
statuses=("pending", "running"),
|
||||
message="任务已中断(无执行器接管)",
|
||||
)
|
||||
|
||||
try:
|
||||
batch_id, count = create_planned_tasks(
|
||||
db,
|
||||
@@ -988,27 +1015,71 @@ async def create_task_batch(
|
||||
@router.get("/tasks", response_model=list[HuyaTaskOut])
|
||||
def list_tasks(
|
||||
batch_id: str | None = None,
|
||||
include_images: bool = Query(False, description="是否返回 base64 小程序码(默认否,轮询请保持 false)"),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("huya:task")),
|
||||
):
|
||||
"""查看虎牙任务记录。"""
|
||||
cleanup_orphan_huya_tasks(
|
||||
db,
|
||||
active_batch_ids=huya_batch_registry.active_ids(),
|
||||
statuses=("pending", "running"),
|
||||
message="任务已中断(无执行器接管)",
|
||||
)
|
||||
query = db.query(HuyaTask).options(joinedload(HuyaTask.account))
|
||||
if batch_id:
|
||||
query = query.filter(HuyaTask.batch_id == batch_id)
|
||||
tasks = query.order_by(HuyaTask.id.desc()).limit(300).all()
|
||||
return [_task_out(task) for task in tasks]
|
||||
return [_task_out(task, include_images=include_images) for task in tasks]
|
||||
|
||||
|
||||
@router.get("/tasks/{task_id}", response_model=HuyaTaskOut)
|
||||
def get_task(
|
||||
task_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("huya:task")),
|
||||
):
|
||||
"""获取单条虎牙任务详情(含二维码图片)。"""
|
||||
task = (
|
||||
db.query(HuyaTask)
|
||||
.options(joinedload(HuyaTask.account))
|
||||
.filter(HuyaTask.id == task_id)
|
||||
.first()
|
||||
)
|
||||
if not task:
|
||||
raise HTTPException(status_code=404, detail="任务不存在")
|
||||
return _task_out(task, include_images=True)
|
||||
|
||||
|
||||
@router.post("/stop/{batch_id}")
|
||||
def stop_batch(
|
||||
batch_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("huya:task")),
|
||||
):
|
||||
"""停止正在运行的虎牙批次。"""
|
||||
batch = huya_batch_registry.get(batch_id)
|
||||
if batch:
|
||||
if batch.get("finished"):
|
||||
huya_batch_registry.pop(batch_id)
|
||||
cleaned = cleanup_orphan_huya_tasks(
|
||||
db,
|
||||
batch_id=batch_id,
|
||||
message="批次已结束",
|
||||
)
|
||||
if cleaned:
|
||||
return {"message": f"批次已结束,已清理 {cleaned} 个残留任务", "success": True}
|
||||
raise HTTPException(status_code=404, detail="批次已结束")
|
||||
batch["runner"].stop()
|
||||
return {"message": "已发送停止信号", "success": True}
|
||||
|
||||
cleaned = cleanup_orphan_huya_tasks(
|
||||
db,
|
||||
batch_id=batch_id,
|
||||
message="任务已停止(批次不存在)",
|
||||
)
|
||||
if cleaned:
|
||||
return {"message": f"已清理 {cleaned} 个残留任务", "success": True}
|
||||
raise HTTPException(status_code=404, detail="批次不存在或已结束")
|
||||
|
||||
|
||||
@@ -1042,4 +1113,6 @@ async def ws_huya_logs(websocket: WebSocket, batch_id: str):
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
finally:
|
||||
huya_batch_registry.pop(batch_id)
|
||||
latest = huya_batch_registry.get(batch_id)
|
||||
if latest and latest.get("finished"):
|
||||
huya_batch_registry.pop(batch_id)
|
||||
|
||||
Reference in New Issue
Block a user