修复虎牙任务台卡死与轮询过重,并调整默认端口

默认端口改为后端 8800 / 前端 5174,避免多项目冲突。
清理无执行器的残留 running 任务,支持停止无效批次;绑定二维码结果页不再被假活跃批次锁死。
任务列表默认剥离 base64 小程序码,按需拉取详情,空闲轮询降频,显著降低 tasks 请求体积。
This commit is contained in:
yml2213
2026-07-24 11:38:07 +08:00
parent 69b6120ede
commit 9ec4bba0a9
13 changed files with 430 additions and 181 deletions
+57 -114
View File
@@ -22,8 +22,6 @@ HUYA_RECHARGE_SOURCE_ID = "yellowcarlist"
HUYA_RECHARGE_SCENE = 4
HUYA_PAYMENT_POLL_SECONDS = 180
HUYA_PAYMENT_POLL_INTERVAL = 3
HUYA_BIND_ROLE_POLL_SECONDS = 180
HUYA_BIND_ROLE_POLL_INTERVAL = 3
HUYA_BIND_ZT_UUID = "b02faae1"
HUYA_BIND_ROOM_ID = "30596253"
HUYA_RECHARGE_EXTRA_PRODUCTS = [
@@ -65,6 +63,11 @@ class HuyaBatchRunner:
self._stop.set()
def _push_log(self, level: str, message: str):
if level == "result":
try:
huya_batch_registry.mark_finished(self.batch_id)
except NameError:
pass
if level != "result" and message:
log_func = getattr(logger, level, logger.info)
log_func(f"[huya] {message}")
@@ -252,86 +255,6 @@ class HuyaBatchRunner:
f"&roomid={HUYA_BIND_ROOM_ID}"
)
def _wait_bind_role_result(
self,
client: HuyaHttpClient,
worker_db: Session,
task: HuyaTask,
account: HuyaAccount,
uid: int,
cookie: str,
b_act_id_int: int,
result: dict,
) -> tuple[str, dict]:
deadline = time.monotonic() + HUYA_BIND_ROLE_POLL_SECONDS
qrcode_token = str(result.get("qrcode_token") or "")
qrcode_finished = not qrcode_token
while not self._stop.is_set() and time.monotonic() < deadline:
if self._stop.wait(HUYA_BIND_ROLE_POLL_INTERVAL):
break
if qrcode_token and not qrcode_finished:
qrcode_status = client.get_livelink_qrcode_status(qrcode_token, timeout=10.0)
if qrcode_status is not None:
result["qrcode_status"] = qrcode_status
if qrcode_status["is_expired"] or qrcode_status["is_failure"]:
result.update({
"bind_phase": "qrcode_expired",
"bind_ready_for_confirm": False,
})
self._update_task_progress(worker_db, task, "running", "绑定小程序码已失效,请重新获取", result)
return "", result
if qrcode_status["is_completed"]:
qrcode_finished = True
result["bind_phase"] = "qrcode_completed"
elif qrcode_status["is_scan"]:
result["bind_phase"] = "qrcode_scanned"
else:
result["bind_phase"] = "waiting_scan"
bind_status, bind_query_result = self._resolve_bind_status(
client=client,
uid=uid,
cookie=cookie,
b_act_id_int=b_act_id_int,
)
if bind_status is None:
continue
result.update(bind_query_result)
if bind_status.status != 200:
result.update({
"bind_phase": "role_check_failed",
"bind_status": bind_status.to_dict(),
})
self._update_task_progress(worker_db, task, "running", bind_status.msg or "等待绑定角色同步", result)
continue
previous_phase = result.get("bind_phase")
ready = self._bind_ready_result(bind_status)
if not ready["role_name"] and previous_phase in {"waiting_scan", "qrcode_scanned", "qrcode_completed"}:
ready["bind_phase"] = previous_phase
result.update(ready)
role_name = ready["role_name"]
if role_name:
self._apply_role_to_account(account, bind_status, "game_queried")
self._update_task_progress(worker_db, task, "running", f"已识别角色: {role_name},待确认绑定", result)
return role_name, result
if result.get("bind_phase") == "qrcode_completed":
message = "小程序绑定已完成,等待角色同步"
elif result.get("bind_phase") == "qrcode_scanned":
message = "已扫码,等待小程序绑定完成"
else:
message = "已生成绑定小程序码,等待扫码绑定"
self._update_task_progress(worker_db, task, "running", message, result)
result.update({
"bind_phase": "role_timeout" if not self._stop.is_set() else "stopped",
"bind_ready_for_confirm": False,
})
return "", result
def _mark_task(
self,
worker_db: Session,
@@ -671,7 +594,7 @@ class HuyaBatchRunner:
return
if scheduled_at and scheduled_at.timestamp() > time.time():
if not self._wait_until(scheduled_at, uid):
self._mark_task(worker_db, task, "failed", "兑换任务已停止")
self._mark_task(worker_db, task, "stopped", "兑换任务已停止")
return
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
@@ -1037,6 +960,10 @@ class HuyaBatchRunner:
paid_message += f",支付时间 {self._format_local_time(int(payment_order['pay_time']) // 1000)}"
self._mark_task(worker_db, task, "success", paid_message, result)
return
if payment_status == "stopped":
account.status = "recharge_order_created"
self._mark_task(worker_db, task, "stopped", f"{message},已停止监听支付", result)
return
account.status = "recharge_order_created"
timeout_message = f"{message}{result['payment_status_label']}"
@@ -1167,6 +1094,7 @@ class HuyaBatchRunner:
**bind_query_result,
"bind_phase": "waiting_scan" if mini_qrcode.get("qrcode_token") else "waiting_role",
"bind_ready_for_confirm": False,
"bind_polling": False,
"bind_redirect_url": bind_redirect_url,
**role_info,
**change_state,
@@ -1181,29 +1109,7 @@ class HuyaBatchRunner:
account.game_channel = self._role_channel(bind_status) or account.game_channel
account.nickname = profile_nick or account.nickname
account.updated_at = datetime.now(timezone.utc)
self._update_task_progress(worker_db, task, "running", "已生成绑定小程序码,等待绑定角色", result)
role_name, result = self._wait_bind_role_result(
client=client,
worker_db=worker_db,
task=task,
account=account,
uid=uid,
cookie=cookie,
b_act_id_int=b_act_id_int,
result=result,
)
if role_name:
self._mark_task(worker_db, task, "success", f"已识别角色: {role_name},待确认绑定", result)
return
if result.get("bind_phase") == "stopped":
self._mark_task(worker_db, task, "failed", "任务已停止", result)
return
if result.get("bind_phase") == "qrcode_expired":
self._mark_task(worker_db, task, "failed", "绑定小程序码已失效,请重新获取", result)
return
self._mark_task(worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result)
self._mark_task(worker_db, task, "success", "已生成绑定小程序码", result)
def _execute_query_game_name(
self,
@@ -1260,6 +1166,8 @@ class HuyaBatchRunner:
**role_info,
**change_state,
**bind_query_result,
"bind_ready_for_confirm": bool(role_info["role_name"]),
"bind_phase": "role_ready" if role_info["role_name"] else "waiting_role",
}
role_name = role_info["role_name"]
if role_name:
@@ -1417,7 +1325,7 @@ class HuyaBatchRunner:
return
if self._stop.is_set():
self._mark_task(worker_db, task, "failed", "任务已停止")
self._mark_task(worker_db, task, "stopped", "任务已停止")
return
task.status = "running"
@@ -1562,6 +1470,17 @@ class HuyaBatchRegistry:
def __init__(self):
self._batches: dict[str, dict] = {}
self._lock = threading.Lock()
def _cleanup_locked(self, ttl_seconds: int = 300):
now = time.time()
expired = [
batch_id
for batch_id, batch in self._batches.items()
if batch.get("finished") and now - float(batch.get("finished_at") or now) > ttl_seconds
]
for batch_id in expired:
self._batches.pop(batch_id, None)
def register(
self,
@@ -1570,17 +1489,41 @@ class HuyaBatchRegistry:
loop: asyncio.AbstractEventLoop,
runner: HuyaBatchRunner,
):
self._batches[batch_id] = {
"log_queue": log_queue,
"loop": loop,
"runner": runner,
}
with self._lock:
self._cleanup_locked()
self._batches[batch_id] = {
"log_queue": log_queue,
"loop": loop,
"runner": runner,
"finished": False,
"finished_at": None,
}
def get(self, batch_id: str):
return self._batches.get(batch_id)
with self._lock:
self._cleanup_locked()
return self._batches.get(batch_id)
def active_ids(self) -> set[str]:
with self._lock:
self._cleanup_locked()
return {
batch_id
for batch_id, batch in self._batches.items()
if not batch.get("finished")
}
def mark_finished(self, batch_id: str):
with self._lock:
batch = self._batches.get(batch_id)
if not batch:
return
batch["finished"] = True
batch["finished_at"] = time.time()
def pop(self, batch_id: str):
return self._batches.pop(batch_id, None)
with self._lock:
return self._batches.pop(batch_id, None)
huya_batch_registry = HuyaBatchRegistry()