feat(douyu): 整合自动化中心并修复重登队列
This commit is contained in:
@@ -194,7 +194,11 @@ def _load_batch_state(
|
||||
|
||||
|
||||
def settle_fresh_results(
|
||||
batch_id: str, *, now: datetime, max_attempts: int = 2
|
||||
batch_id: str,
|
||||
*,
|
||||
now: datetime,
|
||||
max_attempts: int = 2,
|
||||
expected_account_ids: list[int] | tuple[int, ...] | None = None,
|
||||
) -> dict[str, int]:
|
||||
"""在**新会话(新事务)**里读回批次结果并结算。
|
||||
|
||||
@@ -209,10 +213,21 @@ def settle_fresh_results(
|
||||
session = WorkerSessionLocal()
|
||||
try:
|
||||
fresh, order = _load_batch_state(session, batch_id)
|
||||
# runner 可能因登录预算、账号预检等原因没有生成 LoginTask。此时也必须
|
||||
# 结算本轮已经 claim 的账号,否则工作项会永久停在 running,并阻塞后续
|
||||
# 所有重登候选。2026-11-22 生产曾因此卡住 5 条、堵住 261 条积压。
|
||||
settle_account_ids = list(
|
||||
dict.fromkeys(
|
||||
[
|
||||
*order,
|
||||
*(int(account_id) for account_id in expected_account_ids or ()),
|
||||
]
|
||||
)
|
||||
)
|
||||
counters = settle_results(
|
||||
session,
|
||||
fresh,
|
||||
order,
|
||||
settle_account_ids,
|
||||
now=now,
|
||||
max_attempts=max_attempts,
|
||||
)
|
||||
@@ -560,7 +575,12 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
runner_db.close()
|
||||
|
||||
# runner 在另一个会话里提交了结果:必须用新事务读回(见 settle_fresh_results)。
|
||||
counters = settle_fresh_results(batch_id, now=now, max_attempts=max_attempts)
|
||||
counters = settle_fresh_results(
|
||||
batch_id,
|
||||
now=now,
|
||||
max_attempts=max_attempts,
|
||||
expected_account_ids=account_ids,
|
||||
)
|
||||
paused, hard_failures, hard_failure_ratio = evaluate_failure_circuit(
|
||||
counters,
|
||||
config,
|
||||
@@ -649,13 +669,18 @@ def settle_results(
|
||||
for account_id in account_ids:
|
||||
entry = fresh.get(account_id)
|
||||
if entry is None:
|
||||
account = db.get(Account, int(account_id))
|
||||
retry_at = now + timedelta(hours=1)
|
||||
if account is not None:
|
||||
account.relogin_next_at = retry_at
|
||||
reschedule_account_work_type(
|
||||
db,
|
||||
account_id=int(account_id),
|
||||
work_type=WORK_LOGIN_RELOGIN,
|
||||
due_at=now + timedelta(hours=1),
|
||||
due_at=retry_at,
|
||||
reason="自动重登未产生账号任务,重新排队",
|
||||
)
|
||||
counters["retry"] += 1
|
||||
continue
|
||||
account, task = entry
|
||||
if task.attempt_status == "success":
|
||||
|
||||
Reference in New Issue
Block a user