优化(履约租约): 绑定失败保留账号直到人工释放

- clean 任务 binding_failed 租约改为无限期保留
- Reaper 不再自动释放 clean 绑定失败账号,只保留历史任务旧策略
- 迁移 0133 清理存量 clean 绑定失败租约到期时间
- 工作台显示账号保留中并提供人工释放
- 补充租约/回收测试
This commit is contained in:
yml
2026-11-25 13:47:34 +08:00
parent adf2e9a066
commit 18df3443d8
7 changed files with 105 additions and 13 deletions
@@ -529,6 +529,10 @@
"20261124_0132_fulfillment_flow_version.py": {
"revision": "20261124_0132",
"sha256": "c32ae403de640251e4ae8d86bd0948c189c3580141f1454490b4798bb286e3a3"
},
"20261125_0133_binding_failed_hold.py": {
"revision": "20261125_0133",
"sha256": "d282e42776fd9501b5293c2a116ad1467df0ddb1847d950b477086e3ae5440a7"
}
}
}
@@ -0,0 +1,37 @@
"""绑定失败 clean 任务保留账号,取消旧的 120 分钟过期。
只清理 clean 任务已经写入的绑定失败租约到期时间;不改任务状态。
历史任务继续沿用旧策略。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261125_0133"
down_revision: str | None = "20261124_0132"
branch_labels: str | None = None
depends_on: str | None = None
def upgrade() -> None:
op.execute(
sa.text(
"""
UPDATE account_leases l
JOIN platform_fulfillment_tasks t ON t.lease_id = l.id
SET l.expires_at = NULL
WHERE l.purpose = 'platform_fulfillment'
AND l.status = 'active'
AND t.flow_version = 2
AND t.execution_status = 'manual_required'
AND t.issue_code = 'binding_failed'
"""
)
)
def downgrade() -> None:
# 数据策略迁移无法可靠恢复旧的 120 分钟到期时间,保留当前保留策略。
pass
@@ -13,6 +13,7 @@ from ...platform_fulfillment.account_selector import (
expire_platform_fulfillment_leases,
release_lease,
)
from ...platform_fulfillment.common import FLOW_VERSION_CLEAN
from ...platform_fulfillment.service import binding_failed_lease_ttl_minutes
JOB_KEY = "platform_fulfillment_lease_reaper"
@@ -41,6 +42,8 @@ def run_platform_fulfillment_lease_reaper(
AccountLease.expires_at.is_(None),
PlatformFulfillmentTask.execution_status == "manual_required",
PlatformFulfillmentTask.issue_code == "binding_failed",
# 新流程绑定失败账号保留到人工释放;只清理历史存量。
PlatformFulfillmentTask.flow_version != FLOW_VERSION_CLEAN,
PlatformFulfillmentTask.updated_at <= stale_binding_cutoff,
Account.role_bind_status == "unbound",
)
@@ -67,6 +70,31 @@ def run_platform_fulfillment_lease_reaper(
.limit(max(1, int(job.batch_size or 500)))
.all()
)
held_leases: list[AccountLease] = []
if candidate_leases:
held_tasks = (
db.query(PlatformFulfillmentTask)
.filter(
PlatformFulfillmentTask.lease_id.in_(
[int(lease.id) for lease in candidate_leases]
),
PlatformFulfillmentTask.flow_version == FLOW_VERSION_CLEAN,
PlatformFulfillmentTask.execution_status == "manual_required",
PlatformFulfillmentTask.issue_code == "binding_failed",
)
.all()
)
held_lease_ids = {int(task.lease_id) for task in held_tasks if task.lease_id}
held_leases = [
lease for lease in candidate_leases if int(lease.id) in held_lease_ids
]
candidate_leases = [
lease for lease in candidate_leases if int(lease.id) not in held_lease_ids
]
for lease in held_leases:
lease.expires_at = None
if held_leases:
db.flush()
released_ids = {int(lease.id) for lease in candidate_leases}
converted_tasks: dict[int, PlatformFulfillmentTask] = {}
if candidate_leases:
@@ -28,6 +28,7 @@ from .binding_status import (
)
from .common import (
BINDING_COOLDOWN_ISSUES,
FLOW_VERSION_CLEAN,
_jsonable,
_now,
binding_failed_lease_ttl_minutes,
@@ -92,6 +93,9 @@ def _lease_policy_for_task(
return "cancelled", 0
if status == "manual_required":
if task.issue_code == "binding_failed":
# Clean 任务绑定失败时保留账号,直到绑定成功或人工释放。
if task.flow_version == FLOW_VERSION_CLEAN:
return "binding_failed_hold", None
return (
"binding_failed_cleanup",
binding_failed_lease_ttl_minutes(),
@@ -802,6 +802,19 @@ export default function PlatformFulfillmentPage() {
}
};
const releaseHeldAccount = (task: PlatformFulfillmentTask) => {
Modal.confirm({
title: `释放任务 #${task.id} 的保留账号?`,
content:
'绑定失败账号默认保留到绑定成功或人工释放。释放后任务会回到待分配,客户需要重新选号后刷新二维码。',
okText: '释放账号',
okButtonProps: { danger: true },
cancelText: '取消',
onOk: () =>
runTaskAction(() => platformFulfillmentApi.release(task.id)),
});
};
const requestAllocation = async (task: PlatformFulfillmentTask) => {
if (allocatingTaskId !== null) return;
setAllocatingTaskId(task.id);
@@ -1390,6 +1403,9 @@ export default function PlatformFulfillmentPage() {
<Tag color={bindingStatusColor(task.bindingStatus)}>
{bindingStatusLabel(task.bindingStatus)}
</Tag>
{task.account && task.bindingStatus === 'failed' ? (
<Tag color="orange">账号保留中</Tag>
) : null}
<Text>{task.bindTask?.roleName || (task.bindTask?.bindPhase === 'role_ready' ? '角色待确认' : '-')}</Text>
{task.bindingStatus === 'bound' ? (
<Text type="secondary" style={{ fontSize: 12 }}>
@@ -1536,6 +1552,11 @@ export default function PlatformFulfillmentPage() {
重新选号并绑定
</Button>
) : null}
{task.account && task.bindingStatus === 'failed' ? (
<Button size="small" danger onClick={() => releaseHeldAccount(task)}>
释放账号
</Button>
) : null}
{task.executionStatus === 'role_ready' ? (
<Button size="small" type="primary" icon={<CheckCircleOutlined />} onClick={() => void runTaskAction(() => platformFulfillmentApi.confirmBinding(task.id))}>
代客户确认