fix(fulfillment): reconcile stale pending roles from newer observations
This commit is contained in:
@@ -25,11 +25,15 @@ from web.backend.services.account_binding import (
|
|||||||
role_change_available_at,
|
role_change_available_at,
|
||||||
)
|
)
|
||||||
from web.backend.services.douyu_binding import (
|
from web.backend.services.douyu_binding import (
|
||||||
|
BINDING_RECONCILE_CONFIRMED,
|
||||||
|
BINDING_RECONCILE_STALE_PENDING,
|
||||||
BINDING_VIEW_CONFIRMED,
|
BINDING_VIEW_CONFIRMED,
|
||||||
BINDING_VIEW_FAILED,
|
BINDING_VIEW_FAILED,
|
||||||
BINDING_VIEW_NONE,
|
BINDING_VIEW_NONE,
|
||||||
BINDING_VIEW_PENDING,
|
BINDING_VIEW_PENDING,
|
||||||
|
reconcile_douyu_binding,
|
||||||
resolve_douyu_binding,
|
resolve_douyu_binding,
|
||||||
|
resolve_fulfillment_binding,
|
||||||
)
|
)
|
||||||
from web.backend.services.douyu_runner_bind import BindMixin
|
from web.backend.services.douyu_runner_bind import BindMixin
|
||||||
|
|
||||||
@@ -369,13 +373,19 @@ def test_task_result_helpers_accept_existing_action_alias_pending():
|
|||||||
assert binding_role_from_result(result) == "只为爱杰宝"
|
assert binding_role_from_result(result) == "只为爱杰宝"
|
||||||
|
|
||||||
|
|
||||||
def _binding_task(*, status: str, result: dict[str, Any]) -> DouyuTask:
|
def _binding_task(
|
||||||
|
*,
|
||||||
|
status: str,
|
||||||
|
result: dict[str, Any],
|
||||||
|
task_id: int = 101,
|
||||||
|
task_type: str = "get_bind_qr",
|
||||||
|
) -> DouyuTask:
|
||||||
return DouyuTask(
|
return DouyuTask(
|
||||||
id=101,
|
id=task_id,
|
||||||
batch_id="binding-view",
|
batch_id="binding-view",
|
||||||
account_id=1,
|
account_id=1,
|
||||||
created_by=1,
|
created_by=1,
|
||||||
task_type="get_bind_qr",
|
task_type=task_type,
|
||||||
handbook_scope="elite",
|
handbook_scope="elite",
|
||||||
status=status,
|
status=status,
|
||||||
result=result,
|
result=result,
|
||||||
@@ -487,6 +497,129 @@ def test_account_binding_without_task_does_not_confirm_fulfillment_order():
|
|||||||
assert binding.bind_confirmed is False
|
assert binding.bind_confirmed is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_newer_observation_reconciles_pending_role_by_identity():
|
||||||
|
account = _binding_account()
|
||||||
|
account.game_name = "续梦乖"
|
||||||
|
pending = resolve_douyu_binding(
|
||||||
|
_binding_task(
|
||||||
|
task_id=100,
|
||||||
|
status="success",
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"baseline_role_name": "布丁贩卖机",
|
||||||
|
"before_bind_info": {
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"role_name": "布丁贩卖机",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"need_bind_act": True,
|
||||||
|
},
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"need_bind_act": True,
|
||||||
|
"bind_ready_for_confirm": True,
|
||||||
|
"bind_confirmed": False,
|
||||||
|
"bind_phase": "role_ready",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
account=account,
|
||||||
|
)
|
||||||
|
observation = resolve_douyu_binding(
|
||||||
|
_binding_task(
|
||||||
|
task_id=101,
|
||||||
|
task_type="query_game_name",
|
||||||
|
status="success",
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"bind_confirmed": True,
|
||||||
|
"bind_phase": "confirmed",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
account=account,
|
||||||
|
)
|
||||||
|
|
||||||
|
reconciliation = reconcile_douyu_binding(pending, observation)
|
||||||
|
fulfillment_view = resolve_fulfillment_binding(
|
||||||
|
_binding_task(
|
||||||
|
task_id=101,
|
||||||
|
task_type="query_game_name",
|
||||||
|
status="success",
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"bind_confirmed": True,
|
||||||
|
"bind_phase": "confirmed",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
account=account,
|
||||||
|
binding_status="bound",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reconciliation.action == BINDING_RECONCILE_CONFIRMED
|
||||||
|
assert fulfillment_view.status == BINDING_VIEW_CONFIRMED
|
||||||
|
assert fulfillment_view.effective_role_name == "续梦乖"
|
||||||
|
|
||||||
|
|
||||||
|
def test_newer_observation_invalidates_different_pending_role():
|
||||||
|
account = _binding_account()
|
||||||
|
account.game_name = "续梦乖"
|
||||||
|
pending = resolve_douyu_binding(
|
||||||
|
_binding_task(
|
||||||
|
task_id=100,
|
||||||
|
status="success",
|
||||||
|
result={
|
||||||
|
"role_name": "布丁贩卖机",
|
||||||
|
"baseline_role_name": "续梦乖",
|
||||||
|
"before_bind_info": {
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"need_bind_act": True,
|
||||||
|
},
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"need_bind_act": True,
|
||||||
|
"bind_ready_for_confirm": True,
|
||||||
|
"bind_confirmed": False,
|
||||||
|
"bind_phase": "role_ready",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
account=account,
|
||||||
|
)
|
||||||
|
observation_task = _binding_task(
|
||||||
|
task_id=101,
|
||||||
|
task_type="query_game_name",
|
||||||
|
status="success",
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"bind_confirmed": True,
|
||||||
|
"bind_phase": "confirmed",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
observation = resolve_douyu_binding(observation_task, account=account)
|
||||||
|
|
||||||
|
reconciliation = reconcile_douyu_binding(pending, observation)
|
||||||
|
fulfillment_view = resolve_fulfillment_binding(
|
||||||
|
observation_task,
|
||||||
|
account=account,
|
||||||
|
binding_status="waiting_bind",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert reconciliation.action == BINDING_RECONCILE_STALE_PENDING
|
||||||
|
assert reconciliation.pending_role_name == "布丁贩卖机"
|
||||||
|
assert reconciliation.observed_role_name == "续梦乖"
|
||||||
|
assert fulfillment_view.status == BINDING_VIEW_NONE
|
||||||
|
assert fulfillment_view.pending_role_name == ""
|
||||||
|
assert fulfillment_view.effective_role_name == "续梦乖"
|
||||||
|
assert fulfillment_view.bind_ready_for_confirm is False
|
||||||
|
assert fulfillment_view.bind_confirmed is False
|
||||||
|
|
||||||
|
|
||||||
def test_confirmed_new_role_replaces_account_current_role():
|
def test_confirmed_new_role_replaces_account_current_role():
|
||||||
account = Account(
|
account = Account(
|
||||||
username="replace-role",
|
username="replace-role",
|
||||||
|
|||||||
@@ -2076,6 +2076,158 @@ class TestBindingAndCompletion(_Base):
|
|||||||
assert event.detail_json["previousBindTaskId"] == old_bind_task.id
|
assert event.detail_json["previousBindTaskId"] == old_bind_task.id
|
||||||
assert event.detail_json["bindTaskId"] == latest_bind_task.id
|
assert event.detail_json["bindTaskId"] == latest_bind_task.id
|
||||||
|
|
||||||
|
def test_newer_observation_invalidates_different_pending_role(self):
|
||||||
|
task = self._allocated()
|
||||||
|
account = self.session.get(Account, task.account_id)
|
||||||
|
account.game_name = "续梦乖"
|
||||||
|
account.game_channel = "手Q · 安卓(android)"
|
||||||
|
account.role_bind_status = "bound"
|
||||||
|
pending_task = DouyuTask(
|
||||||
|
batch_id="pending-old-role",
|
||||||
|
account_id=task.account_id,
|
||||||
|
account_assigned_to=self.admin_id,
|
||||||
|
task_type="get_bind_qr",
|
||||||
|
handbook_scope="elite",
|
||||||
|
status="success",
|
||||||
|
platform_task_id=task.id,
|
||||||
|
result={
|
||||||
|
"role_name": "布丁贩卖机",
|
||||||
|
"area_name": "微信",
|
||||||
|
"plat_name": "苹果(iOS)",
|
||||||
|
"baseline_role_name": "续梦乖",
|
||||||
|
"before_bind_info": {
|
||||||
|
"act_alias": "20260120QYOOB",
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"need_bind_act": True,
|
||||||
|
},
|
||||||
|
"act_alias": "20260120QYOOB",
|
||||||
|
"need_bind_act": True,
|
||||||
|
"bind_ready_for_confirm": True,
|
||||||
|
"bind_confirmed": False,
|
||||||
|
"bind_phase": "role_ready",
|
||||||
|
},
|
||||||
|
created_by=self.admin_id,
|
||||||
|
)
|
||||||
|
self.session.add(pending_task)
|
||||||
|
self.session.flush()
|
||||||
|
task.bind_task_id = pending_task.id
|
||||||
|
task.binding_status = "role_ready"
|
||||||
|
task.execution_status = "role_ready"
|
||||||
|
observation = DouyuTask(
|
||||||
|
batch_id="observe-new-role",
|
||||||
|
account_id=task.account_id,
|
||||||
|
account_assigned_to=self.admin_id,
|
||||||
|
task_type="query_game_name",
|
||||||
|
handbook_scope="elite",
|
||||||
|
status="success",
|
||||||
|
platform_task_id=task.id,
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"bind_confirmed": True,
|
||||||
|
"bind_phase": "confirmed",
|
||||||
|
},
|
||||||
|
created_by=self.admin_id,
|
||||||
|
)
|
||||||
|
self.session.add(observation)
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
|
synced = service.reconcile_binding_observations(self.session)
|
||||||
|
|
||||||
|
self.session.refresh(task)
|
||||||
|
detail = service.get_task_detail(self.session, task.id)
|
||||||
|
event = (
|
||||||
|
self.session.query(PlatformFulfillmentEvent)
|
||||||
|
.filter_by(
|
||||||
|
task_id=task.id,
|
||||||
|
event_type="binding_pending_observation_superseded",
|
||||||
|
)
|
||||||
|
.one()
|
||||||
|
)
|
||||||
|
assert synced == 1
|
||||||
|
assert task.bind_task_id == observation.id
|
||||||
|
assert task.binding_status == "waiting_bind"
|
||||||
|
assert task.execution_status == "waiting_bind"
|
||||||
|
assert detail["task"]["bindTask"]["roleName"] == "续梦乖"
|
||||||
|
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is False
|
||||||
|
assert detail["task"]["bindTask"]["bindConfirmed"] is False
|
||||||
|
assert event.detail_json["pendingRoleName"] == "布丁贩卖机"
|
||||||
|
assert event.detail_json["observedRoleName"] == "续梦乖"
|
||||||
|
|
||||||
|
def test_newer_observation_confirms_matching_pending_role(self):
|
||||||
|
task = self._allocated()
|
||||||
|
account = self.session.get(Account, task.account_id)
|
||||||
|
account.game_name = "续梦乖"
|
||||||
|
account.role_bind_status = "bound"
|
||||||
|
pending_task = DouyuTask(
|
||||||
|
batch_id="pending-matching-role",
|
||||||
|
account_id=task.account_id,
|
||||||
|
account_assigned_to=self.admin_id,
|
||||||
|
task_type="get_bind_qr",
|
||||||
|
handbook_scope="elite",
|
||||||
|
status="success",
|
||||||
|
platform_task_id=task.id,
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"baseline_role_name": "旧角色",
|
||||||
|
"before_bind_info": {
|
||||||
|
"act_alias": "20260120QYOOB",
|
||||||
|
"role_name": "旧角色",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"need_bind_act": True,
|
||||||
|
},
|
||||||
|
"act_alias": "20260120QYOOB",
|
||||||
|
"need_bind_act": True,
|
||||||
|
"bind_ready_for_confirm": True,
|
||||||
|
"bind_confirmed": False,
|
||||||
|
"bind_phase": "role_ready",
|
||||||
|
},
|
||||||
|
created_by=self.admin_id,
|
||||||
|
)
|
||||||
|
self.session.add(pending_task)
|
||||||
|
self.session.flush()
|
||||||
|
task.bind_task_id = pending_task.id
|
||||||
|
task.binding_status = "role_ready"
|
||||||
|
task.execution_status = "role_ready"
|
||||||
|
observation = DouyuTask(
|
||||||
|
batch_id="observe-matching-role",
|
||||||
|
account_id=task.account_id,
|
||||||
|
account_assigned_to=self.admin_id,
|
||||||
|
task_type="query_game_name",
|
||||||
|
handbook_scope="elite",
|
||||||
|
status="success",
|
||||||
|
platform_task_id=task.id,
|
||||||
|
result={
|
||||||
|
"role_name": "续梦乖",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"bind_confirmed": True,
|
||||||
|
"bind_phase": "confirmed",
|
||||||
|
},
|
||||||
|
created_by=self.admin_id,
|
||||||
|
)
|
||||||
|
self.session.add(observation)
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
|
synced = service.sync_task_from_binding_runner(
|
||||||
|
self.session,
|
||||||
|
douyu_task_id=observation.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.session.refresh(task)
|
||||||
|
detail = service.get_task_detail(self.session, task.id)
|
||||||
|
assert synced == 1
|
||||||
|
assert task.bind_task_id == observation.id
|
||||||
|
assert task.binding_status == "bound"
|
||||||
|
assert task.execution_status == "bound"
|
||||||
|
assert detail["task"]["bindTask"]["roleName"] == "续梦乖"
|
||||||
|
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is False
|
||||||
|
assert detail["task"]["bindTask"]["bindConfirmed"] is True
|
||||||
|
|
||||||
def test_refresh_binding_attaches_latest_pending_account_workbench_role(self):
|
def test_refresh_binding_attaches_latest_pending_account_workbench_role(self):
|
||||||
task = self._allocated()
|
task = self._allocated()
|
||||||
old_bind_task = DouyuTask(
|
old_bind_task = DouyuTask(
|
||||||
|
|||||||
@@ -140,6 +140,9 @@ def recover_orphaned_work() -> None:
|
|||||||
cleanup_orphan_login_tasks,
|
cleanup_orphan_login_tasks,
|
||||||
cleanup_orphan_relogin_tasks,
|
cleanup_orphan_relogin_tasks,
|
||||||
)
|
)
|
||||||
|
from .services.platform_fulfillment.service import (
|
||||||
|
reconcile_binding_observations,
|
||||||
|
)
|
||||||
from .services.yyb_service import cleanup_orphan_yyb_tasks
|
from .services.yyb_service import cleanup_orphan_yyb_tasks
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
@@ -180,6 +183,12 @@ def recover_orphaned_work() -> None:
|
|||||||
recovered_work = recover_running_account_work(db)
|
recovered_work = recover_running_account_work(db)
|
||||||
if recovered_work:
|
if recovered_work:
|
||||||
logger.info("启动恢复中断的账号内部工作项: {} 条", recovered_work)
|
logger.info("启动恢复中断的账号内部工作项: {} 条", recovered_work)
|
||||||
|
reconciled_bindings = reconcile_binding_observations(db)
|
||||||
|
if reconciled_bindings:
|
||||||
|
logger.info(
|
||||||
|
"启动收敛账号角色观测到履约任务: {} 条",
|
||||||
|
reconciled_bindings,
|
||||||
|
)
|
||||||
seeded_targets = ensure_default_supply_targets(db)
|
seeded_targets = ensure_default_supply_targets(db)
|
||||||
if seeded_targets:
|
if seeded_targets:
|
||||||
logger.info("已建立默认账号供给目标: {} 条", seeded_targets)
|
logger.info("已建立默认账号供给目标: {} 条", seeded_targets)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import asdict, dataclass
|
from dataclasses import asdict, dataclass, replace
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from ..models import Account, DouyuTask
|
from ..models import Account, DouyuTask
|
||||||
@@ -28,6 +28,17 @@ BINDING_VIEW_PENDING = "pending"
|
|||||||
BINDING_VIEW_CONFIRMED = "confirmed"
|
BINDING_VIEW_CONFIRMED = "confirmed"
|
||||||
BINDING_VIEW_FAILED = "failed"
|
BINDING_VIEW_FAILED = "failed"
|
||||||
|
|
||||||
|
BINDING_OBSERVATION_TASK_TYPES = frozenset(
|
||||||
|
{
|
||||||
|
"query_game_name",
|
||||||
|
"query_change_bind_time",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
BINDING_RECONCILE_NONE = "none"
|
||||||
|
BINDING_RECONCILE_CONFIRMED = "confirmed"
|
||||||
|
BINDING_RECONCILE_STALE_PENDING = "stale_pending"
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class DouyuBindingView:
|
class DouyuBindingView:
|
||||||
@@ -64,6 +75,13 @@ class DouyuBindingView:
|
|||||||
return asdict(self)
|
return asdict(self)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class DouyuBindingReconciliation:
|
||||||
|
action: str
|
||||||
|
pending_role_name: str
|
||||||
|
observed_role_name: str
|
||||||
|
|
||||||
|
|
||||||
def resolve_douyu_binding(
|
def resolve_douyu_binding(
|
||||||
task: DouyuTask | None,
|
task: DouyuTask | None,
|
||||||
*,
|
*,
|
||||||
@@ -103,8 +121,16 @@ def resolve_douyu_binding(
|
|||||||
|
|
||||||
account_bind_status = get_bind_status(account, "elite") if account else ""
|
account_bind_status = get_bind_status(account, "elite") if account else ""
|
||||||
current_role = str(account.game_name or "").strip() if account else ""
|
current_role = str(account.game_name or "").strip() if account else ""
|
||||||
current_area = str(result.get("current_area_name") or "").strip()
|
current_area = str(
|
||||||
current_plat = str(result.get("current_plat_name") or "").strip()
|
result.get("current_area_name")
|
||||||
|
or (result.get("area_name") if bind_confirmed else "")
|
||||||
|
or ""
|
||||||
|
).strip()
|
||||||
|
current_plat = str(
|
||||||
|
result.get("current_plat_name")
|
||||||
|
or (result.get("plat_name") if bind_confirmed else "")
|
||||||
|
or ""
|
||||||
|
).strip()
|
||||||
current_channel = str(account.game_channel or "").strip() if account else ""
|
current_channel = str(account.game_channel or "").strip() if account else ""
|
||||||
if not current_role:
|
if not current_role:
|
||||||
current_role = str(
|
current_role = str(
|
||||||
@@ -163,3 +189,76 @@ def resolve_douyu_binding(
|
|||||||
bind_ready_for_confirm=status == BINDING_VIEW_PENDING,
|
bind_ready_for_confirm=status == BINDING_VIEW_PENDING,
|
||||||
account_bind_status=account_bind_status,
|
account_bind_status=account_bind_status,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_fulfillment_binding(
|
||||||
|
bind_task: DouyuTask | None,
|
||||||
|
*,
|
||||||
|
account: Account | None,
|
||||||
|
binding_status: str,
|
||||||
|
) -> DouyuBindingView:
|
||||||
|
"""解析履约展示;查询任务只是观测,不能直接宣告订单完成。"""
|
||||||
|
binding = resolve_douyu_binding(bind_task, account=account)
|
||||||
|
if (
|
||||||
|
bind_task is None
|
||||||
|
or bind_task.task_type not in BINDING_OBSERVATION_TASK_TYPES
|
||||||
|
):
|
||||||
|
return binding
|
||||||
|
if binding_status == "bound" and binding.status == BINDING_VIEW_CONFIRMED:
|
||||||
|
return binding
|
||||||
|
return replace(
|
||||||
|
binding,
|
||||||
|
status=BINDING_VIEW_FAILED
|
||||||
|
if binding.status == BINDING_VIEW_FAILED
|
||||||
|
else BINDING_VIEW_NONE,
|
||||||
|
bind_phase="observed_current" if binding.current_role_name else "",
|
||||||
|
pending_role_name="",
|
||||||
|
pending_area_name="",
|
||||||
|
pending_plat_name="",
|
||||||
|
pending_channel="",
|
||||||
|
confirmed_role_name="",
|
||||||
|
confirmed_area_name="",
|
||||||
|
confirmed_plat_name="",
|
||||||
|
confirmed_channel="",
|
||||||
|
effective_role_name=binding.current_role_name,
|
||||||
|
effective_area_name=binding.current_area_name,
|
||||||
|
effective_plat_name=binding.current_plat_name,
|
||||||
|
effective_channel=binding.current_channel,
|
||||||
|
qr_url="",
|
||||||
|
reported_ready_for_confirm=False,
|
||||||
|
bind_confirmed=False,
|
||||||
|
bind_ready_for_confirm=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def reconcile_douyu_binding(
|
||||||
|
current: DouyuBindingView,
|
||||||
|
observation: DouyuBindingView,
|
||||||
|
) -> DouyuBindingReconciliation:
|
||||||
|
"""用较新的账号观测收敛旧的待确认角色。"""
|
||||||
|
if (
|
||||||
|
current.status != BINDING_VIEW_PENDING
|
||||||
|
or observation.status != BINDING_VIEW_CONFIRMED
|
||||||
|
or current.source_task_id is None
|
||||||
|
or observation.source_task_id is None
|
||||||
|
or observation.source_task_id <= current.source_task_id
|
||||||
|
or not current.pending_role_name
|
||||||
|
):
|
||||||
|
return DouyuBindingReconciliation(
|
||||||
|
action=BINDING_RECONCILE_NONE,
|
||||||
|
pending_role_name=current.pending_role_name,
|
||||||
|
observed_role_name=observation.confirmed_role_name,
|
||||||
|
)
|
||||||
|
same_role = (
|
||||||
|
current.pending_role_name.casefold()
|
||||||
|
== observation.confirmed_role_name.casefold()
|
||||||
|
)
|
||||||
|
return DouyuBindingReconciliation(
|
||||||
|
action=(
|
||||||
|
BINDING_RECONCILE_CONFIRMED
|
||||||
|
if same_role
|
||||||
|
else BINDING_RECONCILE_STALE_PENDING
|
||||||
|
),
|
||||||
|
pending_role_name=current.pending_role_name,
|
||||||
|
observed_role_name=observation.confirmed_role_name,
|
||||||
|
)
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ from ..models import (
|
|||||||
)
|
)
|
||||||
from .account_events_service import EVENT_COLLAB_SYNCED
|
from .account_events_service import EVENT_COLLAB_SYNCED
|
||||||
from .audit_service import record_account_action, record_audit
|
from .audit_service import record_account_action, record_audit
|
||||||
from .douyu_binding import BINDING_VIEW_CONFIRMED, resolve_douyu_binding
|
from .douyu_binding import (
|
||||||
|
BINDING_VIEW_CONFIRMED,
|
||||||
|
resolve_fulfillment_binding,
|
||||||
|
)
|
||||||
from .douyu_collab_common import (
|
from .douyu_collab_common import (
|
||||||
POINTS_THRESHOLD_DEFAULT,
|
POINTS_THRESHOLD_DEFAULT,
|
||||||
SOURCE_TYPE_PLATFORM_FULFILLMENT,
|
SOURCE_TYPE_PLATFORM_FULFILLMENT,
|
||||||
@@ -103,9 +106,10 @@ def _apply_platform_fulfillment_binding(
|
|||||||
)
|
)
|
||||||
if bind_task is None:
|
if bind_task is None:
|
||||||
return
|
return
|
||||||
binding = resolve_douyu_binding(
|
binding = resolve_fulfillment_binding(
|
||||||
bind_task,
|
bind_task,
|
||||||
account=db.get(Account, int(task.account_id)) if task.account_id else None,
|
account=db.get(Account, int(task.account_id)) if task.account_id else None,
|
||||||
|
binding_status=task.binding_status,
|
||||||
)
|
)
|
||||||
if binding.status != BINDING_VIEW_CONFIRMED:
|
if binding.status != BINDING_VIEW_CONFIRMED:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -19,9 +19,15 @@ from ...models import (
|
|||||||
)
|
)
|
||||||
from ..business_events import record_douyu_task_started
|
from ..business_events import record_douyu_task_started
|
||||||
from ..douyu_binding import (
|
from ..douyu_binding import (
|
||||||
|
BINDING_OBSERVATION_TASK_TYPES,
|
||||||
|
BINDING_RECONCILE_CONFIRMED,
|
||||||
|
BINDING_RECONCILE_STALE_PENDING,
|
||||||
BINDING_VIEW_CONFIRMED,
|
BINDING_VIEW_CONFIRMED,
|
||||||
BINDING_VIEW_PENDING,
|
BINDING_VIEW_PENDING,
|
||||||
|
DouyuBindingReconciliation,
|
||||||
|
reconcile_douyu_binding,
|
||||||
resolve_douyu_binding,
|
resolve_douyu_binding,
|
||||||
|
resolve_fulfillment_binding,
|
||||||
)
|
)
|
||||||
from ..execution_jobs import (
|
from ..execution_jobs import (
|
||||||
JOB_DOUYU_BINDING,
|
JOB_DOUYU_BINDING,
|
||||||
@@ -88,9 +94,10 @@ def start_binding_sync(
|
|||||||
if task.bind_task_id:
|
if task.bind_task_id:
|
||||||
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||||
if existing:
|
if existing:
|
||||||
binding = resolve_douyu_binding(
|
binding = resolve_fulfillment_binding(
|
||||||
existing,
|
existing,
|
||||||
account=db.get(Account, int(task.account_id)),
|
account=db.get(Account, int(task.account_id)),
|
||||||
|
binding_status=task.binding_status,
|
||||||
)
|
)
|
||||||
stale_pending = (
|
stale_pending = (
|
||||||
binding.reported_ready_for_confirm
|
binding.reported_ready_for_confirm
|
||||||
@@ -205,6 +212,15 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|||||||
task = _load_task(db, task_id)
|
task = _load_task(db, task_id)
|
||||||
if task.execution_status in {"completed", "cancelled"}:
|
if task.execution_status in {"completed", "cancelled"}:
|
||||||
return get_task_detail(db, task.id)
|
return get_task_detail(db, task.id)
|
||||||
|
if task.binding_status == "role_ready":
|
||||||
|
observation_task = _binding_observation_task(db, task)
|
||||||
|
if observation_task is not None and _apply_binding_observation(
|
||||||
|
db,
|
||||||
|
task,
|
||||||
|
observation_task,
|
||||||
|
):
|
||||||
|
db.commit()
|
||||||
|
task = _load_task(db, task.id)
|
||||||
latest_pending = _latest_pending_binding_task(db, task)
|
latest_pending = _latest_pending_binding_task(db, task)
|
||||||
if latest_pending is not None and _attach_pending_binding_task(
|
if latest_pending is not None and _attach_pending_binding_task(
|
||||||
db,
|
db,
|
||||||
@@ -226,9 +242,10 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
|||||||
task.last_error,
|
task.last_error,
|
||||||
)
|
)
|
||||||
result = bind_task.result or {}
|
result = bind_task.result or {}
|
||||||
binding = resolve_douyu_binding(
|
binding = resolve_fulfillment_binding(
|
||||||
bind_task,
|
bind_task,
|
||||||
account=db.get(Account, int(task.account_id)) if task.account_id else None,
|
account=db.get(Account, int(task.account_id)) if task.account_id else None,
|
||||||
|
binding_status=task.binding_status,
|
||||||
)
|
)
|
||||||
if bind_task.status in {"failed", "error", "stopped"}:
|
if bind_task.status in {"failed", "error", "stopped"}:
|
||||||
task.binding_status = "failed"
|
task.binding_status = "failed"
|
||||||
@@ -324,6 +341,8 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
|||||||
douyu_task = db.get(DouyuTask, int(douyu_task_id))
|
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:
|
if douyu_task is None or douyu_task.task_type not in BINDING_SYNC_TASK_TYPES:
|
||||||
return len(task_ids)
|
return len(task_ids)
|
||||||
|
if douyu_task.task_type in BINDING_OBSERVATION_TASK_TYPES:
|
||||||
|
return len(task_ids) + _sync_binding_observation(db, douyu_task)
|
||||||
binding = resolve_douyu_binding(
|
binding = resolve_douyu_binding(
|
||||||
douyu_task,
|
douyu_task,
|
||||||
account=db.get(Account, int(douyu_task.account_id))
|
account=db.get(Account, int(douyu_task.account_id))
|
||||||
@@ -381,6 +400,146 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _binding_observation_task(
|
||||||
|
db: Session,
|
||||||
|
task: PlatformFulfillmentTask,
|
||||||
|
) -> DouyuTask | None:
|
||||||
|
if not task.account_id or not task.bind_task_id:
|
||||||
|
return None
|
||||||
|
return (
|
||||||
|
db.query(DouyuTask)
|
||||||
|
.filter(
|
||||||
|
DouyuTask.platform_task_id == int(task.id),
|
||||||
|
DouyuTask.account_id == int(task.account_id),
|
||||||
|
DouyuTask.task_type.in_(BINDING_OBSERVATION_TASK_TYPES),
|
||||||
|
DouyuTask.status == "success",
|
||||||
|
DouyuTask.id > int(task.bind_task_id),
|
||||||
|
)
|
||||||
|
.order_by(DouyuTask.id.desc())
|
||||||
|
.limit(1)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_binding_observation(
|
||||||
|
db: Session,
|
||||||
|
task: PlatformFulfillmentTask,
|
||||||
|
observation_task: DouyuTask,
|
||||||
|
*,
|
||||||
|
account: Account | None = None,
|
||||||
|
) -> DouyuBindingReconciliation | None:
|
||||||
|
if not task.account_id:
|
||||||
|
return None
|
||||||
|
current_task = (
|
||||||
|
db.get(DouyuTask, int(task.bind_task_id))
|
||||||
|
if task.bind_task_id
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
resolved_account = account or db.get(Account, int(task.account_id))
|
||||||
|
current = resolve_douyu_binding(current_task, account=resolved_account)
|
||||||
|
observation = resolve_douyu_binding(
|
||||||
|
observation_task,
|
||||||
|
account=resolved_account,
|
||||||
|
)
|
||||||
|
reconciliation = reconcile_douyu_binding(current, observation)
|
||||||
|
if reconciliation.action not in {
|
||||||
|
BINDING_RECONCILE_CONFIRMED,
|
||||||
|
BINDING_RECONCILE_STALE_PENDING,
|
||||||
|
}:
|
||||||
|
return None
|
||||||
|
previous_bind_task_id = task.bind_task_id
|
||||||
|
task.bind_task_id = int(observation_task.id)
|
||||||
|
task.issue_code = ""
|
||||||
|
task.last_error = ""
|
||||||
|
task.updated_at = _now()
|
||||||
|
if reconciliation.action == BINDING_RECONCILE_CONFIRMED:
|
||||||
|
task.binding_status = "bound"
|
||||||
|
task.execution_status = "bound"
|
||||||
|
event_type = "binding_observation_confirmed"
|
||||||
|
else:
|
||||||
|
task.binding_status = "waiting_bind"
|
||||||
|
task.execution_status = "waiting_bind"
|
||||||
|
event_type = "binding_pending_observation_superseded"
|
||||||
|
_append_event(
|
||||||
|
db,
|
||||||
|
task.id,
|
||||||
|
event_type,
|
||||||
|
detail={
|
||||||
|
"bindTaskId": int(observation_task.id),
|
||||||
|
"previousBindTaskId": previous_bind_task_id,
|
||||||
|
"pendingRoleName": reconciliation.pending_role_name,
|
||||||
|
"observedRoleName": reconciliation.observed_role_name,
|
||||||
|
"sourceTaskType": observation_task.task_type,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
_apply_task_lease_policy(db, task)
|
||||||
|
_emit_order_site_binding_state(db, task.id)
|
||||||
|
return reconciliation
|
||||||
|
|
||||||
|
|
||||||
|
def _sync_binding_observation(
|
||||||
|
db: Session,
|
||||||
|
observation_task: DouyuTask,
|
||||||
|
) -> int:
|
||||||
|
if (
|
||||||
|
not observation_task.platform_task_id
|
||||||
|
or not observation_task.account_id
|
||||||
|
or str(observation_task.handbook_scope or "") != "elite"
|
||||||
|
):
|
||||||
|
return 0
|
||||||
|
task = (
|
||||||
|
db.query(PlatformFulfillmentTask)
|
||||||
|
.filter(PlatformFulfillmentTask.id == int(observation_task.platform_task_id))
|
||||||
|
.with_for_update()
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
if task is None or task.execution_status in {"completed", "cancelled"}:
|
||||||
|
return 0
|
||||||
|
reconciled = _apply_binding_observation(
|
||||||
|
db,
|
||||||
|
task,
|
||||||
|
observation_task,
|
||||||
|
account=db.get(Account, int(observation_task.account_id)),
|
||||||
|
)
|
||||||
|
if reconciled is None:
|
||||||
|
return 0
|
||||||
|
db.commit()
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def reconcile_binding_observations(
|
||||||
|
db: Session,
|
||||||
|
*,
|
||||||
|
limit: int = 500,
|
||||||
|
) -> int:
|
||||||
|
"""启动时收敛已落库但尚未触发的较新账号角色观测。"""
|
||||||
|
tasks = (
|
||||||
|
db.query(PlatformFulfillmentTask)
|
||||||
|
.filter(
|
||||||
|
PlatformFulfillmentTask.scope == "elite",
|
||||||
|
PlatformFulfillmentTask.binding_status == "role_ready",
|
||||||
|
PlatformFulfillmentTask.execution_status.notin_(
|
||||||
|
("completed", "cancelled", "manual_required")
|
||||||
|
),
|
||||||
|
PlatformFulfillmentTask.bind_task_id.isnot(None),
|
||||||
|
)
|
||||||
|
.order_by(PlatformFulfillmentTask.id.asc())
|
||||||
|
.limit(max(1, min(limit, 2000)))
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
reconciled = 0
|
||||||
|
for task in tasks:
|
||||||
|
observation_task = _binding_observation_task(db, task)
|
||||||
|
if observation_task is None:
|
||||||
|
continue
|
||||||
|
result = _apply_binding_observation(db, task, observation_task)
|
||||||
|
if result is not None:
|
||||||
|
reconciled += 1
|
||||||
|
if reconciled:
|
||||||
|
db.commit()
|
||||||
|
return reconciled
|
||||||
|
|
||||||
|
|
||||||
def _latest_pending_binding_task(
|
def _latest_pending_binding_task(
|
||||||
db: Session,
|
db: Session,
|
||||||
task: PlatformFulfillmentTask,
|
task: PlatformFulfillmentTask,
|
||||||
@@ -397,6 +556,7 @@ def _latest_pending_binding_task(
|
|||||||
DouyuTask.task_type.in_(PENDING_BINDING_TASK_TYPES),
|
DouyuTask.task_type.in_(PENDING_BINDING_TASK_TYPES),
|
||||||
DouyuTask.handbook_scope == "elite",
|
DouyuTask.handbook_scope == "elite",
|
||||||
DouyuTask.status == "success",
|
DouyuTask.status == "success",
|
||||||
|
DouyuTask.id > int(task.bind_task_id or 0),
|
||||||
)
|
)
|
||||||
.order_by(DouyuTask.id.desc())
|
.order_by(DouyuTask.id.desc())
|
||||||
.limit(20)
|
.limit(20)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ from .allocation import ( # noqa: F401
|
|||||||
from .binding import ( # noqa: F401
|
from .binding import ( # noqa: F401
|
||||||
confirm_binding,
|
confirm_binding,
|
||||||
confirm_binding_sync,
|
confirm_binding_sync,
|
||||||
|
reconcile_binding_observations,
|
||||||
reconcile_binding_state_outbox,
|
reconcile_binding_state_outbox,
|
||||||
refresh_binding,
|
refresh_binding,
|
||||||
requires_account_binding_sync,
|
requires_account_binding_sync,
|
||||||
@@ -155,6 +156,7 @@ __all__ = [
|
|||||||
"mark_manual_required",
|
"mark_manual_required",
|
||||||
"prepare_manual_allocation",
|
"prepare_manual_allocation",
|
||||||
"prepare_platform_task_for_collab_takeover",
|
"prepare_platform_task_for_collab_takeover",
|
||||||
|
"reconcile_binding_observations",
|
||||||
"reconcile_binding_state_outbox",
|
"reconcile_binding_state_outbox",
|
||||||
"record_douyu_operation_result",
|
"record_douyu_operation_result",
|
||||||
"record_douyu_operation_started",
|
"record_douyu_operation_started",
|
||||||
|
|||||||
@@ -25,10 +25,11 @@ from ...models import (
|
|||||||
)
|
)
|
||||||
from ...permissions import ROLE_LABELS
|
from ...permissions import ROLE_LABELS
|
||||||
from ..douyu_binding import (
|
from ..douyu_binding import (
|
||||||
|
BINDING_OBSERVATION_TASK_TYPES,
|
||||||
BINDING_VIEW_CONFIRMED,
|
BINDING_VIEW_CONFIRMED,
|
||||||
BINDING_VIEW_FAILED,
|
BINDING_VIEW_FAILED,
|
||||||
BINDING_VIEW_PENDING,
|
BINDING_VIEW_PENDING,
|
||||||
resolve_douyu_binding,
|
resolve_fulfillment_binding,
|
||||||
)
|
)
|
||||||
from ..file_service import build_file_url
|
from ..file_service import build_file_url
|
||||||
from . import classification
|
from . import classification
|
||||||
@@ -364,9 +365,10 @@ def _task_out(
|
|||||||
)
|
)
|
||||||
collab_order = resolved.collab_orders.get(int(task.id))
|
collab_order = resolved.collab_orders.get(int(task.id))
|
||||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||||
binding = resolve_douyu_binding(
|
binding = resolve_fulfillment_binding(
|
||||||
bind_task,
|
bind_task,
|
||||||
account=account,
|
account=account,
|
||||||
|
binding_status=task.binding_status,
|
||||||
)
|
)
|
||||||
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
||||||
effective_last_error = _effective_task_message(task, effective_issue)
|
effective_last_error = _effective_task_message(task, effective_issue)
|
||||||
@@ -599,8 +601,15 @@ def _customer_binding_queue_state(
|
|||||||
return "failed", 0
|
return "failed", 0
|
||||||
if bind_task is None:
|
if bind_task is None:
|
||||||
return "ready_to_start", 0
|
return "ready_to_start", 0
|
||||||
binding = resolve_douyu_binding(
|
if (
|
||||||
|
bind_task.task_type in BINDING_OBSERVATION_TASK_TYPES
|
||||||
|
and task.binding_status != "bound"
|
||||||
|
):
|
||||||
|
return "ready_to_start", 0
|
||||||
|
binding = resolve_fulfillment_binding(
|
||||||
bind_task,
|
bind_task,
|
||||||
|
account=None,
|
||||||
|
binding_status=task.binding_status,
|
||||||
)
|
)
|
||||||
if binding.status == BINDING_VIEW_CONFIRMED:
|
if binding.status == BINDING_VIEW_CONFIRMED:
|
||||||
return "confirmed", 0
|
return "confirmed", 0
|
||||||
@@ -630,9 +639,10 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
|||||||
)
|
)
|
||||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||||
account = db.get(Account, int(task.account_id)) if task.account_id else None
|
account = db.get(Account, int(task.account_id)) if task.account_id else None
|
||||||
binding = resolve_douyu_binding(
|
binding = resolve_fulfillment_binding(
|
||||||
bind_task,
|
bind_task,
|
||||||
account=account,
|
account=account,
|
||||||
|
binding_status=task.binding_status,
|
||||||
)
|
)
|
||||||
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
effective_issue = _effective_binding_issue(task, bind_task, bind_result)
|
||||||
lease = (
|
lease = (
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ const BINDING_STATUS_LABELS: Record<string, string> = {
|
|||||||
not_started: '未开始',
|
not_started: '未开始',
|
||||||
preparing: '准备中',
|
preparing: '准备中',
|
||||||
qr_pending: '待扫码',
|
qr_pending: '待扫码',
|
||||||
|
waiting_bind: '待重新绑定',
|
||||||
role_ready: '待确认角色',
|
role_ready: '待确认角色',
|
||||||
bound: '已绑定',
|
bound: '已绑定',
|
||||||
failed: '绑定失败',
|
failed: '绑定失败',
|
||||||
@@ -131,6 +132,8 @@ const EVENT_LABELS: Record<string, string> = {
|
|||||||
binding_synced_from_account: '同步账号最新角色绑定',
|
binding_synced_from_account: '同步账号最新角色绑定',
|
||||||
binding_false_confirmation_reverted: '纠正误判的账号历史绑定',
|
binding_false_confirmation_reverted: '纠正误判的账号历史绑定',
|
||||||
binding_pending_task_linked: '关联最新待确认角色',
|
binding_pending_task_linked: '关联最新待确认角色',
|
||||||
|
binding_pending_observation_superseded: '旧待确认角色已被最新绑定替换',
|
||||||
|
binding_observation_confirmed: '账号观测确认角色绑定',
|
||||||
eligibility_check_failed: '兑换资格查询失败',
|
eligibility_check_failed: '兑换资格查询失败',
|
||||||
eligibility_auto_refresh_failed: '自动刷新兑换资格失败',
|
eligibility_auto_refresh_failed: '自动刷新兑换资格失败',
|
||||||
exchange_eligibility_checked: '刷新兑换资格',
|
exchange_eligibility_checked: '刷新兑换资格',
|
||||||
|
|||||||
Reference in New Issue
Block a user