feat(douyu): 落地账号平台同步与登录配额
This commit is contained in:
@@ -18,7 +18,7 @@ from core.douyu import DouyuLogin, create_login_api_strategy
|
||||
from core.douyu.login import AccountLike
|
||||
|
||||
from ..models import Account as AccountModel
|
||||
from ..models import LoginTask
|
||||
from ..models import LoginAttempt, LoginTask
|
||||
from ..models import ProxyConfig as ProxyConfigModel
|
||||
from .account_events_service import (
|
||||
EVENT_LOGIN_FAILED,
|
||||
@@ -27,10 +27,17 @@ from .account_events_service import (
|
||||
emit,
|
||||
)
|
||||
from .account_lifecycle import on_login_success, sync_status
|
||||
from .account_sync_service import DOMAIN_STOREHOME, mark_domain_dirty
|
||||
from .account_work_queue import enqueue_account_work
|
||||
from .cookie_check_service import check_douyu_cookie
|
||||
from .douyu_account_probe import persist_probe_result, probe_session
|
||||
from .douyu_service import LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
from .login_failure import classify_failure, failure_kind_label, is_account_fault
|
||||
from .login_quota_service import (
|
||||
finish_login_attempt,
|
||||
login_concurrency_limit,
|
||||
reserve_login_attempt,
|
||||
)
|
||||
from .proxy_service import build_proxy_runtime
|
||||
from .task_runtime import TaskLane, submit
|
||||
|
||||
@@ -240,6 +247,23 @@ def _write_login_success(
|
||||
)
|
||||
|
||||
|
||||
def _enqueue_post_login_sync(db: Session, account: AccountModel) -> None:
|
||||
"""登录并验证成功后,把商品能力标脏并进入内部同步队列。"""
|
||||
mark_domain_dirty(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_STOREHOME,
|
||||
reason="登录成功,等待同步完整商城商品能力",
|
||||
)
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
work_type=DOMAIN_STOREHOME + "_sync",
|
||||
priority=20,
|
||||
reason="登录成功联动同步",
|
||||
)
|
||||
|
||||
|
||||
def _persist_login_cookie_verification(
|
||||
db: Session,
|
||||
task: LoginTask,
|
||||
@@ -313,6 +337,7 @@ class LoginBatchRunner:
|
||||
concurrency: int = 3,
|
||||
api_strategy: str = "wgapi",
|
||||
mode: str = "login",
|
||||
trigger_source: str = "manual",
|
||||
):
|
||||
self.db = db
|
||||
self.account_ids = account_ids
|
||||
@@ -326,9 +351,16 @@ class LoginBatchRunner:
|
||||
self.log_queue = log_queue
|
||||
self.loop = loop
|
||||
self.batch_id = uuid.uuid4().hex[:12]
|
||||
self.concurrency = max(1, min(concurrency, 10)) # 限制 1-10
|
||||
self.concurrency = max(
|
||||
1,
|
||||
min(concurrency, 10, login_concurrency_limit(db)),
|
||||
)
|
||||
self.api_strategy = create_login_api_strategy(api_strategy)
|
||||
self.mode = mode if mode in ("login", "check", "relogin") else "login"
|
||||
self.trigger_source = trigger_source if trigger_source in {
|
||||
"manual",
|
||||
"automation",
|
||||
} else "manual"
|
||||
self._stop = threading.Event()
|
||||
self._counter_lock = threading.Lock()
|
||||
self._completed = 0
|
||||
@@ -587,6 +619,14 @@ class LoginBatchRunner:
|
||||
batch_id=self.batch_id,
|
||||
mode=self.mode,
|
||||
)
|
||||
if account.ck_check_status == "valid":
|
||||
try:
|
||||
_enqueue_post_login_sync(worker_db, account)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
self._push_log(
|
||||
"warning",
|
||||
f"[{current}] 登录成功,但登记商品同步队列失败: {exc}",
|
||||
)
|
||||
if account is not None and account.ck_check_status == "valid":
|
||||
self._push_log(
|
||||
"success",
|
||||
@@ -718,6 +758,21 @@ class LoginBatchRunner:
|
||||
self._push_log("warning", f"跳过无权账号: {acc.username}")
|
||||
continue
|
||||
|
||||
quota = reserve_login_attempt(
|
||||
self.db,
|
||||
account=acc,
|
||||
mode=self.mode,
|
||||
trigger_source=self.trigger_source,
|
||||
batch_id=batch_id,
|
||||
created_by=self.created_by,
|
||||
)
|
||||
if not quota.allowed:
|
||||
self._push_log(
|
||||
"warning",
|
||||
f"跳过账号 {acc.username}: {quota.reason}",
|
||||
)
|
||||
continue
|
||||
|
||||
# 解耦后不再需要「哪一行持有 CK」这个不变量(CK 在账号上)。
|
||||
# 这里只需要保证**同一账号不并发两行**:
|
||||
# ① 复用该账号在途的尝试(避免并发两个批次各建一行);
|
||||
@@ -806,6 +861,10 @@ class LoginBatchRunner:
|
||||
self._push_log("info", f"批量{action_name}任务 {batch_id} 完成")
|
||||
self._push_log("result", "")
|
||||
finally:
|
||||
try:
|
||||
_settle_login_attempts(batch_id)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.exception(f"[登录批次 {batch_id}] 结算登录尝试流水失败: {exc}")
|
||||
# 标记批次结束:WS 端据此决定何时清理注册表(断线重连可继续订阅日志)。
|
||||
batch_registry.mark_finished(batch_id)
|
||||
# 确保 DB Session 被关闭,避免连接泄漏
|
||||
@@ -853,6 +912,61 @@ class BatchRegistry:
|
||||
return self._batches.pop(batch_id, None)
|
||||
|
||||
|
||||
def _settle_login_attempts(batch_id: str) -> None:
|
||||
"""用新的短事务把本批不可变登录流水结转到终态。"""
|
||||
session = WorkerSessionLocal()
|
||||
try:
|
||||
attempts = (
|
||||
session.query(LoginAttempt)
|
||||
.filter(
|
||||
LoginAttempt.batch_id == batch_id,
|
||||
LoginAttempt.status.in_(("pending", "running")),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for attempt in attempts:
|
||||
task = (
|
||||
session.query(LoginTask)
|
||||
.filter(
|
||||
LoginTask.account_id == attempt.account_id,
|
||||
LoginTask.attempt_batch_id == batch_id,
|
||||
)
|
||||
.order_by(LoginTask.id.desc())
|
||||
.first()
|
||||
)
|
||||
task_status = (task.attempt_status if task else "") or (
|
||||
task.status if task else ""
|
||||
)
|
||||
message = (task.attempt_message if task else "") or (
|
||||
task.message if task else ""
|
||||
)
|
||||
if task_status == "success" or task_status == "account_verified":
|
||||
status = "success"
|
||||
failure_kind = ""
|
||||
elif task_status in CHECK_STATUS_MESSAGES:
|
||||
status = "failed"
|
||||
failure_kind = (
|
||||
"network"
|
||||
if task_status == "account_auth_unknown"
|
||||
else "credential"
|
||||
)
|
||||
elif task_status in {"failed", "error"}:
|
||||
status = task_status
|
||||
failure_kind = classify_failure(message)
|
||||
else:
|
||||
status = "failed"
|
||||
failure_kind = "interrupted"
|
||||
finish_login_attempt(
|
||||
session,
|
||||
attempt,
|
||||
status=status,
|
||||
failure_kind=failure_kind,
|
||||
)
|
||||
session.commit()
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
# 模块级单例
|
||||
batch_registry = BatchRegistry()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user