diff --git a/docs/斗鱼/斗鱼账号平台最终实施设计.md b/docs/斗鱼/斗鱼账号平台最终实施设计.md index add22f5..9aedabe 100644 --- a/docs/斗鱼/斗鱼账号平台最终实施设计.md +++ b/docs/斗鱼/斗鱼账号平台最终实施设计.md @@ -455,7 +455,7 @@ AND 不违反 365 天规则 - `ck_health`:只生成/执行 `ck_probe`,不处理 invalid/manual - `ck_relogin`:读取 `login_relogin` 工作,受登录配额 -- `account_overview_refresh`:拆成 points/gold/role/exchange_records 同步域 +- `account_overview_refresh`:已淘汰,改由 points/gold/role/exchange_records 各同步域按需维护 - `account_capability_refresh`:变为 `storehome_sync` 补货工作 - `platform_fulfillment_lease_reaper`:保持独立 - `douyu_tasks_retention`:保持独立 diff --git a/docs/斗鱼/账号供给库存与CK生命周期.md b/docs/斗鱼/账号供给库存与CK生命周期.md index 1eb66b3..9609388 100644 --- a/docs/斗鱼/账号供给库存与CK生命周期.md +++ b/docs/斗鱼/账号供给库存与CK生命周期.md @@ -444,7 +444,6 @@ AND 没有 force_refresh | --- | --- | --- | --- | | `ck_health` | 开启 | 每 5 分钟一轮,每轮 200,并发 5 | 高价值账号约 6 小时重检,普通账号约 24 小时 | | `account_capability_refresh` | 开启 | 每 5 分钟一轮,每轮 100,并发 3 | 刷新有 pending 履约商品能力的账号 | -| `account_overview_refresh` | 关闭 | 每轮 300,并发 3 | 曾因单轮 29 个账号全部失败并触发熔断 | | `ck_relogin` | 关闭 | 每轮 30,并发 3 | 自动重登自 2026-10-06 后未再运行 | | `douyu_tasks_retention` | 开启 | 每批 2000 | 清理斗鱼任务日志 | | `points_expire_check` | 关闭 | 每天 | 积分到期补记 | @@ -525,14 +524,11 @@ CK 有效账号中: `ck_relogin` 当前关闭,CK 失效账号已经积压约 2 万。直接重新开启当前配置,可能每 5 分钟 拉起最多 30 个账号,瞬时并发 3,形成不可控的连续登录。 -### 账号资料刷新因熔断关闭 +### 账号资料全量定时刷新已淘汰 -`account_overview_refresh` 曾因一轮 29 个账号全部失败而自动关闭。重新开启前必须先解决: - -- CK 质量筛选; -- 活动登录态与基础 CK 的区分; -- 并发和失败熔断; -- 轻量商品快照与完整资料同步的拆分。 +`account_overview_refresh` 已从默认作业注册表移除,生产配置也会在迁移中停用。 +账号资料改由按需同步、账号目录补货和履约能力补货链路维护,避免全库周期性 +重查产生风控、重复日志和无意义数据库写入。 ## 登录工作分类 @@ -760,7 +756,6 @@ manual:停止自动化 2. 先以每日 30-50 个小批量开启 `ck_relogin`; 3. 监控成功率和失败分型; 4. 再逐步提升到计算预算; -5. 恢复账号资料刷新的小批量试运行。 ### P2:供给库存驱动 diff --git a/docs/自动化与资料收集.md b/docs/自动化与资料收集.md index e56145d..d451527 100644 --- a/docs/自动化与资料收集.md +++ b/docs/自动化与资料收集.md @@ -1,6 +1,6 @@ # 自动化与资料收集 -更新时间:2026-11-06 +更新时间:2026-11-22 本文描述当前已落地的自动化、外部订单读取和文件存储能力。具体实现以 `web/backend/services/automation/`、`external_order_source.py` 和 `file_storage.py` 为准。 @@ -8,7 +8,9 @@ ## 自动化运行模型 - 调度器随应用启动,总开关为 `AUTOMATION_ENABLED`,默认关闭。 -- 每个作业还有独立开关和运行参数,默认关闭,在 `/automation` 页面配置。 +- 每个作业还有独立开关和运行参数,在 `/automation`「斗鱼自动化中心」统一配置。 +- 平台状态、CK 趋势、供给目标、履约保护目标、登录预算、待人工账号和运行记录 + 都集中在该页面;账号检测页只保留批量账号检测本职功能。 - 部署固定单容器、单 worker;作业在进程内线程池运行,重启会把未结束记录标为中断。 - 作业状态和运行记录落 `automation_jobs`、`automation_runs`,不靠内存保存进度。 - 单个作业失败只影响本轮;失败率异常时作业会自动停用,避免继续消耗账号或触发风控。 @@ -19,10 +21,16 @@ | --- | --- | --- | | `ck_health` | 检测持有 CK 的账号 | 区分 `valid`、`invalid`、`error`;`error` 不覆盖上次有效结论 | | `ck_relogin` | 对失效 CK 自动重登 | 有退避、次数上限和失败分型;极验/凭证/必须绑手机转人工 | -| `account_overview_refresh` | 分批刷新账号资料和一键查询快照 | 只查 CK 有效且未停用账号;“没查过”优先于“过期重查” | +| `account_sync_queue` | 独立消费账号目录、积分、鱼翅和角色同步 | 使用独立执行槽,不占用订单选号 | +| `account_capability_refresh` | 为待履约商品补充账号能力目录 | 只做本地候选筛选,外部查询进入同步队列 | +| `account_supply_replenishment` | 按供给目标缺口补充账号 | 通用补货只占低水位,需求补货优先 | +| `platform_fulfillment_lease_reaper` | 回收过期履约租约 | 扫码超时未绑定账号按有界宽限期释放 | | `douyu_tasks_retention` | 分批清理斗鱼执行日志 | 默认启用;噪声查询保留 7 天,其余任务保留 90 天,在途任务不删 | | `points_expire` | 补记积分到期 | 只写账号事件,不修改外部平台 | +`account_overview_refresh` 已淘汰:账号资料改由按需同步、履约能力补货和 CK +生命周期链路维护,不再做全库周期性资料重查。 + 所有账号级作业都必须排除 `status` 为 `disabled` / `archived` 的账号,并尊重 `auto_managed=False`。停用是“账号不可用”,`auto_managed=False` 是“账号可用但不要自动操作”, 两者不能混用。 diff --git a/tests/test_account_overview_refresh_job.py b/tests/test_account_overview_refresh_job.py deleted file mode 100644 index 49fe39a..0000000 --- a/tests/test_account_overview_refresh_job.py +++ /dev/null @@ -1,349 +0,0 @@ -"""定时补全账号资料的作业(`account_overview_refresh`)。 - -**为什么不能一次性跑全量**:生产约 8,000 个待查账号,一次性发起 ≈ 8 万次斗鱼请求, -很容易触发风控 —— 而风控的代价是所有账号一起受影响。所以做成定时分批, -并复用「自动巡检」的两道保护(`cookie_operable_filter` + 错误率熔断)。 -""" - -from __future__ import annotations - -from datetime import UTC, datetime, timedelta - -from tests.support.db import get_test_engine, make_test_session -from web.backend.models import Account, AutomationJob, User -from web.backend.services.automation.jobs import account_overview_refresh as job - - -class _Base: - def setup_method(self): - self.engine = get_test_engine() - self.session = make_test_session(self.engine) - self.admin = User(username="ovr-admin", password_hash="h", role="super_admin") - self.session.add(self.admin) - self.session.flush() - self.job = AutomationJob( - key=job.JOB_KEY, - title="账号资料定时补全", - # 「同一账号多久重检」= 7 天(就是界面上那个字段) - interval_seconds=7 * 24 * 3600, - batch_size=10, - concurrency=3, - enabled=False, - ) - self.session.add(self.job) - self.session.commit() - - def teardown_method(self): - self.session.close() - - def account( - self, - name: str, - *, - ck: str = "valid", - queried_days_ago: int | None = None, - auto_managed: bool = True, - status: str = "active", - hv_checked_days_ago: float | None = None, - hv_status: str | None = None, - ) -> Account: - acc = Account( - username=name, - password="p", - email=f"{name}@e.com", - email_password="ep", - cookie="acf_uid=1; acf_ccn=c", - ck_check_status=ck, - auto_managed=auto_managed, - status=status, - ) - if queried_days_ago is not None: - acc.overview_queried_at = datetime.now(UTC) - timedelta( - days=queried_days_ago - ) - if hv_checked_days_ago is not None: - acc.high_value_checked_at = datetime.now(UTC) - timedelta( - days=hv_checked_days_ago - ) - if hv_status is not None: - acc.high_value_bairimeng_status = hv_status - acc.high_value_mengtu_status = hv_status - self.session.add(acc) - self.session.commit() - return acc - - def selected(self) -> list[Account]: - return job.build_select_query(self.session, self.job, datetime.now(UTC)).all() - - -class TestSelection(_Base): - def test_only_valid_ck_is_selected(self): - """只查有效 CK —— 失效的查了也是白查,只会多一条失败记录。""" - self.account("ovr-valid", ck="valid") - self.account("ovr-invalid", ck="invalid") - self.account("ovr-error", ck="error") - names = [a.username for a in self.selected()] - assert "ovr-valid" in names - assert "ovr-invalid" not in names - assert "ovr-error" not in names - - def test_never_queried_comes_first(self): - """**没查过的优先** —— 否则周期性刷新会永远挤掉从没查过的新账号。""" - self.account("ovr-old", queried_days_ago=30) - self.account("ovr-never") - assert self.selected()[0].username == "ovr-never" - - def test_assigned_and_recent_login_lead_within_the_same_group(self): - """没查过的仍整体优先;同一档里已分配的在前,再按最近登录。""" - stale = self.account("ovr-stale-assigned", queried_days_ago=30) - stale.assigned_to = self.admin.id - recent = self.account("ovr-recent") - recent.cookie_updated_at = datetime.now(UTC) - older = self.account("ovr-older") - older.cookie_updated_at = datetime.now(UTC) - timedelta(days=10) - assigned = self.account("ovr-assigned") - assigned.assigned_to = self.admin.id - assigned.cookie_updated_at = datetime.now(UTC) - timedelta(days=1) - self.session.commit() - - names = [a.username for a in self.selected()] - assert names.index("ovr-assigned") < names.index("ovr-recent") - assert names.index("ovr-recent") < names.index("ovr-older") - assert names.index("ovr-older") < names.index("ovr-stale-assigned") - - def test_recently_queried_is_skipped(self): - """刚查过的要跳过(`refresh_days` 之内不再查)。""" - self.account("ovr-fresh", queried_days_ago=1) - names = [a.username for a in self.selected()] - assert "ovr-fresh" not in names - - def test_stale_is_selected_again(self): - self.account("ovr-stale", queried_days_ago=30) - assert "ovr-stale" in [a.username for a in self.selected()] - - def test_recheck_interval_comes_from_the_ui_field(self): - """**用界面上那个「同一账号多久重检」** —— 与其它批量作业同一套口径。 - - 之前这里读的是 `job.config["refresh_days"]`,于是界面上那个字段 - 对本作业是**死的**(改了没反应)—— 正是用户批评过的 - 「看起来能配、其实配的是不相干的东西」。 - """ - self.job.interval_seconds = 100 * 24 * 3600 - self.session.commit() - self.account("ovr-30d", queried_days_ago=30) - assert "ovr-30d" not in [a.username for a in self.selected()], ( - "重检间隔 100 天时,30 天前查过的账号不该重新入选" - ) - - def test_manual_and_unmanaged_accounts_are_excluded(self): - """人工状态(停用/归档)与「不参与自动化」的账号不该被自动碰。""" - self.account("ovr-disabled", status="disabled") - self.account("ovr-archived", status="archived") - self.account("ovr-unmanaged", auto_managed=False) - names = [a.username for a in self.selected()] - for bad in ("ovr-disabled", "ovr-archived", "ovr-unmanaged"): - assert bad not in names - - def test_batch_size_limits_the_round(self): - for i in range(5): - self.account(f"ovr-batch-{i}") - self.job.batch_size = 2 - self.session.commit() - assert len(self.selected()) == 2 - - def test_remaining_count_works_with_limit_applied(self): - """回归:计数不能走 `build_select_query`。 - - 那个查询已经 `LIMIT` 了,再 `order_by(None)` 会被 SQLAlchemy 拒绝: - "order_by() being called on a Query which already has LIMIT"。 - (真踩过 —— 作业第一版就是这么挂的。) - """ - for i in range(4): - self.account(f"ovr-count-{i}") - self.job.batch_size = 1 - self.session.commit() - assert job.count_remaining(self.session, self.job, datetime.now(UTC)) >= 4 - - -class TestHighValueRefresh(_Base): - """高价值结论有**自己独立的时钟** —— 资料刷新必须跟着它走。 - - 缺口长这样:一键查询里商品接口单独失败(其它项都拿到了), - ``overview_queried_at`` 被刷新了、``high_value_checked_at`` 却停在原地。 - 资料看起来很新,于是**再也不会被选中** —— 高价值状态永久停在旧结论。 - """ - - def test_stale_high_value_is_selected_even_if_overview_is_fresh(self): - """资料 1 天前查过(很新),但高价值结论 30 天前 —— 必须重选。""" - self.account( - "ovr-hv-stale", - queried_days_ago=1, - hv_checked_days_ago=30, - hv_status="available", - ) - assert "ovr-hv-stale" in [a.username for a in self.selected()] - - def test_fresh_high_value_is_not_selected(self): - """资料和高价值结论都新鲜 —— 不该被重选(避免重复请求)。""" - self.account( - "ovr-hv-fresh", - queried_days_ago=1, - hv_checked_days_ago=1, - hv_status="available", - ) - assert "ovr-hv-fresh" not in [a.username for a in self.selected()] - - def test_never_checked_high_value_is_not_selected_on_its_own(self): - """从来没查到过高价值结论,**不**因此被重选。 - - 这种情况要么是「账号根本还没查过资料」(本来就该被选中), - 要么是**商品接口对这个账号稳定失败**(没配 manualID、账号没有宝典)。 - 后者若每轮都重选,同一批账号会反复撞墙,把真正有新欠账的挤掉。 - 「资料过期」那条已经兜住了它。 - """ - self.account("ovr-hv-never", queried_days_ago=1) - assert "ovr-hv-never" not in [a.username for a in self.selected()] - - def test_high_value_uses_the_same_ruler_as_the_ui_field(self): - """高价值过期**用同一把尺子**(界面上那个「同一账号多久重检」)。 - - 之前这里读的是 `job.config["refresh_days"]`,界面上那个字段对本作业 - 就是死的;同理,若高价值另设一个窗口,那个字段对本作业就变成半死的。 - """ - self.job.interval_seconds = 100 * 24 * 3600 - self.session.commit() - # 资料 30 天前(按 100 天不该重选),高价值结论 60 天前(同样不该) - self.account( - "ovr-hv-60d", - queried_days_ago=30, - hv_checked_days_ago=60, - hv_status="available", - ) - assert "ovr-hv-60d" not in [a.username for a in self.selected()] - - # 缩回 7 天 → 高价值 60 天前就过期了,要重选 - self.job.interval_seconds = 7 * 24 * 3600 - self.session.commit() - assert "ovr-hv-60d" in [a.username for a in self.selected()] - - def test_high_value_refresh_still_requires_valid_ck(self): - """高价值过期**不能**绕过 CK 有效性 —— CK 失效的查了也是白查。""" - self.account( - "ovr-hv-stale-invalid", - ck="invalid", - queried_days_ago=1, - hv_checked_days_ago=30, - hv_status="available", - ) - assert "ovr-hv-stale-invalid" not in [a.username for a in self.selected()] - - def test_invalidated_conclusion_is_rechecked_within_six_hours(self): - """结论被**作废**(unknown)的账号,6 小时内要重查 —— 哪怕重检间隔是 7 天。 - - 场景:CK 失效期间账号可能被别人手工登录并兑换(这些号共享给客服), - 所以 CK 一判定失效就把结论作废。CK 恢复之后必须**尽快**重新确认 —— - 否则运营会一直拿着一份「待确认」的结论,而它其实已经变了。 - - ⚠️ 这里用的是「资料重检间隔」(7 天)之外的**另一个**窗口: - 作废的结论等 7 天太久了,运营等不了。但也不能更密 —— - 商城接口对某些账号稳定失败,那类账号会一直是 unknown,每轮都重选 - 就是反复撞墙,挤掉真正有新欠账的账号。 - """ - # 资料 1 天前(很新,按 7 天不该重选),高价值结论是 10 小时前的 unknown - self.account( - "ovr-hv-invalidated", - queried_days_ago=1, - hv_checked_days_ago=10 / 24, - hv_status="unknown", - ) - assert "ovr-hv-invalidated" in [a.username for a in self.selected()] - - def test_invalidated_conclusion_is_not_rechecked_too_often(self): - """作废的结论 6 小时内**不**重选 —— 不能反复撞同一个失败的接口。""" - self.account( - "ovr-hv-invalidated-fresh", - queried_days_ago=1, - hv_checked_days_ago=1 / 24, # 1 小时前 - hv_status="unknown", - ) - assert "ovr-hv-invalidated-fresh" not in [a.username for a in self.selected()] - - -class TestCircuitBreaker(_Base): - """失败率过高就**自己把自己关掉**(风控保护)。 - - 与「自动巡检」同口径。大面积失败通常意味着被风控或斗鱼接口抖动 —— - 这时候继续跑只会把更多账号写坏,停手交给人确认才是对的。 - """ - - def _run_with(self, total: int, failed: int): - """打桩执行器,只关心失败率熔断的逻辑。""" - from web.backend.models import DouyuTask - - for i in range(total): - self.account(f"ovr-cb-{i}") - - def fake_create(db, ids, task_type, scope, created_by, payload=None): - batch_id = "cb-batch" - for account_id in ids: - self.session.add( - DouyuTask( - batch_id=batch_id, - account_id=account_id, - task_type=task_type, - handbook_scope=scope, - status="planned", - created_by=created_by, - ) - ) - self.session.commit() - return batch_id, len(ids) - - session = self.session # 闭包捕获:下面 _FakeRunner 里的 self 是 runner - - class _FakeRunner: - def __init__(self, *a, **k): - pass - - def run(self): - # 把前 `failed` 个标成失败,其余成功 - rows = ( - session.query(DouyuTask) - .filter(DouyuTask.batch_id == "cb-batch") - .order_by(DouyuTask.id.asc()) - .all() - ) - for index, row in enumerate(rows): - row.status = "failed" if index < failed else "success" - session.commit() - - import web.backend.services.automation.jobs.account_overview_refresh as mod - - original_create = mod.create_douyu_planned_tasks - original_runner = mod.DouyuBatchRunner - mod.create_douyu_planned_tasks = fake_create - mod.DouyuBatchRunner = _FakeRunner - try: - return job.run_account_overview_refresh(self.session, self.job) - finally: - mod.create_douyu_planned_tasks = original_create - mod.DouyuBatchRunner = original_runner - - def test_high_failure_ratio_disables_the_job(self): - out = self._run_with(total=20, failed=15) # 75% 失败 - self.session.refresh(self.job) - assert out["paused"] is True - assert self.job.enabled is False, "失败率过高应自动关闭作业" - assert "风控" in out["message"] - - def test_normal_failure_ratio_keeps_job_running(self): - out = self._run_with(total=20, failed=1) # 5% 失败(真实的 CK 失效率) - self.session.refresh(self.job) - assert out["paused"] is False - assert "风控" not in out["message"] - - def test_small_sample_does_not_trigger_breaker(self): - """样本太少时比例没有意义(1 个失败就是 100%),不该熔断。""" - out = self._run_with(total=3, failed=3) - self.session.refresh(self.job) - assert out["paused"] is False, "样本不足时不该熔断" diff --git a/tests/test_automation_ck_health.py b/tests/test_automation_ck_health.py index b8fc012..535110a 100644 --- a/tests/test_automation_ck_health.py +++ b/tests/test_automation_ck_health.py @@ -550,6 +550,7 @@ class TestAutomationApi(_Base): assert payload["scheduler"]["running"] is False keys = [item["key"] for item in payload["jobs"]] assert ck_health.JOB_KEY in keys + assert "account_overview_refresh" not in keys job = next(item for item in payload["jobs"] if item["key"] == ck_health.JOB_KEY) assert job["enabled"] is False assert job["interval_seconds"] == 6 * 3600 diff --git a/tests/test_automation_ck_relogin.py b/tests/test_automation_ck_relogin.py index 3c171ff..416a537 100644 --- a/tests/test_automation_ck_relogin.py +++ b/tests/test_automation_ck_relogin.py @@ -22,6 +22,7 @@ from datetime import UTC, datetime, timedelta from tests.support.db import get_test_engine, make_test_session from web.backend.models import ( Account, + AccountWorkItem, AutomationJob, LoginAttempt, LoginQuotaSetting, @@ -735,6 +736,43 @@ class TestSettlementVisibility(_Base): checker.close() stale.close() + def test_settlement_releases_claimed_account_without_attempt(self): + """预算跳过等无尝试记录场景也必须结束 running 工作项。""" + account = self.make_account("acc-no-attempt") + now = datetime.now(UTC) + work = AccountWorkItem( + account_id=account.id, + work_type=ck_relogin.WORK_LOGIN_RELOGIN, + priority=30, + status="running", + due_at=now, + attempt_count=1, + max_attempts=20, + dedupe_key=f"login_relogin:{account.id}", + started_at=now, + ) + self.session.add(work) + self.session.commit() + + counters = ck_relogin.settle_fresh_results( + "batch-without-attempts", + now=now, + max_attempts=2, + expected_account_ids=[account.id], + ) + + checker = make_test_session(self.engine) + try: + settled_work = checker.get(AccountWorkItem, work.id) + settled_account = checker.get(Account, account.id) + assert counters["scanned"] == 1 + assert counters["retry"] == 1 + assert settled_work.status == "pending" + assert settled_work.due_at >= now + timedelta(minutes=59) + assert settled_account.relogin_next_at >= now + timedelta(minutes=59) + finally: + checker.close() + def test_settlement_marks_real_success_as_valid(self): """结算看到 success 就必须清零退避,不能写成失败。""" account = self.make_account( diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 766931e..fda4605 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent PROJECT_ROOT = TESTS_DIR.parent VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" -HEAD_REVISION = "20261122_0116" +HEAD_REVISION = "20261122_0117" # 迁移新增、但不声明在模型里的复合查询索引。 EXTRA_INDEXES = ( diff --git a/web/backend/migrations/migration_manifest.json b/web/backend/migrations/migration_manifest.json index 1e87138..c255ddf 100644 --- a/web/backend/migrations/migration_manifest.json +++ b/web/backend/migrations/migration_manifest.json @@ -465,6 +465,10 @@ "20261122_0116_enable_fulfillment_lease_reaper.py": { "revision": "20261122_0116", "sha256": "e756ffc9e953fc1b6167376eff830f1441194636626587808e2a2746a9b16fce" + }, + "20261122_0117_retire_account_overview_refresh.py": { + "revision": "20261122_0117", + "sha256": "d49c6ddabdee24fc4013e765e2305f7cf9cb4750fae914303501c3d0a1b0723d" } } } diff --git a/web/backend/migrations/versions/20261122_0117_retire_account_overview_refresh.py b/web/backend/migrations/versions/20261122_0117_retire_account_overview_refresh.py new file mode 100644 index 0000000..8b64b8a --- /dev/null +++ b/web/backend/migrations/versions/20261122_0117_retire_account_overview_refresh.py @@ -0,0 +1,39 @@ +"""retire obsolete account overview refresh job. + +Revision ID: 20261122_0117 +Revises: 20261122_0116 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + +revision: str = "20261122_0117" +down_revision: str | None = "20261122_0116" +branch_labels: str | None = None +depends_on: str | None = None + +JOB_KEY = "account_overview_refresh" + + +def upgrade() -> None: + if not sa.inspect(op.get_bind()).has_table("automation_jobs"): + return + op.execute( + sa.text( + """ + UPDATE automation_jobs + SET enabled = 0, + next_run_at = NULL, + last_message = '功能已淘汰:账号资料改由定向同步与履约补货维护', + updated_at = CURRENT_TIMESTAMP + WHERE `key` = :key + """ + ).bindparams(key=JOB_KEY) + ) + + +def downgrade() -> None: + """不自动恢复已淘汰作业。""" + return diff --git a/web/backend/routers/automation.py b/web/backend/routers/automation.py index abdbb03..6464340 100644 --- a/web/backend/routers/automation.py +++ b/web/backend/routers/automation.py @@ -124,10 +124,20 @@ def list_jobs( ): """列出作业配置、最近一次结果与调度器状态。""" ensure_jobs(db) - jobs = db.query(AutomationJob).order_by(AutomationJob.key.asc()).all() + active_job_keys = set(registered_jobs()) + jobs = ( + db.query(AutomationJob) + .filter(AutomationJob.key.in_(active_job_keys)) + .order_by(AutomationJob.key.asc()) + .all() + ) latest: dict[str, AutomationRun] = {} for run in ( - db.query(AutomationRun).order_by(AutomationRun.id.desc()).limit(200).all() + db.query(AutomationRun) + .filter(AutomationRun.job_key.in_(active_job_keys)) + .order_by(AutomationRun.id.desc()) + .limit(200) + .all() ): latest.setdefault(run.job_key, run) # 巡检与重登是两个作业,但通过数据库联动:巡检把行标成 invalid,重登作业的 @@ -390,6 +400,8 @@ def list_runs( query = db.query(AutomationRun) if job_key: query = query.filter(AutomationRun.job_key == job_key) + else: + query = query.filter(AutomationRun.job_key.in_(set(registered_jobs()))) runs = query.order_by(AutomationRun.id.desc()).limit(limit).all() return {"items": [_run_out(run) for run in runs]} diff --git a/web/backend/services/automation/__init__.py b/web/backend/services/automation/__init__.py index baa0287..cc6f843 100644 --- a/web/backend/services/automation/__init__.py +++ b/web/backend/services/automation/__init__.py @@ -12,12 +12,6 @@ from .jobs.account_capability_refresh import ( from .jobs.account_capability_refresh import ( run_account_capability_refresh, ) -from .jobs.account_overview_refresh import ( - JOB_KEY as ACCOUNT_OVERVIEW_REFRESH_KEY, -) -from .jobs.account_overview_refresh import ( - run_account_overview_refresh, -) from .jobs.account_supply_replenishment import ( JOB_KEY as ACCOUNT_SUPPLY_REPLENISHMENT_KEY, ) @@ -64,7 +58,6 @@ from .scheduler import ( __all__ = [ "ACCOUNT_CAPABILITY_REFRESH_KEY", - "ACCOUNT_OVERVIEW_REFRESH_KEY", "ACCOUNT_SUPPLY_REPLENISHMENT_KEY", "ACCOUNT_SYNC_QUEUE_KEY", "CK_HEALTH_KEY", @@ -80,7 +73,6 @@ __all__ = [ "registered_jobs", "reset_registry", "run_account_capability_refresh", - "run_account_overview_refresh", "run_account_supply_replenishment", "run_account_sync_queue_job", "run_ck_health", @@ -172,33 +164,6 @@ def register_default_jobs() -> None: run=run_ck_relogin, ) ) - register_job( - JobSpec( - key=ACCOUNT_OVERVIEW_REFRESH_KEY, - title="账号资料定时补全", - # 每 10 分钟一轮、每轮 400 个。 - # - # 依据是**试水实测**,不是拍脑袋:200 个并发 3 跑了 131 秒 - # = **1.53 个/秒 ≈ 5,500 个/小时**。瓶颈不在查询速度,而在调度频率。 - # 400 个 × 0.65 秒 ≈ **4.4 分钟**跑完,10 分钟一轮 - # = **2,400 个/小时**,7,784 个约 **3.2 小时**补完。 - # - # ⚠️ 一轮时长必须**明显小于间隔**:调度器是**串行**的 - # (见 scheduler.run_due_jobs 的注释:作业一个一个跑), - # 我的作业跑着的时候 CK 巡检/重登都得等着。所以宁可轮次多一些, - # 也不要一轮跑太久 —— 4.4 分钟 / 10 分钟是刻意留出的余量。 - # - # 想更快就调大 `batch_size`(账号列表里那套),但**跑得越快越容易被风控**, - # 而风控是所有账号一起受影响。 - # 「同一账号多久重检」= 查过的账号 7 天后再查(界面上的那个字段)。 - # ⚠️ 它**不参与调度** —— 调度频率由 run_interval_seconds 决定。 - interval_seconds=7 * 24 * 3600, - run_interval_seconds=5 * 60, - batch_size=400, - concurrency=3, - run=run_account_overview_refresh, - ) - ) register_job( JobSpec( key=CK_HEALTH_KEY, diff --git a/web/backend/services/automation/jobs/account_overview_refresh.py b/web/backend/services/automation/jobs/account_overview_refresh.py deleted file mode 100644 index 3f76184..0000000 --- a/web/backend/services/automation/jobs/account_overview_refresh.py +++ /dev/null @@ -1,265 +0,0 @@ -"""作业:**定时分批**补全账号基础资料(一键查询的内容)。 - -## 为什么不能一次性跑全量 - -生产有约 8,000 个「CK 有效但没查过」的账号。一次性发起等于 -**~8 万次斗鱼请求**,很容易触发风控 —— 而风控的代价是所有账号一起受影响。 - -所以做成**定时分批**:每轮只取 `batch_size` 个(默认 30), -跑完就结束,下一轮再来。慢一点没关系,**稳**最重要。 - -口径与「自动巡检」(`ck_health`)一致,并复用它的两道保护: -- `cookie_operable_filter()`:账号有 CK 且没有在途尝试 -- **错误率熔断**:一轮里失败比例过高(通常是风控或接口抖动)就 - **自己把自己关掉**,不再继续污染账号 - -## 两类账号分开 - -| 类型 | 条件 | 优先级 | -| --- | --- | --- | -| **没查过的** | `overview_queried_at IS NULL` | **优先**(补历史欠账)| -| 查过但过期了 | `overview_queried_at < now - interval_seconds` | 其次(周期性刷新)| - -同一档里面,已分配的排在未分配前面,最近登录成功(`cookie_updated_at`)的再靠前。 -价值分要先有查询结果才算得出来,所以新登录、已经分给客服的号先补。 - -用户明确要求这两类分开 —— 否则"周期性刷新老账号"会永远挤掉"从没查过的新账号"。 - -## 查询记录 - -每次都会建真实的 `douyu_tasks` 行(`query_account_overview`), -所以「哪个账号、什么时候、查出来什么」都有据可查; -账号上的 `overview_queried_at` 则是"下次该不该再查"的依据。 - -## 只查有效 CK - -`ck_check_status == 'valid'`。CK 失效的账号查了也是白查(只会多一条失败记录), -交给「CK 检测与重登」去处理。 -""" - -from __future__ import annotations - -from datetime import UTC, datetime, timedelta -from typing import Any - -from loguru import logger -from sqlalchemy import or_ -from sqlalchemy.orm import Session - -from ....models import Account, AutomationJob -from ...account_lifecycle import MANUAL_STATES -from ...account_value import high_value_needs_refresh_expr -from ...douyu_runner import DouyuBatchRunner -from ...douyu_service import cookie_operable_filter, create_douyu_planned_tasks - -JOB_KEY = "account_overview_refresh" - -#: `interval_seconds` 缺失时的兜底(天)—— 正常都会走界面上那个「同一账号多久重检」 -DEFAULT_RECHECK_SECONDS = 7 * 24 * 3600 - -#: 触发熔断的最小样本量:样本太少时比例没有意义(1 个失败就是 100%) -ERROR_PAUSE_MIN_SCANNED = 10 - -#: 失败比例超过这个值就熔断(与 ck_health 同口径) -ERROR_PAUSE_RATIO = 0.5 - - -def _positive_int(value: Any, default: int) -> int: - try: - parsed = int(value) - except (TypeError, ValueError): - return default - return parsed if parsed > 0 else default - - -def build_select_query(db: Session, job: AutomationJob, now: datetime): - """选出本轮要查的账号。 - - - 只要 **CK 有效**(`ck_check_status == 'valid'`)—— 失效的查了也是白查 - - **没查过的优先**:`overview_queried_at IS NULL` 排在最前 - (MySQL 没有 `NULLS FIRST`,用 `IS NULL` 的布尔值降序等价实现) - - 查过的只在超过 `interval_seconds` 后才重新入选 - (就是界面上那个「同一账号多久重检」—— 与其它作业**同一套口径**) - """ - # ⚠️ 用 `interval_seconds`(界面上那个「同一账号多久重检」), - # 与其它批量作业**同一套口径**。 - # - # 之前这里读的是 `job.config["refresh_days"]` —— 于是界面上那个字段 - # 对本作业是**死的**(改了没反应)。那正是用户刚批评过的 - # 「看起来能配、其实配的是不相干的东西」。 - batch = _positive_int(job.batch_size, 30) - return ( - _base_query(db, job, now) - # 没查过的排最前。同一档里:已分配的优先,再是最近登录成功的 - # (cookie_updated_at 越新越靠前;空的排后面)。 - # MySQL 没有 NULLS FIRST,用 IS NULL 的布尔值表达。 - .order_by( - Account.overview_queried_at.is_(None).desc(), - Account.assigned_to.isnot(None).desc(), - Account.cookie_updated_at.is_(None).asc(), - Account.cookie_updated_at.desc(), - Account.id.asc(), - ) - .limit(batch) - ) - - -def _base_query(db: Session, job: AutomationJob, now: datetime): - """**不带排序和 LIMIT** 的基础查询。 - - 单独抽出来是因为计数不能再走 `build_select_query`: - SQLAlchemy 不允许在已 `LIMIT` 的查询上再 `order_by(None)` - (抛 "order_by() being called on a Query which already has LIMIT")。 - """ - # ⚠️ 用 `interval_seconds`(界面上那个「同一账号多久重检」), - # 与其它批量作业**同一套口径**。 - # - # 之前这里读的是 `job.config["refresh_days"]` —— 于是界面上那个字段 - # 对本作业是**死的**(改了没反应)。那正是用户刚批评过的 - # 「看起来能配、其实配的是不相干的东西」。 - recheck_seconds = _positive_int(job.interval_seconds, DEFAULT_RECHECK_SECONDS) - cutoff = now - timedelta(seconds=recheck_seconds) - return db.query(Account).filter( - cookie_operable_filter(), - Account.auto_managed.is_(True), - Account.status.notin_(MANUAL_STATES), - Account.ck_check_status == "valid", - or_( - Account.overview_queried_at.is_(None), - Account.overview_queried_at < cutoff, - # 高价值结论有**自己独立的时钟**(`high_value_checked_at`): - # 一键查询里商品接口单独失败时,资料时间被刷新了、高价值时间却 - # 停在原地;CK 失效那段时间查不动也一样。少了这一条,这类账号 - # 的高价值状态会**永久**停在旧结论,没人再拉它。 - # - # 用的是**同一把尺子**(作业上那个「同一账号多久重检」), - # 不是另设一个窗口 —— 否则界面上那个字段对本作业就又变成半死的。 - high_value_needs_refresh_expr(now=now, max_seconds=recheck_seconds), - ), - ) - - -def count_remaining(db: Session, job: AutomationJob, now: datetime) -> int: - """还剩多少待查(只用于提示,不参与调度)。""" - return _base_query(db, job, now).count() - - -def _system_user_id(db: Session) -> int: - """任务行的 created_by —— 定时任务不是某个人的操作,记在超管名下。""" - from ....models import User - - user = ( - db.query(User) - .filter(User.role == "super_admin") - .order_by(User.id.asc()) - .first() - ) - if user is None: - raise RuntimeError("库中没有超管用户,无法为定时任务提供 created_by") - return int(user.id) - - -def run_refresh_for_accounts( - db: Session, - job: AutomationJob, - accounts: list[Account], - *, - now: datetime, - remaining_count: int | None = None, - source: str = "scheduled_refresh", - log_label: str = "overview", - empty_message: str = "全库暂无待补全或到期的账号", -) -> dict[str, Any]: - """对指定账号执行资料刷新;能力刷新作业也复用这条写入链。""" - if not accounts: - return { - "scanned": 0, - "valid": 0, - "invalid": 0, - "succeeded": 0, - "failed": 0, - "message": empty_message, - } - - account_ids = [a.id for a in accounts] - batch_id, count = create_douyu_planned_tasks( - db, - account_ids, - "query_account_overview", - "elite", - _system_user_id(db), - # 标上来源,便于在任务列表里区分「定时巡检」与「人工点查询」 - {"source": source}, - ) - if count == 0: - return { - "scanned": 0, - "valid": 0, - "invalid": 0, - "succeeded": 0, - "failed": 0, - "message": "候选账号均不可查(可能正有在途任务)", - } - - # 复用现有执行器:并发、代理、进度、失败处理都是现成的 - # ⚠️ 必须 close_db=False,避免 runner 结束时把调度线程的 session 关闭 - runner = DouyuBatchRunner( - db, - batch_id, - "query_account_overview", - concurrency=max(1, min(int(job.concurrency or 1), 10)), - close_db=False, - ) - runner.run() - db.commit() - - from ....models import DouyuTask - - rows = db.query(DouyuTask).filter(DouyuTask.batch_id == batch_id).all() - succeeded = sum(1 for t in rows if t.status == "success") - failed = sum(1 for t in rows if t.status == "failed") - - error_ratio = failed / len(rows) if rows else 0.0 - paused = False - if len(rows) >= ERROR_PAUSE_MIN_SCANNED and error_ratio > ERROR_PAUSE_RATIO: - # 大面积失败通常是风控或斗鱼接口抖动。继续跑只会把更多账号写坏, - # 所以**自己把自己关掉**,交给人来确认(与 ck_health 同口径)。 - job.enabled = False - db.commit() - paused = True - logger.warning( - f"[{log_label}] 失败率 {error_ratio:.0%} 过高,已自动关闭作业" - f"(batch={batch_id},failed={failed}/{len(rows)})—— 请确认是否被风控" - ) - - message = f"本轮查 {len(rows)} 个:成功 {succeeded} / 失败 {failed}" - if remaining_count is not None: - message += f"(剩 {remaining_count} 个待查)" - if paused: - # 把**可能的原因**写进消息里:运维看作业历史时才知道该去查什么, - # 否则只看到"失败率高"会一头雾水。 - message += ";失败率过高,已自动关闭作业(大面积失败通常是**被风控**或接口抖动,请先确认再重开)" - logger.info(f"[{log_label}] {message}") - return { - "scanned": len(rows), - "valid": succeeded, - "invalid": failed, - "succeeded": succeeded, - "failed": failed, - "batch_id": batch_id, - "paused": paused, - "message": message, - } - - -def run_account_overview_refresh(db: Session, job: AutomationJob) -> dict[str, Any]: - """本轮:选一批 → 建任务 → 就地跑完 → 看失败率决定要不要熔断。""" - now = datetime.now(UTC) - accounts = build_select_query(db, job, now).all() - return run_refresh_for_accounts( - db, - job, - accounts, - now=now, - remaining_count=count_remaining(db, job, now), - ) diff --git a/web/backend/services/automation/jobs/ck_relogin.py b/web/backend/services/automation/jobs/ck_relogin.py index bbbcac8..a9cf97d 100644 --- a/web/backend/services/automation/jobs/ck_relogin.py +++ b/web/backend/services/automation/jobs/ck_relogin.py @@ -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": diff --git a/web/frontend/src/layouts/MainLayout.tsx b/web/frontend/src/layouts/MainLayout.tsx index f0faf86..9380ab9 100644 --- a/web/frontend/src/layouts/MainLayout.tsx +++ b/web/frontend/src/layouts/MainLayout.tsx @@ -78,7 +78,7 @@ export default function MainLayout({ onLogout }: { onLogout?: () => void }) { douyuItems.push({ key: '/cookie-operations', label: 'CK 检测与重登', icon: }); } if (can('automation:view')) { - douyuItems.push({ key: '/automation', label: '自动巡检', icon: }); + douyuItems.push({ key: '/automation', label: '斗鱼自动化', icon: }); } if (can('douyu:task')) { douyuItems.push({ key: '/douyu/elite', label: '精英宝典', icon: }); diff --git a/web/frontend/src/pages/AccountCheckPage.tsx b/web/frontend/src/pages/AccountCheckPage.tsx index 2a0112e..70616fb 100644 --- a/web/frontend/src/pages/AccountCheckPage.tsx +++ b/web/frontend/src/pages/AccountCheckPage.tsx @@ -9,7 +9,6 @@ import { DownloadOutlined, PlayCircleOutlined, ReloadOutlined, StopOutlined } fr import { accountApi, accountCheckApi, type AccountCheckBatch, type AccountCheckItem } from '../api/modules'; import { formatTime } from '../utils/time'; import { getErrorMessage } from '../utils/error'; -import AccountPlatformStatusPanel from './douyu-tasks/AccountPlatformStatusPanel'; const { TextArea } = Input; const { Text, Title } = Typography; @@ -341,8 +340,6 @@ export default function AccountCheckPage() { 账号检测 - - {/* 两种来源并存:手动粘贴是旧逻辑,一条没动; diff --git a/web/frontend/src/pages/AutomationPage.tsx b/web/frontend/src/pages/AutomationPage.tsx index fd18800..b5e5392 100644 --- a/web/frontend/src/pages/AutomationPage.tsx +++ b/web/frontend/src/pages/AutomationPage.tsx @@ -1,16 +1,21 @@ import type { ReactNode } from "react"; -import { useCallback, useEffect, useState } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { Alert, Button, Card, + Collapse, + Col, Descriptions, Form, InputNumber, Popconfirm, + Row, + Select, Space, Switch, Table, + Tabs, Tag, Typography, } from "antd"; @@ -18,6 +23,7 @@ import type { ColumnsType } from "antd/es/table"; import { CheckCircleOutlined, ReloadOutlined, + SaveOutlined, ThunderboltOutlined, } from "@ant-design/icons"; import { message } from "../utils/antdMessage"; @@ -25,6 +31,7 @@ import { getErrorMessage } from "../utils/error"; import { automationApi, type AutomationJobItem, + type AutomationLoginQuota, type AutomationLoginQuotaRecommendation, type AutomationManualGroup, type AutomationManualSample, @@ -32,52 +39,154 @@ import { } from "../api/modules"; import { usePermissions } from "../hooks/usePermissions"; import { formatTime } from "../utils/time"; +import AccountPlatformStatusPanel from "./douyu-tasks/AccountPlatformStatusPanel"; + +const { Text, Title } = Typography; const STATUS_TAG: Record = { success: { color: "green", text: "成功" }, failed: { color: "red", text: "失败" }, - // 被部署/重启打断:不是作业本身的失败(CK 没丢,会自动重试), - // 用中性色,避免和真失败混在一起。 interrupted: { color: "default", text: "被中断" }, running: { color: "blue", text: "执行中" }, skipped: { color: "default", text: "已跳过" }, }; const BUDGET_LABELS: Record = { - daily_attempt_limit: "每日尝试", - hourly_attempt_limit: "每小时尝试", - per_account_daily_limit: "单账号每日", + daily_attempt_limit: "全局每日尝试", + hourly_attempt_limit: "全局每小时尝试", + per_account_daily_limit: "单账号每日尝试", onboarding_daily_limit: "首次登录", - relogin_high_value_daily_limit: "高价值重登", - relogin_ordinary_daily_limit: "普通重登", - manual_daily_limit: "人工保留", + relogin_high_value_daily_limit: "高价值账号重登", + relogin_ordinary_daily_limit: "普通账号重登", + manual_daily_limit: "人工登录保留", login_concurrency: "登录并发", + failure_pause_ratio: "失败率熔断", +}; + +type BudgetFieldName = + | "daily_attempt_limit" + | "hourly_attempt_limit" + | "per_account_daily_limit" + | "onboarding_daily_limit" + | "relogin_high_value_daily_limit" + | "relogin_ordinary_daily_limit" + | "manual_daily_limit" + | "login_concurrency" + | "failure_pause_ratio"; + +const BUDGET_FIELDS: Array<{ + name: BudgetFieldName; + label: string; + min: number; + max: number; + suffix?: string; +}> = [ + { name: "daily_attempt_limit", label: "全局每日尝试", min: 1, max: 1_000_000 }, + { name: "hourly_attempt_limit", label: "全局每小时尝试", min: 1, max: 100_000 }, + { name: "per_account_daily_limit", label: "单账号每日尝试", min: 1, max: 100 }, + { name: "onboarding_daily_limit", label: "首次登录预算", min: 0, max: 100_000 }, + { name: "relogin_high_value_daily_limit", label: "高价值重登预算", min: 0, max: 100_000 }, + { name: "relogin_ordinary_daily_limit", label: "普通重登预算", min: 0, max: 100_000 }, + { name: "manual_daily_limit", label: "人工登录保留", min: 0, max: 100_000 }, + { name: "login_concurrency", label: "登录并发", min: 1, max: 10 }, + { name: "failure_pause_ratio", label: "失败率熔断阈值", min: 0, max: 100, suffix: "%" }, +]; + +interface JobUiSpec { + description: string; + core: boolean; + showRecheck: boolean; + showConcurrency: boolean; +} + +const JOB_UI: Record = { + ck_health: { + description: "只读取现有 CK 判断登录状态;发现失效后交给自动重登,不替换 Cookie。", + core: true, + showRecheck: true, + showConcurrency: true, + }, + ck_relogin: { + description: "对巡检标记为失效的账号执行真实登录,消耗登录预算、邮箱验证码和代理资源。", + core: true, + showRecheck: true, + showConcurrency: true, + }, + account_sync_queue: { + description: "独立消费账号目录、积分、鱼翅和角色同步工作,避免阻塞订单选号。", + core: true, + showRecheck: false, + showConcurrency: true, + }, + account_capability_refresh: { + description: "按待履约商品补充全量账号能力目录,只做本地筛选,外部查询交给同步队列。", + core: true, + showRecheck: false, + showConcurrency: true, + }, + account_supply_replenishment: { + description: "根据高价值和普通账号的覆盖、可兑换、可分配缺口补充候选账号。", + core: true, + showRecheck: false, + showConcurrency: false, + }, + platform_fulfillment_lease_reaper: { + description: "回收过期履约租约和扫码超时任务占用的账号,防止账号池被永久锁死。", + core: true, + showRecheck: false, + showConcurrency: false, + }, + points_expire_check: { + description: "记录积分到期事件,属于低频事实补记,不参与账号供给调度。", + core: false, + showRecheck: false, + showConcurrency: false, + }, + douyu_tasks_retention: { + description: "分批清理过期任务日志,属于低频维护作业。", + core: false, + showRecheck: false, + showConcurrency: false, + }, +}; + +const DEFAULT_JOB_UI: JobUiSpec = { + description: "斗鱼自动化后台作业。", + core: false, + showRecheck: false, + showConcurrency: false, }; function nextRunLabel(job: AutomationJobItem): ReactNode { if (!job.enabled) { - return 未启用; + return 未启用; } if (!job.next_run_at) { - return -; + return 等待调度; } const next = new Date(job.next_run_at).getTime(); if (!Number.isFinite(next)) return formatTime(job.next_run_at); if (next > Date.now()) return formatTime(job.next_run_at); - return ( - - 排队中{" "} - - ({formatTime(job.next_run_at)} 到期) - - - ); + return 已到点,等待调度线程; } function secondsToHours(seconds: number): number { return Math.round((seconds / 3600) * 10) / 10; } +function formatDuration(start: string | null, end: string | null): string { + if (!start || !end) return "-"; + const ms = new Date(end).getTime() - new Date(start).getTime(); + if (!Number.isFinite(ms) || ms < 0) return "-"; + if (ms < 1000) return `${ms}ms`; + return `${(ms / 1000).toFixed(1)}s`; +} + +function jobStatusTag(status: string) { + const tag = STATUS_TAG[status] ?? { color: "default", text: status || "-" }; + return {tag.text}; +} + export default function AutomationPage() { const [jobs, setJobs] = useState([]); const [schedulerEnabled, setSchedulerEnabled] = useState(true); @@ -86,41 +195,54 @@ export default function AutomationPage() { const [manualGroups, setManualGroups] = useState([]); const [manualTotal, setManualTotal] = useState(0); const [pendingRelogin, setPendingRelogin] = useState(0); + const [loginQuota, setLoginQuota] = useState(null); const [budgetRecommendation, setBudgetRecommendation] = useState(null); const [loading, setLoading] = useState(false); const [applyingBudget, setApplyingBudget] = useState(false); + const [savingBudget, setSavingBudget] = useState(false); const [savingKey, setSavingKey] = useState(null); const [runningKey, setRunningKey] = useState(null); - const [forms] = Form.useForm(); + const [historyJobFilter, setHistoryJobFilter] = useState(""); + const [historyStatusFilter, setHistoryStatusFilter] = useState(""); + const [jobForms] = Form.useForm(); + const [budgetForm] = Form.useForm(); const { can } = usePermissions(); const canManage = can("automation:manage"); + const jobTitles = useMemo( + () => Object.fromEntries(jobs.map((job) => [job.key, job.title])), + [jobs], + ); + const load = useCallback(async () => { setLoading(true); try { - const [data, recommendation] = await Promise.all([ + const [data, recommendation, quota] = await Promise.all([ automationApi.jobs(), automationApi.loginQuotaRecommendation(), + automationApi.loginQuota(), ]); setJobs(data.jobs); setBudgetRecommendation(recommendation); + setLoginQuota(quota); setPendingRelogin(data.pending_relogin ?? 0); setSchedulerEnabled(data.scheduler.enabled); setSchedulerRunning(data.scheduler.running); - const history = await automationApi.runs({ limit: 50 }); + budgetForm.setFieldsValue(quota); + const [history, manual] = await Promise.all([ + automationApi.runs({ limit: 80 }), + automationApi.manualQueue(), + ]); setRuns(history.items); - const manual = await automationApi.manualQueue(); setManualGroups(manual.groups); setManualTotal(manual.total); - // 统一回填表单:initialValue 只在首次渲染生效,刷新后不会跟着变。 - forms.setFieldsValue( + jobForms.setFieldsValue( Object.fromEntries( data.jobs.map((job) => [ job.key, { enabled: job.enabled, - // 「多久跑一次」与「同一行多久重检」是两件事,分开两个输入框 run_interval_minutes: Math.max( Math.round((job.run_interval_seconds ?? 300) / 60), 1, @@ -132,23 +254,23 @@ export default function AutomationPage() { ]), ), ); - } catch (e: unknown) { - message.error(getErrorMessage(e)); + } catch (error: unknown) { + message.error(getErrorMessage(error)); } finally { setLoading(false); } - }, [forms]); + }, [budgetForm, jobForms]); useEffect(() => { - load(); + void load(); }, [load]); const saveJob = async (job: AutomationJobItem) => { - const values = forms.getFieldValue(job.key) || {}; + const values = jobForms.getFieldValue(job.key) || {}; setSavingKey(job.key); try { await automationApi.updateJob(job.key, { - enabled: values.enabled, + enabled: Boolean(values.enabled), run_interval_seconds: Math.max( Math.round((values.run_interval_minutes ?? 5) * 60), 30, @@ -162,8 +284,8 @@ export default function AutomationPage() { }); message.success(`${job.title} 配置已保存`); await load(); - } catch (e: unknown) { - message.error(getErrorMessage(e)); + } catch (error: unknown) { + message.error(getErrorMessage(error)); } finally { setSavingKey(null); } @@ -174,16 +296,34 @@ export default function AutomationPage() { try { const outcome = await automationApi.runJob(job.key); message.success( - `跑完一轮:扫描 ${outcome.scanned} 条,有效 ${outcome.valid},失效 ${outcome.invalid}`, + `${job.title}已执行:扫描 ${outcome.scanned},成功 ${outcome.valid},失败 ${outcome.invalid}`, ); await load(); - } catch (e: unknown) { - message.error(getErrorMessage(e)); + } catch (error: unknown) { + message.error(getErrorMessage(error)); } finally { setRunningKey(null); } }; + const saveBudget = async () => { + const values = await budgetForm.validateFields(); + setSavingBudget(true); + try { + await automationApi.updateLoginQuota({ + ...(loginQuota || {}), + ...values, + enabled: Boolean(values.enabled), + } as AutomationLoginQuota); + message.success("登录预算已保存"); + await load(); + } catch (error: unknown) { + message.error(getErrorMessage(error)); + } finally { + setSavingBudget(false); + } + }; + const applyBudgetRecommendation = async () => { if (!budgetRecommendation) return; setApplyingBudget(true); @@ -192,85 +332,74 @@ export default function AutomationPage() { ...budgetRecommendation.current, ...budgetRecommendation.recommended, }); - message.success("登录预算已按建议值更新"); + message.success("已应用预算建议"); await load(); - } catch (e: unknown) { - message.error(getErrorMessage(e)); + } catch (error: unknown) { + message.error(getErrorMessage(error)); } finally { setApplyingBudget(false); } }; const manualColumns: ColumnsType = [ - { title: "账号ID", dataIndex: "account_id", width: 80 }, - { - title: "账号", - dataIndex: "account_username", - width: 160, - ellipsis: true, - }, - { title: "已重试", dataIndex: "relogin_attempts", width: 80 }, - { - title: "失败原因", - dataIndex: "last_error", - ellipsis: true, - render: (value: string) => value || "-", - }, + { title: "账号 ID", dataIndex: "account_id", width: 90 }, + { title: "账号", dataIndex: "account_username", width: 180, ellipsis: true }, + { title: "已尝试", dataIndex: "relogin_attempts", width: 90 }, + { title: "失败原因", dataIndex: "last_error", ellipsis: true }, { title: "最近尝试", dataIndex: "attempt_at", - width: 160, + width: 170, render: (value: string | null) => formatTime(value), }, ]; + const filteredRuns = useMemo( + () => + runs.filter( + (run) => + (!historyJobFilter || run.job_key === historyJobFilter) && + (!historyStatusFilter || run.status === historyStatusFilter), + ), + [historyJobFilter, historyStatusFilter, runs], + ); + const runColumns: ColumnsType = [ { title: "开始时间", dataIndex: "started_at", - width: 180, + width: 170, render: (value: string | null) => formatTime(value), }, { title: "作业", dataIndex: "job_key", - width: 120, + width: 170, + render: (value: string) => jobTitles[value] || value, }, { title: "结果", dataIndex: "status", width: 90, - render: (value: string) => { - const tag = STATUS_TAG[value] ?? { - color: "default", - text: value || "-", - }; - return {tag.text}; - }, + render: (value: string) => jobStatusTag(value), }, - { title: "扫描", dataIndex: "scanned", width: 80 }, - { title: "有效", dataIndex: "valid", width: 80 }, { - title: "失效", - dataIndex: "invalid", - width: 80, - render: (value: number) => - value > 0 ? ( - {value} - ) : ( - 0 - ), + title: "处理量", + width: 210, + render: (_, row) => { + const parts = []; + if (row.scanned) parts.push(`扫描 ${row.scanned}`); + if (row.valid) parts.push(`成功 ${row.valid}`); + if (row.invalid) parts.push(`失败 ${row.invalid}`); + if (row.skipped) parts.push(`跳过 ${row.skipped}`); + return parts.join(" / ") || "-"; + }, }, { title: "耗时", width: 90, render: (_, row) => - // 没实际扫描过的轮次没有真实耗时:被中断的记录 finished_at 是重启后 - // 补上的(见 cleanup_orphan_runs),差值里混入了停机时间,是个假数字; - // 一轮就报错的(如 SQL 语法错误)同理。 - row.scanned > 0 && row.finished_at - ? formatDuration(row.started_at, row.finished_at) - : "-", + row.scanned > 0 ? formatDuration(row.started_at, row.finished_at) : "-", }, { title: "说明", dataIndex: "message", ellipsis: true }, ]; @@ -293,221 +422,245 @@ export default function AutomationPage() { })) : []; - return ( - + const renderJob = (job: AutomationJobItem) => { + const spec = JOB_UI[job.key] || DEFAULT_JOB_UI; + return ( } onClick={load} loading={loading}> - 刷新 + + {job.key === "ck_relogin" ? ( + + 待重登 {pendingRelogin} + + ) : null} + {jobStatusTag(job.last_status)} + + } + style={{ marginBottom: 16 }} + > + {spec.description} + + + {formatTime(job.last_run_at)} + + {nextRunLabel(job)} + + {job.last_message || "-"} + + +
+ + + + + + + {spec.showRecheck ? ( + + + + ) : null} + + + + {spec.showConcurrency ? ( + + + + ) : null} + + + void runOnce(job)} + disabled={!canManage} + > + + + +
+
+ ); + }; + + const coreJobs = jobs.filter((job) => (JOB_UI[job.key] || DEFAULT_JOB_UI).core); + const maintenanceJobs = jobs.filter( + (job) => !(JOB_UI[job.key] || DEFAULT_JOB_UI).core, + ); + + const jobsTab = ( + + {!schedulerEnabled ? ( + + ) : null} + {schedulerEnabled && !schedulerRunning ? ( + + ) : null} + +
+ 核心作业 + {coreJobs.map(renderJob)} + {maintenanceJobs.length ? ( + + ) : null} + + {!canManage ? ( + 当前账号只能查看配置,修改需要 automation:manage 权限。 + ) : null} +
+ ); + + const budgetTab = ( + + } + loading={savingBudget} + disabled={!canManage} + onClick={() => void saveBudget()} + > + 保存预算 } > - {!schedulerEnabled && ( - - )} - {schedulerEnabled && !schedulerRunning && ( - - )} - - -
- - {jobs.map((job) => ( - - 上游:巡检标记为失效且有 CK 的账号,当前待重登{" "} - {pendingRelogin} 个 - - ) : job.key === "ck_health" ? ( - - 下游:CK 自动重登(发现失效后自动接手) - - ) : null - } - style={{ width: "100%" }} - > - - - {formatTime(job.last_run_at)} - - - {nextRunLabel(job)} - - - {job.last_status ? ( - - {STATUS_TAG[job.last_status]?.text ?? job.last_status} - - ) : ( - "-" - )} - - - {job.last_message || "-"} - - - - {/* 字段行:使用 flex 容器水平对齐,底端对齐,自动折行 */} -
- - - - - - - - - - - - - - - - - - runOnce(job)} - disabled={!canManage} - > - - - -
- {!canManage && ( -
- - 当前账号只能查看,修改配置需要 automation:manage 权限。 - -
- )} -
+ + + + + + + + {BUDGET_FIELDS.map((field) => ( + + + + + ))} -
+
+ {loginQuota?.updated_at ? ( + 最近更新:{formatTime(loginQuota.updated_at)} + ) : null}
void applyBudgetRecommendation()} + - + 应用建议 + } > {budgetRecommendation ? ( <> - - - {budgetRecommendation.basis.valid_accounts} - - - {budgetRecommendation.basis.invalid_backlog} - - - {budgetRecommendation.basis.imported_backlog} - - - {budgetRecommendation.basis.recent_sample} - - - {budgetRecommendation.basis.recent_success_rate.toFixed(1)}% - - - {budgetRecommendation.basis.required_daily_success} - - - {budgetRecommendation.basis.recent_sample < 20 ? ( - - ) : null} + + + 有效账号 +
{budgetRecommendation.basis.valid_accounts}
+ + + 失效积压 +
{budgetRecommendation.basis.invalid_backlog}
+ + + 未登录积压 +
{budgetRecommendation.basis.imported_backlog}
+ + + 近期成功率 +
{budgetRecommendation.basis.recent_success_rate.toFixed(1)}%
+ + + 每日成功需求 +
{budgetRecommendation.basis.required_daily_success}
+ +
( - 0 ? "success" : value < 0 ? "danger" : "secondary"} - > + 0 ? "success" : value < 0 ? "danger" : "secondary"}> {value > 0 ? `+${value}` : value} - + ), }, ]} /> ) : ( - 暂无预算建议 + 暂无预算建议 )} - - - 自动重登救不了的类型(共 {manualTotal} 个) - - } - > - {manualGroups.length === 0 ? ( - - 暂无 —— 能自动重试的都会自己重试,需要人工的才出现在这里。 - - ) : ( - manualGroups.map((group) => ( - -
- - )) - )} - - - -
- ); -} -function formatDuration(start: string | null, end: string | null): string { - if (!start || !end) return "-"; - const ms = new Date(end).getTime() - new Date(start).getTime(); - if (!Number.isFinite(ms) || ms < 0) return "-"; - if (ms < 1000) return `${ms}ms`; - return `${(ms / 1000).toFixed(1)}s`; + const manualTab = ( + +
+ + 待人工处理({manualTotal}) + + 自动重登无法处理的账号,按失败类型分组。 +
+ {manualGroups.length === 0 ? ( + 暂无需要人工处理的账号。 + ) : ( + manualGroups.map((group) => ( + +
+ + )) + )} + + ); + + const historyTab = ( + + setHistoryStatusFilter(value || "")} + options={Object.entries(STATUS_TAG).map(([value, item]) => ({ + value, + label: item.text, + }))} + /> + + } + > +
+ + ); + + return ( + +
+
+ + 斗鱼自动化中心 + + + 统一管理 CK 巡检、自动重登、账号目录、供给补货、履约租约、登录预算和运行趋势。 + +
+ + + {schedulerEnabled + ? schedulerRunning + ? "调度运行中" + : "调度线程未运行" + : "自动化总开关关闭"} + + + +
+ + , + }, + { key: "jobs", label: "作业配置", children: jobsTab }, + { key: "budget", label: "登录预算", children: budgetTab }, + { + key: "manual", + label: `待人工${manualTotal ? ` (${manualTotal})` : ""}`, + children: manualTab, + }, + { key: "history", label: "运行记录", children: historyTab }, + ]} + /> +
+ ); } diff --git a/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx b/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx index 97acfd6..95a90ea 100644 --- a/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx +++ b/web/frontend/src/pages/douyu-tasks/AccountPlatformStatusPanel.tsx @@ -4,6 +4,7 @@ import { Button, Card, Col, + Collapse, Empty, Form, Input, @@ -80,6 +81,76 @@ interface TrendRow { reloginOk: number; } +function MiniTrend({ + title, + values, + dates, + color, + suffix = '', +}: { + title: string; + values: number[]; + dates: string[]; + color: string; + suffix?: string; +}) { + const width = 260; + const height = 64; + const padding = 5; + const max = Math.max(...values, 1); + const step = values.length > 1 ? (width - padding * 2) / (values.length - 1) : 0; + const points = values.map((value, index) => { + const x = padding + step * index; + const y = height - padding - (Math.max(0, value) / max) * (height - padding * 2); + return `${x},${y}`; + }).join(' '); + const latest = values.at(-1) || 0; + + return ( +
+ + {title} + + 最近 {latest}{suffix} + + + + + {points ? ( + + ) : null} + +
+ {dates[0]?.slice(5) || '-'} + {dates.at(-1)?.slice(5) || '-'} +
+
+ ); +} + export default function AccountPlatformStatusPanel() { const { can } = usePermissions(); const canManage = can('douyu:config'); @@ -606,16 +677,66 @@ export default function AccountPlatformStatusPanel() { }} /> - - {`近 ${trends?.days || 7} 天趋势`} -
+
+ {`近 ${trends?.days || 7} 天运行趋势`} + + 查询、登录、CK 失效和重登结果 + +
+ + + row.queryTotal)} + dates={trendRows.map((row) => row.date)} + color="#1677ff" + /> + + + row.loginTotal)} + dates={trendRows.map((row) => row.date)} + color="#13c2c2" + /> + + + row.ckInvalid)} + dates={trendRows.map((row) => row.date)} + color="#ff4d4f" + /> + + + row.reloginOk)} + dates={trendRows.map((row) => row.date)} + color="#52c41a" + /> + + + + ), + }, + ]} /> diff --git a/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx b/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx index a8f4674..bcdae55 100644 --- a/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx +++ b/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx @@ -171,6 +171,21 @@ function isLoopbackPaymentUrl(value: string): boolean { return /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(:|\/|$)/i.test(value); } +async function waitForBatchCompletion( + handbook: HandbookKind, + batchId: string, + expectedCount: number, +): Promise { + const deadline = Date.now() + 180_000; + while (true) { + const batchTasks = await douyuApi.listTasks(handbook, batchId); + const allFinished = batchTasks.length >= expectedCount + && batchTasks.every((task) => !['planned', 'pending', 'running'].includes(task.status)); + if (allFinished || Date.now() >= deadline) return batchTasks; + await new Promise((resolve) => window.setTimeout(resolve, 800)); + } +} + /** 绑定状态小标签:取值 unknown / unbound / pending / bound。 */ function BindStatusTag({ status, label }: { status?: string; label?: string }) { if (!status) return null; @@ -1100,15 +1115,11 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) const created = await startTask('query_elite_status', targetIds); if (!created) return; - const deadline = Date.now() + 180_000; - let batchTasks: DouyuTaskItem[] = []; - while (true) { - batchTasks = await douyuApi.listTasks(handbook, created.batch_id); - const allFinished = batchTasks.length >= created.count - && batchTasks.every((task) => !['planned', 'pending', 'running'].includes(task.status)); - if (allFinished || Date.now() >= deadline) break; - await new Promise((resolve) => window.setTimeout(resolve, 800)); - } + const batchTasks = await waitForBatchCompletion( + handbook, + created.batch_id, + created.count, + ); const unopenedIds = new Set( batchTasks