fix(douyu): unify binding state and scheduler health
This commit is contained in:
@@ -17,10 +17,12 @@ from ...models import (
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from ..account_binding import (
|
||||
pending_binding_role_from_result,
|
||||
)
|
||||
from ..business_events import record_douyu_task_started
|
||||
from ..douyu_binding import (
|
||||
BINDING_VIEW_CONFIRMED,
|
||||
BINDING_VIEW_PENDING,
|
||||
resolve_douyu_binding,
|
||||
)
|
||||
from ..execution_jobs import (
|
||||
JOB_DOUYU_BINDING,
|
||||
LANE_FULFILLMENT,
|
||||
@@ -64,6 +66,10 @@ ACCOUNT_CONFIRMATION_TASK_TYPES = frozenset(
|
||||
"confirm_xpd_bind",
|
||||
}
|
||||
)
|
||||
PENDING_BINDING_TASK_TYPES = frozenset({"get_bind_qr"})
|
||||
ACTIVE_PENDING_TASK_STATUSES = frozenset(
|
||||
{"allocated", "waiting_bind", "role_ready"}
|
||||
)
|
||||
|
||||
|
||||
def start_binding_sync(
|
||||
@@ -82,12 +88,18 @@ def start_binding_sync(
|
||||
if task.bind_task_id:
|
||||
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||
if existing:
|
||||
result = existing.result or {}
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
stale_pending = (
|
||||
bool(result.get("bind_ready_for_confirm")) and not pending_role
|
||||
binding = resolve_douyu_binding(
|
||||
existing,
|
||||
account=db.get(Account, int(task.account_id)),
|
||||
)
|
||||
if bool(result.get("bind_confirmed")) or pending_role:
|
||||
stale_pending = (
|
||||
binding.reported_ready_for_confirm
|
||||
and not binding.bind_ready_for_confirm
|
||||
)
|
||||
if binding.status in {
|
||||
BINDING_VIEW_CONFIRMED,
|
||||
BINDING_VIEW_PENDING,
|
||||
}:
|
||||
return get_task_detail(db, task.id)
|
||||
if existing.status in {"planned", "pending", "running"}:
|
||||
return get_task_detail(db, task.id)
|
||||
@@ -193,6 +205,14 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
task = _load_task(db, task_id)
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
return get_task_detail(db, task.id)
|
||||
latest_pending = _latest_pending_binding_task(db, task)
|
||||
if latest_pending is not None and _attach_pending_binding_task(
|
||||
db,
|
||||
task,
|
||||
latest_pending,
|
||||
):
|
||||
db.commit()
|
||||
task = _load_task(db, task.id)
|
||||
if not task.bind_task_id:
|
||||
return get_task_detail(db, task.id)
|
||||
bind_task = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||
@@ -206,23 +226,26 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
task.last_error,
|
||||
)
|
||||
result = bind_task.result or {}
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
binding = resolve_douyu_binding(
|
||||
bind_task,
|
||||
account=db.get(Account, int(task.account_id)) if task.account_id else None,
|
||||
)
|
||||
if bind_task.status in {"failed", "error", "stopped"}:
|
||||
task.binding_status = "failed"
|
||||
task.execution_status = "manual_required"
|
||||
task.issue_code = _binding_failure_issue(bind_task, result)
|
||||
task.last_error = _customer_task_message(task)
|
||||
elif bool(result.get("bind_confirmed")):
|
||||
elif binding.status == BINDING_VIEW_CONFIRMED:
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
elif pending_role:
|
||||
elif binding.status == BINDING_VIEW_PENDING:
|
||||
task.binding_status = "role_ready"
|
||||
task.execution_status = "role_ready"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
elif bool(result.get("bind_ready_for_confirm")):
|
||||
elif binding.reported_ready_for_confirm:
|
||||
# 兼容旧快照:同名旧角色曾被误标为待确认。清掉绑定指针后,客户页会
|
||||
# 回到已分配状态,并在下一次刷新时自动生成新二维码、重新开始轮询。
|
||||
previous_bind_task_id = task.bind_task_id
|
||||
@@ -250,7 +273,7 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
_apply_task_lease_policy(db, task)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
elif result.get("url"):
|
||||
elif binding.qr_url:
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
task.issue_code = ""
|
||||
@@ -301,15 +324,55 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
||||
douyu_task = db.get(DouyuTask, int(douyu_task_id))
|
||||
if douyu_task is None or douyu_task.task_type not in BINDING_SYNC_TASK_TYPES:
|
||||
return len(task_ids)
|
||||
result = douyu_task.result or {}
|
||||
binding = resolve_douyu_binding(
|
||||
douyu_task,
|
||||
account=db.get(Account, int(douyu_task.account_id))
|
||||
if douyu_task.account_id
|
||||
else None,
|
||||
)
|
||||
if (
|
||||
douyu_task.task_type not in ACCOUNT_CONFIRMATION_TASK_TYPES
|
||||
or result.get("bind_confirmed") is not True
|
||||
or binding.status != BINDING_VIEW_CONFIRMED
|
||||
):
|
||||
# 查询/出码任务只能证明“账号历史绑定是什么”,不能证明本次订单已经确认。
|
||||
# 只有明确执行成功并回查到 bind_confirmed 的确认任务,才允许把账号权威
|
||||
# 绑定投影到履约任务。
|
||||
return len(task_ids)
|
||||
# 只有明确执行成功并回查到 bind_confirmed 的确认任务,才允许把任务级
|
||||
# 确认事实投影到履约任务。
|
||||
linked = 0
|
||||
if (
|
||||
douyu_task.task_type in PENDING_BINDING_TASK_TYPES
|
||||
and binding.status == BINDING_VIEW_PENDING
|
||||
):
|
||||
active_task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.join(AccountLease, AccountLease.id == PlatformFulfillmentTask.lease_id)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.account_id == int(douyu_task.account_id),
|
||||
PlatformFulfillmentTask.scope == "elite",
|
||||
PlatformFulfillmentTask.binding_status.in_(
|
||||
ACTIVE_PENDING_TASK_STATUSES
|
||||
),
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
("completed", "cancelled", "manual_required")
|
||||
),
|
||||
AccountLease.status == "active",
|
||||
)
|
||||
.order_by(
|
||||
PlatformFulfillmentTask.updated_at.desc(),
|
||||
PlatformFulfillmentTask.id.desc(),
|
||||
)
|
||||
.with_for_update()
|
||||
.first()
|
||||
)
|
||||
if active_task is not None and _attach_pending_binding_task(
|
||||
db,
|
||||
active_task,
|
||||
douyu_task,
|
||||
):
|
||||
_apply_task_lease_policy(db, active_task)
|
||||
_emit_order_site_binding_state(db, active_task.id)
|
||||
db.commit()
|
||||
linked = 1
|
||||
return len(task_ids) + linked
|
||||
return len(task_ids) + sync_active_task_from_account_binding(
|
||||
db,
|
||||
account_id=int(douyu_task.account_id),
|
||||
@@ -318,6 +381,83 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
||||
)
|
||||
|
||||
|
||||
def _latest_pending_binding_task(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
) -> DouyuTask | None:
|
||||
if (
|
||||
not task.account_id
|
||||
or task.binding_status not in ACTIVE_PENDING_TASK_STATUSES
|
||||
):
|
||||
return None
|
||||
rows = (
|
||||
db.query(DouyuTask)
|
||||
.filter(
|
||||
DouyuTask.account_id == int(task.account_id),
|
||||
DouyuTask.task_type.in_(PENDING_BINDING_TASK_TYPES),
|
||||
DouyuTask.handbook_scope == "elite",
|
||||
DouyuTask.status == "success",
|
||||
)
|
||||
.order_by(DouyuTask.id.desc())
|
||||
.limit(20)
|
||||
.all()
|
||||
)
|
||||
account = db.get(Account, int(task.account_id))
|
||||
for row in rows:
|
||||
if resolve_douyu_binding(
|
||||
row,
|
||||
account=account,
|
||||
).status == BINDING_VIEW_PENDING:
|
||||
return row
|
||||
return None
|
||||
|
||||
|
||||
def _attach_pending_binding_task(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
bind_task: DouyuTask,
|
||||
) -> bool:
|
||||
account = (
|
||||
db.get(Account, int(task.account_id))
|
||||
if task.account_id
|
||||
else None
|
||||
)
|
||||
binding = resolve_douyu_binding(bind_task, account=account)
|
||||
if (
|
||||
task.binding_status not in ACTIVE_PENDING_TASK_STATUSES
|
||||
or not task.account_id
|
||||
or int(bind_task.account_id or 0) != int(task.account_id)
|
||||
or bind_task.task_type not in PENDING_BINDING_TASK_TYPES
|
||||
or binding.status != BINDING_VIEW_PENDING
|
||||
):
|
||||
return False
|
||||
current_id = int(task.bind_task_id or 0)
|
||||
if int(bind_task.id) == current_id:
|
||||
return False
|
||||
if current_id and int(bind_task.id) < current_id:
|
||||
return False
|
||||
previous_bind_task_id = task.bind_task_id
|
||||
task.bind_task_id = int(bind_task.id)
|
||||
task.binding_status = "role_ready"
|
||||
task.execution_status = "role_ready"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"binding_pending_task_linked",
|
||||
detail={
|
||||
"bindTaskId": int(bind_task.id),
|
||||
"previousBindTaskId": previous_bind_task_id,
|
||||
"roleName": binding.pending_role_name,
|
||||
"source": "account_workbench",
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return True
|
||||
|
||||
|
||||
def sync_active_task_from_account_binding(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -325,11 +465,12 @@ def sync_active_task_from_account_binding(
|
||||
scope: str,
|
||||
source_task_id: int | None = None,
|
||||
) -> int:
|
||||
"""账号权威绑定已确认时,把当前活跃履约任务同步到 bound。
|
||||
"""确认任务成功后,把当前活跃履约任务同步到 bound。
|
||||
|
||||
精英宝典工作台发出的确认任务不挂在履约任务上,因此不能只看
|
||||
``PlatformFulfillmentTask.bind_task_id``。账号上的绑定状态才是最终事实。
|
||||
只在任务已经识别出待确认角色后同步,避免把账号历史绑定误当成当前订单结果。
|
||||
``PlatformFulfillmentTask.bind_task_id``。这里的唯一事实是确认任务的
|
||||
``result``:只有 ``resolve_douyu_binding`` 返回 confirmed 才允许投影。
|
||||
账号上的 ``role_bind_status`` 只描述当前生效角色,不能证明本订单已确认。
|
||||
"""
|
||||
if scope != "elite":
|
||||
return 0
|
||||
@@ -338,15 +479,19 @@ def sync_active_task_from_account_binding(
|
||||
if source_task_id is not None
|
||||
else None
|
||||
)
|
||||
account = db.get(Account, int(account_id))
|
||||
binding = (
|
||||
resolve_douyu_binding(source_task, account=account)
|
||||
if source_task is not None
|
||||
else None
|
||||
)
|
||||
if (
|
||||
source_task is None
|
||||
or binding is None
|
||||
or source_task.task_type not in ACCOUNT_CONFIRMATION_TASK_TYPES
|
||||
or (source_task.result or {}).get("bind_confirmed") is not True
|
||||
or binding.status != BINDING_VIEW_CONFIRMED
|
||||
):
|
||||
return 0
|
||||
account = db.get(Account, int(account_id))
|
||||
if account is None or account.role_bind_status != "bound":
|
||||
return 0
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.join(AccountLease, AccountLease.id == PlatformFulfillmentTask.lease_id)
|
||||
@@ -379,13 +524,25 @@ def sync_active_task_from_account_binding(
|
||||
task.id,
|
||||
"binding_synced_from_account",
|
||||
detail={
|
||||
"accountId": int(account.id),
|
||||
"accountName": account.nickname or account.username,
|
||||
"roleName": str(account.game_name or ""),
|
||||
"gameChannel": str(account.game_channel or ""),
|
||||
"accountId": int(account_id),
|
||||
"accountName": (
|
||||
account.nickname or account.username
|
||||
if account is not None
|
||||
else ""
|
||||
),
|
||||
"roleName": (
|
||||
binding.confirmed_role_name
|
||||
or binding.effective_role_name
|
||||
or binding.current_role_name
|
||||
),
|
||||
"gameChannel": (
|
||||
binding.confirmed_channel
|
||||
or binding.effective_channel
|
||||
or binding.current_channel
|
||||
),
|
||||
"roleChangeAvailableAt": (
|
||||
account.role_change_available_at.isoformat()
|
||||
if account.role_change_available_at
|
||||
if account is not None and account.role_change_available_at
|
||||
else None
|
||||
),
|
||||
"sourceTaskId": source_task_id,
|
||||
|
||||
Reference in New Issue
Block a user