fix(fulfillment): reconcile stale pending roles from newer observations
This commit is contained in:
@@ -19,9 +19,15 @@ from ...models import (
|
||||
)
|
||||
from ..business_events import record_douyu_task_started
|
||||
from ..douyu_binding import (
|
||||
BINDING_OBSERVATION_TASK_TYPES,
|
||||
BINDING_RECONCILE_CONFIRMED,
|
||||
BINDING_RECONCILE_STALE_PENDING,
|
||||
BINDING_VIEW_CONFIRMED,
|
||||
BINDING_VIEW_PENDING,
|
||||
DouyuBindingReconciliation,
|
||||
reconcile_douyu_binding,
|
||||
resolve_douyu_binding,
|
||||
resolve_fulfillment_binding,
|
||||
)
|
||||
from ..execution_jobs import (
|
||||
JOB_DOUYU_BINDING,
|
||||
@@ -88,9 +94,10 @@ def start_binding_sync(
|
||||
if task.bind_task_id:
|
||||
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||
if existing:
|
||||
binding = resolve_douyu_binding(
|
||||
binding = resolve_fulfillment_binding(
|
||||
existing,
|
||||
account=db.get(Account, int(task.account_id)),
|
||||
binding_status=task.binding_status,
|
||||
)
|
||||
stale_pending = (
|
||||
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)
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
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)
|
||||
if latest_pending is not None and _attach_pending_binding_task(
|
||||
db,
|
||||
@@ -226,9 +242,10 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
task.last_error,
|
||||
)
|
||||
result = bind_task.result or {}
|
||||
binding = resolve_douyu_binding(
|
||||
binding = resolve_fulfillment_binding(
|
||||
bind_task,
|
||||
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"}:
|
||||
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))
|
||||
if douyu_task is None or douyu_task.task_type not in BINDING_SYNC_TASK_TYPES:
|
||||
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(
|
||||
douyu_task,
|
||||
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(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -397,6 +556,7 @@ def _latest_pending_binding_task(
|
||||
DouyuTask.task_type.in_(PENDING_BINDING_TASK_TYPES),
|
||||
DouyuTask.handbook_scope == "elite",
|
||||
DouyuTask.status == "success",
|
||||
DouyuTask.id > int(task.bind_task_id or 0),
|
||||
)
|
||||
.order_by(DouyuTask.id.desc())
|
||||
.limit(20)
|
||||
|
||||
Reference in New Issue
Block a user