From 18df3443d852063cfdc856bee19666d18511a505 Mon Sep 17 00:00:00 2001 From: yml Date: Wed, 25 Nov 2026 13:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96(=E5=B1=A5=E7=BA=A6=E7=A7=9F?= =?UTF-8?q?=E7=BA=A6):=20=E7=BB=91=E5=AE=9A=E5=A4=B1=E8=B4=A5=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E8=B4=A6=E5=8F=B7=E7=9B=B4=E5=88=B0=E4=BA=BA=E5=B7=A5?= =?UTF-8?q?=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - clean 任务 binding_failed 租约改为无限期保留 - Reaper 不再自动释放 clean 绑定失败账号,只保留历史任务旧策略 - 迁移 0133 清理存量 clean 绑定失败租约到期时间 - 工作台显示账号保留中并提供人工释放 - 补充租约/回收测试 --- tests/test_migrations.py | 2 +- tests/test_platform_fulfillment.py | 22 +++++------ .../migrations/migration_manifest.json | 4 ++ .../20261125_0133_binding_failed_hold.py | 37 +++++++++++++++++++ .../jobs/platform_fulfillment_lease_reaper.py | 28 ++++++++++++++ .../services/platform_fulfillment/runtime.py | 4 ++ .../src/pages/PlatformFulfillmentPage.tsx | 21 +++++++++++ 7 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 web/backend/migrations/versions/20261125_0133_binding_failed_hold.py diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 508d7a5..f7a4064 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -45,7 +45,7 @@ TESTS_DIR = Path(__file__).resolve().parent PROJECT_ROOT = TESTS_DIR.parent VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" -HEAD_REVISION = "20261124_0132" +HEAD_REVISION = "20261125_0133" # 迁移新增、但不声明在模型里的复合查询索引。 EXTRA_INDEXES = ( diff --git a/tests/test_platform_fulfillment.py b/tests/test_platform_fulfillment.py index 20d3504..340bfa9 100644 --- a/tests/test_platform_fulfillment.py +++ b/tests/test_platform_fulfillment.py @@ -5078,7 +5078,7 @@ class TestBindingAndCompletion(_Base): assert task.execution_status == "manual_required" assert task.issue_code == "lease_expired" - def test_binding_failed_lease_uses_bounded_cleanup_ttl(self): + def test_clean_binding_failed_lease_is_held_without_expiry(self): task = self._allocated() account = self.session.get(Account, task.account_id) account.role_bind_status = "unbound" @@ -5105,11 +5105,10 @@ class TestBindingAndCompletion(_Base): lease = self.session.query(AccountLease).one() self.session.refresh(task) assert task.issue_code == "binding_failed" - assert lease.expires_at is not None - assert lease.expires_at >= datetime.now(UTC) + timedelta(minutes=119) - assert lease.expires_at <= datetime.now(UTC) + timedelta(minutes=121) + assert task.flow_version == 2 + assert lease.expires_at is None - def test_reaper_recovers_stale_binding_failed_unbound_lease(self): + def test_reaper_does_not_release_clean_binding_failed_lease(self): task = self._allocated() lease = self.session.query(AccountLease).one() account = self.session.get(Account, task.account_id) @@ -5130,13 +5129,12 @@ class TestBindingAndCompletion(_Base): self.session.refresh(task) self.session.refresh(lease) - assert result["stale_binding_recovered"] == 1 - assert result["expired"] == 1 - assert lease.status == "expired" - assert task.account_id is None - assert task.lease_id is None - assert task.bind_task_id is None - assert task.issue_code == "lease_expired" + assert result["stale_binding_recovered"] == 0 + assert result["expired"] == 0 + assert lease.status == "active" + assert task.account_id is not None + assert task.lease_id is not None + assert task.issue_code == "binding_failed" def test_unlimited_lease_is_ignored_by_reaper(self): task = self._allocated() diff --git a/web/backend/migrations/migration_manifest.json b/web/backend/migrations/migration_manifest.json index f914a51..d574d93 100644 --- a/web/backend/migrations/migration_manifest.json +++ b/web/backend/migrations/migration_manifest.json @@ -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" } } } diff --git a/web/backend/migrations/versions/20261125_0133_binding_failed_hold.py b/web/backend/migrations/versions/20261125_0133_binding_failed_hold.py new file mode 100644 index 0000000..04f681e --- /dev/null +++ b/web/backend/migrations/versions/20261125_0133_binding_failed_hold.py @@ -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 diff --git a/web/backend/services/automation/jobs/platform_fulfillment_lease_reaper.py b/web/backend/services/automation/jobs/platform_fulfillment_lease_reaper.py index a918b3a..1366039 100644 --- a/web/backend/services/automation/jobs/platform_fulfillment_lease_reaper.py +++ b/web/backend/services/automation/jobs/platform_fulfillment_lease_reaper.py @@ -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: diff --git a/web/backend/services/platform_fulfillment/runtime.py b/web/backend/services/platform_fulfillment/runtime.py index 04eb338..eb3ca90 100644 --- a/web/backend/services/platform_fulfillment/runtime.py +++ b/web/backend/services/platform_fulfillment/runtime.py @@ -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(), diff --git a/web/frontend/src/pages/PlatformFulfillmentPage.tsx b/web/frontend/src/pages/PlatformFulfillmentPage.tsx index a15ebcf..1b3226e 100644 --- a/web/frontend/src/pages/PlatformFulfillmentPage.tsx +++ b/web/frontend/src/pages/PlatformFulfillmentPage.tsx @@ -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() { {bindingStatusLabel(task.bindingStatus)} + {task.account && task.bindingStatus === 'failed' ? ( + 账号保留中 + ) : null} {task.bindTask?.roleName || (task.bindTask?.bindPhase === 'role_ready' ? '角色待确认' : '-')} {task.bindingStatus === 'bound' ? ( @@ -1536,6 +1552,11 @@ export default function PlatformFulfillmentPage() { 重新选号并绑定 ) : null} + {task.account && task.bindingStatus === 'failed' ? ( + + ) : null} {task.executionStatus === 'role_ready' ? (