394 lines
12 KiB
Python
394 lines
12 KiB
Python
"""账号内部同步队列执行器。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from collections.abc import Callable
|
||
from concurrent.futures import as_completed
|
||
from datetime import UTC, datetime
|
||
from typing import Any
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..database import WorkerSessionLocal
|
||
from ..models import Account, AccountWorkItem
|
||
from .account_exchange_sync_service import sync_exchange_records
|
||
from .account_sync_service import (
|
||
DOMAIN_ELITE,
|
||
DOMAIN_ESPORTS,
|
||
DOMAIN_EXCHANGE_RECORDS,
|
||
DOMAIN_GOLD,
|
||
DOMAIN_POINTS,
|
||
DOMAIN_ROLE,
|
||
DOMAIN_STOREHOME,
|
||
sync_storehome_snapshot,
|
||
)
|
||
from .account_work_queue import (
|
||
claim_account_work,
|
||
complete_account_work,
|
||
fail_account_work,
|
||
)
|
||
from .douyu_account_probe import (
|
||
config_mapping,
|
||
persist_probe_result,
|
||
probe_account,
|
||
)
|
||
from .douyu_activity_query import activity_client
|
||
from .douyu_read_gateway import StorehomeSnapshot, fetch_storehome_snapshot
|
||
from .douyu_service import ensure_douyu_config
|
||
from .task_runtime import TaskLane, available_slots, submit
|
||
|
||
WORK_STOREHOME_SYNC = DOMAIN_STOREHOME + "_sync"
|
||
WORK_POINTS_SYNC = DOMAIN_POINTS + "_sync"
|
||
WORK_GOLD_SYNC = DOMAIN_GOLD + "_sync"
|
||
WORK_ROLE_SYNC = DOMAIN_ROLE + "_sync"
|
||
WORK_ELITE_SYNC = DOMAIN_ELITE + "_sync"
|
||
WORK_ESPORTS_SYNC = DOMAIN_ESPORTS + "_sync"
|
||
WORK_EXCHANGE_RECORDS_SYNC = DOMAIN_EXCHANGE_RECORDS + "_sync"
|
||
ACCOUNT_SYNC_WORK_TYPES = (
|
||
WORK_STOREHOME_SYNC,
|
||
WORK_POINTS_SYNC,
|
||
WORK_GOLD_SYNC,
|
||
WORK_ROLE_SYNC,
|
||
WORK_ELITE_SYNC,
|
||
WORK_ESPORTS_SYNC,
|
||
WORK_EXCHANGE_RECORDS_SYNC,
|
||
)
|
||
|
||
|
||
def _execute_storehome_sync(
|
||
db: Session,
|
||
*,
|
||
account: Account,
|
||
item: AccountWorkItem,
|
||
client_factory: Callable[[str], Any],
|
||
snapshot_fetcher: Callable[..., StorehomeSnapshot],
|
||
) -> dict[str, Any]:
|
||
if not account.cookie:
|
||
raise RuntimeError("账号没有可用 CK,不能同步商品能力")
|
||
if account.ck_check_status in {"invalid", "manual"}:
|
||
raise RuntimeError(f"账号 CK 状态为 {account.ck_check_status},暂不能同步商品能力")
|
||
config = ensure_douyu_config(db)
|
||
manual_id = str(getattr(config, "manual_id", "") or "")
|
||
rid = str(getattr(config, "rid", "") or "")
|
||
if not manual_id:
|
||
raise RuntimeError("未配置精英宝典 manualID")
|
||
client = client_factory(account.cookie)
|
||
snapshot = snapshot_fetcher(
|
||
client,
|
||
manual_id=manual_id,
|
||
rid=rid,
|
||
)
|
||
result = sync_storehome_snapshot(
|
||
db,
|
||
account,
|
||
snapshot,
|
||
source="account_work.storehome_sync",
|
||
)
|
||
return {
|
||
"ok": True,
|
||
"work_id": int(item.id),
|
||
"account_id": int(account.id),
|
||
"item_count": result.item_count,
|
||
"changed_count": result.changed_count,
|
||
}
|
||
|
||
|
||
def _profile_probe_kwargs(
|
||
work_type: str,
|
||
config: Any,
|
||
) -> dict[str, Any]:
|
||
base = {
|
||
"with_elite": False,
|
||
"with_esports": False,
|
||
"with_storehome": False,
|
||
"with_eligibility": False,
|
||
"with_limited_goods": False,
|
||
"with_gold": False,
|
||
"with_goods": False,
|
||
"with_elite_role": False,
|
||
"include_esports": False,
|
||
"include_peace": False,
|
||
"use_cache": False,
|
||
}
|
||
if work_type == WORK_POINTS_SYNC:
|
||
return {**base, "with_eligibility": True}
|
||
if work_type == WORK_GOLD_SYNC:
|
||
return {**base, "with_gold": True}
|
||
if work_type == WORK_ROLE_SYNC:
|
||
return {
|
||
**base,
|
||
"with_elite_role": True,
|
||
"include_esports": True,
|
||
"include_peace": bool(getattr(config, "peace_enabled", False)),
|
||
}
|
||
if work_type == WORK_ELITE_SYNC:
|
||
return {**base, "with_elite": True}
|
||
if work_type == WORK_ESPORTS_SYNC:
|
||
return {**base, "with_esports": True}
|
||
raise RuntimeError(f"不支持的账号资料同步类型: {work_type}")
|
||
|
||
|
||
def _profile_domain_succeeded(work_type: str, probe: dict[str, Any]) -> bool:
|
||
if work_type == WORK_POINTS_SYNC:
|
||
eligibility = probe.get("eligibility") or {}
|
||
return bool(
|
||
eligibility.get("points") is not None
|
||
and isinstance(eligibility.get("points_response"), dict)
|
||
and eligibility["points_response"]
|
||
)
|
||
if work_type == WORK_GOLD_SYNC:
|
||
return (probe.get("gold") or {}).get("status") == "ok"
|
||
if work_type == WORK_ROLE_SYNC:
|
||
roles = probe.get("roles") or {}
|
||
return any(
|
||
(role or {}).get("status") in {"bound", "pending", "unbound"}
|
||
for role in roles.values()
|
||
if isinstance(role, dict)
|
||
)
|
||
if work_type == WORK_ELITE_SYNC:
|
||
return (probe.get("elite") or {}).get("status") == "ok"
|
||
if work_type == WORK_ESPORTS_SYNC:
|
||
return (probe.get("esports") or {}).get("status") == "ok"
|
||
return False
|
||
|
||
|
||
def _execute_profile_sync(
|
||
db: Session,
|
||
*,
|
||
account: Account,
|
||
item: AccountWorkItem,
|
||
client_factory: Callable[[str], Any],
|
||
probe_fetcher: Callable[..., dict[str, Any]],
|
||
) -> dict[str, Any]:
|
||
if not account.cookie:
|
||
raise RuntimeError("账号没有可用 CK,不能同步账号资料")
|
||
if account.ck_check_status in {"invalid", "manual"}:
|
||
raise RuntimeError(f"账号 CK 状态为 {account.ck_check_status},暂不能同步")
|
||
config = ensure_douyu_config(db)
|
||
client = client_factory(account.cookie)
|
||
probe = probe_fetcher(
|
||
account,
|
||
config,
|
||
client=client,
|
||
**_profile_probe_kwargs(item.work_type, config),
|
||
)
|
||
errors = probe.get("errors") or {}
|
||
domain_error = {
|
||
WORK_POINTS_SYNC: "points",
|
||
WORK_GOLD_SYNC: "gold",
|
||
WORK_ROLE_SYNC: "elite_role",
|
||
WORK_ELITE_SYNC: "elite",
|
||
WORK_ESPORTS_SYNC: "esports",
|
||
}.get(item.work_type, "")
|
||
if domain_error and domain_error in errors:
|
||
raise RuntimeError(str(errors[domain_error]))
|
||
if not _profile_domain_succeeded(item.work_type, probe):
|
||
detail = ";".join(str(value) for value in errors.values())
|
||
raise RuntimeError(detail or f"{item.work_type} 未返回可落库结果")
|
||
persist_probe_result(
|
||
db,
|
||
account,
|
||
probe,
|
||
source=f"account_work.{item.work_type}",
|
||
record_event=False,
|
||
update_session=False,
|
||
)
|
||
return {
|
||
"ok": True,
|
||
"work_id": int(item.id),
|
||
"account_id": int(account.id),
|
||
"work_type": item.work_type,
|
||
"errors": sorted(errors),
|
||
}
|
||
|
||
|
||
def _execute_exchange_records_sync(
|
||
db: Session,
|
||
*,
|
||
account: Account,
|
||
item: AccountWorkItem,
|
||
client_factory: Callable[[str], Any],
|
||
) -> dict[str, Any]:
|
||
if not account.cookie:
|
||
raise RuntimeError("账号没有可用 CK,不能同步兑换记录")
|
||
if account.ck_check_status in {"invalid", "manual"}:
|
||
raise RuntimeError(
|
||
f"账号 CK 状态为 {account.ck_check_status},暂不能同步兑换记录"
|
||
)
|
||
config = ensure_douyu_config(db)
|
||
result = sync_exchange_records(
|
||
db,
|
||
account,
|
||
client_factory(account.cookie),
|
||
config_mapping(config),
|
||
source=f"account_work.{item.work_type}",
|
||
)
|
||
return {
|
||
"ok": True,
|
||
"work_id": int(item.id),
|
||
"account_id": int(account.id),
|
||
"work_type": item.work_type,
|
||
**result,
|
||
}
|
||
|
||
|
||
def execute_account_work_item(
|
||
work_id: int,
|
||
*,
|
||
session_factory: Callable[[], Session] = WorkerSessionLocal,
|
||
client_factory: Callable[[str], Any] = activity_client,
|
||
snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot,
|
||
probe_fetcher: Callable[..., dict[str, Any]] = probe_account,
|
||
) -> dict[str, Any]:
|
||
"""在一个短事务中执行单条账号维护工作。"""
|
||
session = session_factory()
|
||
item: AccountWorkItem | None = None
|
||
try:
|
||
item = session.get(AccountWorkItem, int(work_id))
|
||
if item is None or item.status != "running":
|
||
return {"ok": False, "work_id": int(work_id), "skipped": True}
|
||
account = session.get(Account, int(item.account_id))
|
||
if account is None:
|
||
raise RuntimeError("工作项对应账号不存在")
|
||
if item.work_type == WORK_STOREHOME_SYNC:
|
||
result = _execute_storehome_sync(
|
||
session,
|
||
account=account,
|
||
item=item,
|
||
client_factory=client_factory,
|
||
snapshot_fetcher=snapshot_fetcher,
|
||
)
|
||
elif item.work_type == WORK_EXCHANGE_RECORDS_SYNC:
|
||
result = _execute_exchange_records_sync(
|
||
session,
|
||
account=account,
|
||
item=item,
|
||
client_factory=client_factory,
|
||
)
|
||
elif item.work_type in ACCOUNT_SYNC_WORK_TYPES:
|
||
result = _execute_profile_sync(
|
||
session,
|
||
account=account,
|
||
item=item,
|
||
client_factory=client_factory,
|
||
probe_fetcher=probe_fetcher,
|
||
)
|
||
else:
|
||
raise RuntimeError(f"不支持的工作类型: {item.work_type}")
|
||
complete_account_work(session, item)
|
||
session.commit()
|
||
return result
|
||
except Exception as exc: # noqa: BLE001 - 单工作项失败必须收敛到队列状态
|
||
if not session.is_active:
|
||
session.rollback()
|
||
item = session.get(AccountWorkItem, int(work_id))
|
||
if item is not None:
|
||
fail_account_work(
|
||
session,
|
||
item,
|
||
error=exc,
|
||
retry_after_seconds=15 * 60,
|
||
)
|
||
session.commit()
|
||
return {
|
||
"ok": False,
|
||
"work_id": int(work_id),
|
||
"error": f"{type(exc).__name__}: {exc}"[:500],
|
||
}
|
||
finally:
|
||
session.close()
|
||
|
||
|
||
def run_account_sync_queue(
|
||
db: Session,
|
||
*,
|
||
batch_size: int = 20,
|
||
now: datetime | None = None,
|
||
session_factory: Callable[[], Session] = WorkerSessionLocal,
|
||
client_factory: Callable[[str], Any] = activity_client,
|
||
snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot,
|
||
probe_fetcher: Callable[..., dict[str, Any]] = probe_account,
|
||
) -> dict[str, Any]:
|
||
"""领取少量到期工作并通过独立账号同步槽执行。"""
|
||
moment = now or datetime.now(UTC)
|
||
capacity = available_slots(TaskLane.ACCOUNT_SYNC)
|
||
if capacity <= 0:
|
||
return {
|
||
"scanned": 0,
|
||
"success": 0,
|
||
"failed": 0,
|
||
"message": "账号同步槽已满,本轮跳过",
|
||
}
|
||
limit = max(1, min(int(batch_size), capacity))
|
||
items = claim_account_work(
|
||
db,
|
||
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||
limit=limit,
|
||
now=moment,
|
||
)
|
||
db.commit()
|
||
if not items:
|
||
return {
|
||
"scanned": 0,
|
||
"success": 0,
|
||
"failed": 0,
|
||
"message": "暂无到期的账号同步工作",
|
||
}
|
||
|
||
futures = {}
|
||
for item in items:
|
||
work_id = int(item.id)
|
||
future = submit(
|
||
TaskLane.ACCOUNT_SYNC,
|
||
execute_account_work_item,
|
||
work_id,
|
||
session_factory=session_factory,
|
||
client_factory=client_factory,
|
||
snapshot_fetcher=snapshot_fetcher,
|
||
probe_fetcher=probe_fetcher,
|
||
)
|
||
if future is not None:
|
||
futures[future] = work_id
|
||
|
||
success = 0
|
||
failed = 0
|
||
skipped = 0
|
||
for future in as_completed(futures):
|
||
try:
|
||
result = future.result()
|
||
except Exception: # noqa: BLE001 - worker 异常已由执行函数收敛,这里只计数
|
||
failed += 1
|
||
continue
|
||
if result.get("ok"):
|
||
success += 1
|
||
elif result.get("skipped"):
|
||
skipped += 1
|
||
else:
|
||
failed += 1
|
||
|
||
return {
|
||
"scanned": len(futures),
|
||
"success": success,
|
||
"failed": failed,
|
||
"skipped": skipped,
|
||
"message": (
|
||
f"账号同步完成 {success} 条"
|
||
+ (f",失败 {failed} 条" if failed else "")
|
||
+ (f",跳过 {skipped} 条" if skipped else "")
|
||
),
|
||
}
|
||
|
||
|
||
__all__ = [
|
||
"ACCOUNT_SYNC_WORK_TYPES",
|
||
"WORK_ELITE_SYNC",
|
||
"WORK_ESPORTS_SYNC",
|
||
"WORK_EXCHANGE_RECORDS_SYNC",
|
||
"WORK_GOLD_SYNC",
|
||
"WORK_POINTS_SYNC",
|
||
"WORK_ROLE_SYNC",
|
||
"WORK_STOREHOME_SYNC",
|
||
"execute_account_work_item",
|
||
"run_account_sync_queue",
|
||
]
|