优化(账号自动化): 履约保护登录配额与重登优先级

This commit is contained in:
yml
2026-11-25 22:13:21 +08:00
parent 0943b7a362
commit 9bdd2f4e1a
25 changed files with 1510 additions and 193 deletions
@@ -1,11 +1,12 @@
"""租约账号 CK 到期前刷新(P1)。
"""履约保护账号 CK 到期前刷新(P1)。
`ck_relogin` 只处理已经 `ck_check_status=invalid` 的账号;但租约账号如果等到
失效再重登,履约期间会出现一段不可用窗口。这个作业专门在 CK **预测到期前
24 小时**,对仍持有有效 CK 的生效租约账号做一次主动登录,延长 CK 生命周期。
`ck_relogin` 只处理已经 `ck_check_status=invalid` 的账号;但履约保护账号如果
等到失效再重登,履约期间会出现一段不可用窗口。这个作业专门在 CK **预测到期前
24 小时**,对仍持有有效 CK 的履约保护账号做一次主动登录,延长 CK 生命周期。
范围:
- 预售履约任务分配的账号、进行中的斗鱼外部协作单绑定账号;
- `account_leases.status=active` 且 purpose 为 `platform_fulfillment` / `douyu_collab`;
- `ck_check_status=valid`、有 Cookie、可自动管理、没有在途登录;
- 预测到期时间 <= now + 24h(`ck_expected_expires_at` 或 `cookie_updated_at + 7d`);
@@ -23,18 +24,18 @@ from typing import Any
from sqlalchemy import func, or_
from sqlalchemy.orm import Session
from ....models import Account, AccountLease, AutomationJob, User
from ....models import Account, AutomationJob, User
from ...account_lifecycle import DEFAULT_CK_LIFETIME, MANUAL_KINDS
from ...account_protection_service import load_protection_snapshot
from ...account_query_filters import automation_account_filter
from ...login_automation import run_login_batch_with_fallback
from ...login_quota_service import CATEGORY_LEASE_REFRESH
JOB_KEY = "lease_ck_refresh"
# 单账号总时长上限:要覆盖斗鱼验证码邮件的最坏投递延迟(实测 8~13 分钟),
# 否则等待会被掐断成"等待验证码超时",租约账号一批批地失败。
LEASE_MAX_TOTAL_TIME = 1800
TITLE = "租约 CK 到期前刷新"
# 单账号总时长上限:覆盖极验 + 一轮邮箱验证码最多 3 分钟(180s)等待。
LEASE_MAX_TOTAL_TIME = 300
TITLE = "履约 CK 到期前刷新"
REFRESH_BEFORE = timedelta(hours=24)
@@ -55,15 +56,17 @@ def due_lease_account_query(
db: Session,
now: datetime | None = None,
):
"""到期前 24h 的租约账号查询(执行和测试共用)。"""
"""到期前 24h 的履约保护账号查询(预售 / 协作单 / 生效租约共用)。"""
moment = now or datetime.now(UTC)
cutoff = moment + REFRESH_BEFORE
protection = load_protection_snapshot(db)
relogin_ids = protection.relogin_ids
if not relogin_ids:
return db.query(Account).filter(Account.id == -1)
return (
db.query(Account)
.join(AccountLease, AccountLease.account_id == Account.id)
.filter(
AccountLease.status == "active",
AccountLease.purpose.in_(("platform_fulfillment", "douyu_collab")),
Account.id.in_(relogin_ids),
Account.ck_check_status == "valid",
Account.cookie_updated_at.isnot(None),
automation_account_filter(cookie_operable=True),
@@ -79,7 +82,12 @@ def due_lease_account_query(
Account.relogin_next_at <= moment,
),
)
.order_by(Account.cookie_updated_at.asc(), Account.id.asc())
.order_by(
Account.ck_expected_expires_at.is_(None).asc(),
Account.ck_expected_expires_at.asc(),
Account.cookie_updated_at.asc(),
Account.id.asc(),
)
)