Files
live-hub-py/web/backend/services/automation/jobs/account_lease_reaper.py
T
yml2213 18bc7be4b1 feat(pool): 账号自动分配与回收(租约 + 补给链 + 独立工作台)
在既有「管理员分配 → 客服导入工作台 → 手动选号」之外**新增**一条
「客服选皮肤 + 数量 → 系统分配账号 + 按需补积分 → 用完自动收回」的路径。
旧路径只做并集与可选参数扩展,行为不变(有用例守着)。

- 归属与使用权分离:新增 account_leases(生成列 + 唯一索引,并发抢号由数据库裁决),
  assigned_to 收窄为管理员/运营的维护责任;客服可见范围扩为「名下 ∪ 租用中」
- account_pool 服务:候选筛选、两梯队排序(先花快过期的 / 先选充值少的)、
  供给与成本预估、租约领取/释放/回收
- 补给链:查余额 → 充鱼翅(单次 ≤800 元,超限拆单)→ 赠精英令 → 兑换;
  运行期两道闸门:_skip_when(余额已够就别再花钱)、_depends_on(前置未成功就跳过)
- 转人工:鱼翅支付等 10 分钟未到账转 manual_required,租约先不还;
  account_lease_reaper 兜 TTL 到期 / 漏释放 / 孤儿批次
- 运营流水:业务事件补 actor_id = task.created_by;账号详情页展示操作人、渠道
  与租赁记录
- 新工作台:/douyu/pool + 菜单「账号分配台」(新建,不改造旧工作台)
- 修复 D1:官方补录的兑换事件漏写 subject_key(10,612 条主键为空),
  会让 365 天同皮肤规则漏判 87% 的历史;补代码 + 回填脚本
- 迁移 20261007_0052;权限 account:pool_use / account:pool_admin

测试:766 passed, 6 skipped(改动前基线 727);ruff 与 tsc -b 干净。
设计稿与实施记录:docs/设计-账号自动分配与回收.md
2026-10-07 12:29:08 +08:00

96 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""作业:兜底回收账号租约(账号池的「自动收回」保险丝)。
## 为什么必须有它
租约的主释放路径是 `_mark_task` 写终态时的即时钩子(`release_for_task`),
覆盖 99% 的情况。但下面三种情况会留下**悬空租约**,而这个账号会一直显示
"被占用"、谁也领不走:
1. **进程被杀 / 重启**:执行器线程没了,任务永远停在 `pending/running`;
2. **释放时抛异常**:`_mark_task` 里那一步是旁路,失败只记 warning;
3. **任务行被保留策略删掉**:批次看着"没有未完成任务",但钩子再也不会跑。
本作业按三条规则收:
| 规则 | 判据 | 置为 |
| --- | --- | --- |
| TTL 到期 | `expires_at <= now` | `expired` |
| 漏释放 | 批次**已无**未完成任务(`planned/pending/running`) | `released` |
| 孤儿批次 | 批次不在**本进程**的执行器注册表里 | `expired` |
## ⚠️ 部署前提
「孤儿批次」这条规则依赖**单进程部署**:注册表 `douyu_batch_registry` 在内存里,
只有跑这个批次的那个进程知道它。生产是 `uvicorn --workers 1`(见
`docker-entrypoint.sh`),所以成立。
**如果将来改成多 worker,这条规则会误杀别的 worker 正在跑的批次** ——
到那时必须换成落库的批次心跳(或在 `douyu_tasks` 上记执行者标识),
不能只靠内存注册表。
"""
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
from ....models import AutomationJob
from ...account_pool import (
orphan_leases,
reap_expired,
reap_missed_releases,
release_many,
)
JOB_KEY = "account_lease_reaper"
#: 一轮最多处理多少条租约(三类各用这个上限)。
DEFAULT_BATCH = 500
def _positive_int(value: Any, default: int) -> int:
try:
parsed = int(value)
except (TypeError, ValueError):
return default
return parsed if parsed > 0 else default
def run_account_lease_reaper(
db: Session, job: AutomationJob, *, now: datetime | None = None
) -> dict[str, Any]:
batch = _positive_int(job.batch_size, DEFAULT_BATCH)
moment = now or datetime.now(UTC)
# 延迟导入:作业模块在应用启动时被加载,顶层导入 runner 会把
# 「路由 → runner → account_pool」整条链在启动期拉起来,容易出循环导入。
from ....services.douyu_runner import douyu_batch_registry
# ① TTL 到期
expired = reap_expired(db, now=moment, batch_size=batch)
# ② 任务已终态但租约没释放
missed = reap_missed_releases(db, limit=batch)
# ③ 批次已不在本进程注册表(进程重启)
active_batch_ids = douyu_batch_registry.active_ids()
orphans = orphan_leases(db, active_batch_ids=active_batch_ids, limit=batch)
orphaned = release_many(db, orphans, reason="orphan_batch", status="expired")
total = expired + missed + orphaned
message = (
f"租约回收 {total} 条"
f"(TTL 到期 {expired} / 漏释放 {missed} / 孤儿批次 {orphaned})"
)
if total:
logger.info(message)
return {
"scanned": total,
"expired": expired,
"missed_release": missed,
"orphan_batch": orphaned,
"active_batches": len(active_batch_ids),
"message": message,
}