fix(douyu): unify binding state and scheduler health
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
"""link latest account-workbench pending role to active fulfillment tasks.
|
||||
|
||||
Revision ID: 20261123_0127
|
||||
Revises: 20261123_0126
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
from web.backend.services.account_binding import pending_binding_role_from_result
|
||||
|
||||
revision: str = "20261123_0127"
|
||||
down_revision: str | None = "20261123_0126"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
tasks = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT
|
||||
t.id AS task_id,
|
||||
t.account_id,
|
||||
t.bind_task_id,
|
||||
a.username,
|
||||
a.nickname
|
||||
FROM platform_fulfillment_tasks AS t
|
||||
JOIN accounts AS a ON a.id = t.account_id
|
||||
JOIN account_leases AS l ON l.id = t.lease_id
|
||||
WHERE t.scope = 'elite'
|
||||
AND t.binding_status IN ('allocated', 'waiting_bind', 'role_ready')
|
||||
AND t.execution_status NOT IN ('completed', 'cancelled', 'manual_required')
|
||||
AND l.status = 'active'
|
||||
"""
|
||||
)
|
||||
).mappings().all()
|
||||
now = datetime.now(UTC).replace(tzinfo=None)
|
||||
for task in tasks:
|
||||
candidates = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT id, result
|
||||
FROM douyu_tasks
|
||||
WHERE account_id = :account_id
|
||||
AND task_type = 'get_bind_qr'
|
||||
AND handbook_scope = 'elite'
|
||||
AND status = 'success'
|
||||
AND id > :current_bind_task_id
|
||||
ORDER BY id DESC
|
||||
LIMIT 20
|
||||
"""
|
||||
),
|
||||
{
|
||||
"account_id": int(task["account_id"]),
|
||||
"current_bind_task_id": int(task["bind_task_id"] or 0),
|
||||
},
|
||||
).mappings().all()
|
||||
latest: dict[str, object] | None = None
|
||||
role_name = ""
|
||||
for candidate in candidates:
|
||||
result = candidate["result"]
|
||||
if isinstance(result, str):
|
||||
try:
|
||||
result = json.loads(result)
|
||||
except json.JSONDecodeError:
|
||||
result = {}
|
||||
if not isinstance(result, dict):
|
||||
continue
|
||||
role_name = pending_binding_role_from_result(result)
|
||||
if role_name:
|
||||
latest = dict(candidate)
|
||||
break
|
||||
if latest is None:
|
||||
continue
|
||||
task_id = int(task["task_id"])
|
||||
bind_task_id = int(latest["id"])
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE platform_fulfillment_tasks
|
||||
SET bind_task_id = :bind_task_id,
|
||||
binding_status = 'role_ready',
|
||||
execution_status = 'role_ready',
|
||||
issue_code = '',
|
||||
last_error = '',
|
||||
updated_at = :now
|
||||
WHERE id = :task_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"task_id": task_id,
|
||||
"bind_task_id": bind_task_id,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
detail = {
|
||||
"accountId": int(task["account_id"]),
|
||||
"accountName": str(task["nickname"] or task["username"] or ""),
|
||||
"bindTaskId": bind_task_id,
|
||||
"previousBindTaskId": int(task["bind_task_id"] or 0),
|
||||
"roleName": role_name,
|
||||
"source": "account_workbench",
|
||||
"backfilled": True,
|
||||
}
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
INSERT INTO platform_fulfillment_events (
|
||||
task_id,
|
||||
event_type,
|
||||
actor_type,
|
||||
actor_id,
|
||||
detail_json,
|
||||
created_at
|
||||
) VALUES (
|
||||
:task_id,
|
||||
'binding_pending_task_linked',
|
||||
'system',
|
||||
'',
|
||||
:detail_json,
|
||||
:created_at
|
||||
)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"task_id": task_id,
|
||||
"detail_json": json.dumps(detail, ensure_ascii=False),
|
||||
"created_at": now,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""数据修复不回退,避免履约任务重新指向过期待确认角色。"""
|
||||
Reference in New Issue
Block a user