fix(douyu): unify binding state and scheduler health
This commit is contained in:
@@ -24,9 +24,11 @@ from ...models import (
|
||||
User,
|
||||
)
|
||||
from ...permissions import ROLE_LABELS
|
||||
from ..account_binding import (
|
||||
binding_role_from_result,
|
||||
pending_binding_role_from_result,
|
||||
from ..douyu_binding import (
|
||||
BINDING_VIEW_CONFIRMED,
|
||||
BINDING_VIEW_FAILED,
|
||||
BINDING_VIEW_PENDING,
|
||||
resolve_douyu_binding,
|
||||
)
|
||||
from ..file_service import build_file_url
|
||||
from . import classification
|
||||
@@ -362,11 +364,9 @@ def _task_out(
|
||||
)
|
||||
collab_order = resolved.collab_orders.get(int(task.id))
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
bind_result_confirmed = bool(bind_result.get("bind_confirmed"))
|
||||
account_binding_confirmed = bool(
|
||||
bind_result_confirmed
|
||||
and account is not None
|
||||
and account.role_bind_status == "bound"
|
||||
binding = resolve_douyu_binding(
|
||||
bind_task,
|
||||
account=account,
|
||||
)
|
||||
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
||||
effective_last_error = _effective_task_message(task, effective_issue)
|
||||
@@ -382,12 +382,6 @@ def _task_out(
|
||||
.limit(1)
|
||||
.scalar()
|
||||
)
|
||||
# 外层只消费共享绑定规则,不再自己比较角色名或 bindAct。
|
||||
pending_role = (
|
||||
""
|
||||
if account_binding_confirmed or task.binding_status == "bound"
|
||||
else pending_binding_role_from_result(bind_result)
|
||||
)
|
||||
sale_mode = classification.normalize_sale_mode(task.sale_mode)
|
||||
availability = classification.availability_state(
|
||||
sale_mode=sale_mode,
|
||||
@@ -486,20 +480,13 @@ def _task_out(
|
||||
"taskType": bind_task.task_type,
|
||||
"status": bind_task.status,
|
||||
"message": bind_task.message,
|
||||
"qrUrl": str(bind_result.get("url") or "") if include_qr else "",
|
||||
"roleName": (
|
||||
str(account.game_name or "")
|
||||
if account_binding_confirmed and account is not None
|
||||
else binding_role_from_result(bind_result)
|
||||
),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindPhase": str(bind_result.get("bind_phase") or ""),
|
||||
"bindReadyForConfirm": bool(pending_role),
|
||||
"bindConfirmed": bool(
|
||||
account_binding_confirmed
|
||||
or bind_result.get("bind_confirmed")
|
||||
),
|
||||
"qrUrl": binding.qr_url if include_qr else "",
|
||||
"roleName": binding.effective_role_name,
|
||||
"areaName": binding.effective_area_name,
|
||||
"platName": binding.effective_plat_name,
|
||||
"bindPhase": binding.bind_phase,
|
||||
"bindReadyForConfirm": binding.status == BINDING_VIEW_PENDING,
|
||||
"bindConfirmed": binding.bind_confirmed,
|
||||
}
|
||||
if bind_task
|
||||
else None
|
||||
@@ -612,19 +599,24 @@ def _customer_binding_queue_state(
|
||||
return "failed", 0
|
||||
if bind_task is None:
|
||||
return "ready_to_start", 0
|
||||
result = bind_task.result or {}
|
||||
if bool(result.get("bind_confirmed")) or task.binding_status == "bound":
|
||||
binding = resolve_douyu_binding(
|
||||
bind_task,
|
||||
)
|
||||
if binding.status == BINDING_VIEW_CONFIRMED:
|
||||
return "confirmed", 0
|
||||
if pending_binding_role_from_result(result):
|
||||
if binding.status == BINDING_VIEW_PENDING:
|
||||
return "role_ready", 0
|
||||
if bind_task.status in {"failed", "error", "stopped"}:
|
||||
if binding.status == BINDING_VIEW_FAILED:
|
||||
return "failed", 0
|
||||
if task.binding_status == "confirming":
|
||||
return "confirming", 1000
|
||||
if bind_task.status in {"planned", "pending"}:
|
||||
return "qr_queued", 1000
|
||||
if result.get("url"):
|
||||
return "waiting_scan", 5000 if result.get("bind_polling") else 0
|
||||
if binding.qr_url:
|
||||
return (
|
||||
"waiting_scan",
|
||||
5000 if (bind_task.result or {}).get("bind_polling") else 0,
|
||||
)
|
||||
return "preparing", 1000
|
||||
|
||||
|
||||
@@ -638,16 +630,9 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
)
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
account = db.get(Account, int(task.account_id)) if task.account_id else None
|
||||
bind_result_confirmed = bool(bind_result.get("bind_confirmed"))
|
||||
account_binding_confirmed = bool(
|
||||
bind_result_confirmed
|
||||
and account is not None
|
||||
and account.role_bind_status == "bound"
|
||||
)
|
||||
pending_role = (
|
||||
""
|
||||
if account_binding_confirmed or task.binding_status == "bound"
|
||||
else pending_binding_role_from_result(bind_result)
|
||||
binding = resolve_douyu_binding(
|
||||
bind_task,
|
||||
account=account,
|
||||
)
|
||||
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
||||
lease = (
|
||||
@@ -656,8 +641,6 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
else None
|
||||
)
|
||||
lease_ready = bool(task.account_id and lease and lease.status == "active")
|
||||
binding_failed = task.binding_status == "failed"
|
||||
binding_confirmed = task.binding_status == "bound"
|
||||
queue_state, retry_after_ms = _customer_binding_queue_state(
|
||||
task,
|
||||
bind_task,
|
||||
@@ -685,18 +668,11 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"qrUrl": str(bind_result.get("url") or ""),
|
||||
"qrGeneratedAt": str(bind_result.get("qr_generated_at") or ""),
|
||||
"qrExpiresAt": str(bind_result.get("qr_expires_at") or ""),
|
||||
"roleName": (
|
||||
str(account.game_name or "")
|
||||
if account_binding_confirmed and account is not None
|
||||
else binding_role_from_result(bind_result)
|
||||
),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindReadyForConfirm": (bool(pending_role) and not binding_failed),
|
||||
"bindConfirmed": bool(
|
||||
binding_confirmed
|
||||
and (account_binding_confirmed or bind_result.get("bind_confirmed"))
|
||||
),
|
||||
"roleName": binding.effective_role_name,
|
||||
"areaName": binding.effective_area_name,
|
||||
"platName": binding.effective_plat_name,
|
||||
"bindReadyForConfirm": binding.status == BINDING_VIEW_PENDING,
|
||||
"bindConfirmed": binding.bind_confirmed,
|
||||
"queueState": queue_state,
|
||||
"retryAfterMs": retry_after_ms,
|
||||
"queueLane": bind_task.queue_lane if bind_task else "",
|
||||
|
||||
Reference in New Issue
Block a user