fix(fulfillment): mark confirmed role observations as bound
This commit is contained in:
@@ -37,7 +37,6 @@ BINDING_OBSERVATION_TASK_TYPES = frozenset(
|
||||
|
||||
BINDING_RECONCILE_NONE = "none"
|
||||
BINDING_RECONCILE_CONFIRMED = "confirmed"
|
||||
BINDING_RECONCILE_STALE_PENDING = "stale_pending"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -249,16 +248,8 @@ def reconcile_douyu_binding(
|
||||
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
|
||||
),
|
||||
action=BINDING_RECONCILE_CONFIRMED,
|
||||
pending_role_name=current.pending_role_name,
|
||||
observed_role_name=observation.confirmed_role_name,
|
||||
)
|
||||
|
||||
@@ -21,7 +21,6 @@ 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,
|
||||
@@ -442,24 +441,16 @@ def _apply_binding_observation(
|
||||
account=resolved_account,
|
||||
)
|
||||
reconciliation = reconcile_douyu_binding(current, observation)
|
||||
if reconciliation.action not in {
|
||||
BINDING_RECONCILE_CONFIRMED,
|
||||
BINDING_RECONCILE_STALE_PENDING,
|
||||
}:
|
||||
if reconciliation.action != BINDING_RECONCILE_CONFIRMED:
|
||||
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"
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
event_type = "binding_observation_confirmed"
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
@@ -507,6 +498,47 @@ def _sync_binding_observation(
|
||||
return 1
|
||||
|
||||
|
||||
def _promote_bound_observation(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
observation_task: DouyuTask,
|
||||
*,
|
||||
account: Account | None = None,
|
||||
) -> bool:
|
||||
if observation_task.task_type not in BINDING_OBSERVATION_TASK_TYPES:
|
||||
return False
|
||||
binding = resolve_douyu_binding(
|
||||
observation_task,
|
||||
account=account
|
||||
or (
|
||||
db.get(Account, int(task.account_id))
|
||||
if task.account_id
|
||||
else None
|
||||
),
|
||||
)
|
||||
if binding.status != BINDING_VIEW_CONFIRMED:
|
||||
return False
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"binding_observation_confirmed",
|
||||
detail={
|
||||
"bindTaskId": int(observation_task.id),
|
||||
"previousBindTaskId": task.bind_task_id,
|
||||
"observedRoleName": binding.confirmed_role_name,
|
||||
"sourceTaskType": observation_task.task_type,
|
||||
},
|
||||
)
|
||||
_apply_task_lease_policy(db, task)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return True
|
||||
|
||||
|
||||
def reconcile_binding_observations(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -517,7 +549,9 @@ def reconcile_binding_observations(
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.scope == "elite",
|
||||
PlatformFulfillmentTask.binding_status == "role_ready",
|
||||
PlatformFulfillmentTask.binding_status.in_(
|
||||
("role_ready", "waiting_bind")
|
||||
),
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
("completed", "cancelled", "manual_required")
|
||||
),
|
||||
@@ -529,6 +563,18 @@ def reconcile_binding_observations(
|
||||
)
|
||||
reconciled = 0
|
||||
for task in tasks:
|
||||
current_task = (
|
||||
db.get(DouyuTask, int(task.bind_task_id))
|
||||
if task.bind_task_id
|
||||
else None
|
||||
)
|
||||
if current_task is not None and _promote_bound_observation(
|
||||
db,
|
||||
task,
|
||||
current_task,
|
||||
):
|
||||
reconciled += 1
|
||||
continue
|
||||
observation_task = _binding_observation_task(db, task)
|
||||
if observation_task is None:
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user