feat(douyu): 落地账号平台同步与登录配额
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from loguru import logger
|
||||
@@ -43,8 +43,12 @@ from .account_events_service import (
|
||||
EVENT_STATUS_CHANGED,
|
||||
emit,
|
||||
)
|
||||
from .account_work_queue import complete_account_work_type, enqueue_account_work
|
||||
from .high_value_service import invalidate_high_value
|
||||
|
||||
WORK_LOGIN_RELOGIN = "login_relogin"
|
||||
DEFAULT_CK_LIFETIME = timedelta(days=7)
|
||||
|
||||
# ── 状态取值域(6 个)──────────────────────────────────────
|
||||
# 刻意**不做** `relogin`(退避重试中):它完全等价于
|
||||
# `invalid + relogin_next_at is not None`,独立成状态就多出一个必须与
|
||||
@@ -318,6 +322,53 @@ def on_ck_verdict(
|
||||
}
|
||||
account.ck_checked_at = now
|
||||
|
||||
if verdict == "valid":
|
||||
account.ck_last_valid_at = now
|
||||
account.ck_invalid_detected_at = None
|
||||
issued_at = _as_utc(account.cookie_updated_at)
|
||||
if issued_at is not None:
|
||||
account.ck_expected_expires_at = issued_at + DEFAULT_CK_LIFETIME
|
||||
account.ck_expiry_source = "predicted"
|
||||
if account.id is not None:
|
||||
try:
|
||||
complete_account_work_type(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
work_type=WORK_LOGIN_RELOGIN,
|
||||
now=now,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 队列是派生索引
|
||||
logger.warning(
|
||||
"完成重登工作项失败 account_id={}: {}",
|
||||
account.id,
|
||||
exc,
|
||||
)
|
||||
elif verdict == "invalid":
|
||||
if previous != "invalid":
|
||||
account.ck_invalid_detected_at = now
|
||||
issued_at = _as_utc(account.cookie_updated_at)
|
||||
if issued_at is not None and issued_at <= now:
|
||||
lifetime = int((now - issued_at).total_seconds())
|
||||
if 0 < lifetime <= int(timedelta(days=90).total_seconds()):
|
||||
account.ck_observed_lifetime_seconds = lifetime
|
||||
account.ck_expected_expires_at = None
|
||||
account.ck_expiry_source = "observed"
|
||||
if account.id is not None:
|
||||
try:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
work_type=WORK_LOGIN_RELOGIN,
|
||||
priority=30,
|
||||
reason="CK 已判定失效,等待受预算控制的重登",
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 生命周期主流程优先
|
||||
logger.warning(
|
||||
"登记自动重登工作项失败 account_id={}: {}",
|
||||
account.id,
|
||||
exc,
|
||||
)
|
||||
|
||||
if verdict == "invalid" and previous != "invalid":
|
||||
emit(
|
||||
db,
|
||||
@@ -357,6 +408,12 @@ def on_login_success(
|
||||
account.relogin_next_at = None
|
||||
account.relogin_failure_kind = ""
|
||||
account.relogin_last_error = ""
|
||||
complete_account_work_type(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
work_type=WORK_LOGIN_RELOGIN,
|
||||
now=occurred_at,
|
||||
)
|
||||
sync_status(db, account, occurred_at=occurred_at)
|
||||
|
||||
|
||||
@@ -380,6 +437,11 @@ def on_ck_cleared(
|
||||
account.ck_check_status = ""
|
||||
account.ck_check_result = None
|
||||
account.ck_checked_at = None
|
||||
account.ck_expected_expires_at = None
|
||||
account.ck_last_valid_at = None
|
||||
account.ck_invalid_detected_at = None
|
||||
account.ck_observed_lifetime_seconds = None
|
||||
account.ck_expiry_source = ""
|
||||
|
||||
emit(
|
||||
db,
|
||||
@@ -424,3 +486,9 @@ def warn_if_drift(account: Account) -> None:
|
||||
logger.warning(
|
||||
f"账号状态漂移:account={account.id} 落库={account.status} 推导={expected}"
|
||||
)
|
||||
|
||||
|
||||
def _as_utc(value: datetime | None) -> datetime | None:
|
||||
if value is None:
|
||||
return None
|
||||
return value.replace(tzinfo=UTC) if value.tzinfo is None else value.astimezone(UTC)
|
||||
|
||||
Reference in New Issue
Block a user