From a9506b77d1caf20555be6a15cb73eb53eb4b150a Mon Sep 17 00:00:00 2001 From: yml Date: Sat, 28 Nov 2026 21:51:22 +0800 Subject: [PATCH] =?UTF-8?q?perf(account):=20=E6=8F=90=E5=8D=87=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=E8=A1=A5=E6=89=AB=E4=B8=8E=E7=99=BB=E5=BD=95=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E5=90=9E=E5=90=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 2 +- README.md | 2 +- docker-compose.yml | 2 +- pyproject.toml | 2 +- tests/test_account_protection_service.py | 14 +++-- tests/test_automation_registry_guard.py | 2 +- tests/test_migrations.py | 2 +- uv.lock | 2 +- .../migrations/migration_manifest.json | 4 ++ ...261128_0139_scale_account_recovery_jobs.py | 62 +++++++++++++++++++ web/backend/services/automation/__init__.py | 4 +- .../jobs/account_capability_refresh.py | 2 +- web/backend/services/login_quota_service.py | 14 +++-- 13 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 web/backend/migrations/versions/20261128_0139_scale_account_recovery_jobs.py diff --git a/.env.example b/.env.example index a435f93..4bf4763 100644 --- a/.env.example +++ b/.env.example @@ -129,7 +129,7 @@ TASK_LANE_BINDING_POLL_WORKERS=6 TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=6 TASK_LANE_LOGIN_WORKERS=4 TASK_LANE_ACCOUNT_CHECK_WORKERS=4 -TASK_LANE_ACCOUNT_SYNC_WORKERS=3 +TASK_LANE_ACCOUNT_SYNC_WORKERS=10 # 账号同步队列待处理水位。通用供给只能占低水位,剩余预留给有明确订单的补货。 ACCOUNT_SYNC_QUEUE_TARGET_PENDING=60 ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING=20 diff --git a/README.md b/README.md index 9f7bc68..7e24ed8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 直播账号运营后台 -斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.13)。 +斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.14)。 ## 能做什么 diff --git a/docker-compose.yml b/docker-compose.yml index 2853d96..8e4271b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -59,7 +59,7 @@ services: - TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=${TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT:-12} - TASK_LANE_LOGIN_WORKERS=${TASK_LANE_LOGIN_WORKERS:-8} - TASK_LANE_ACCOUNT_CHECK_WORKERS=${TASK_LANE_ACCOUNT_CHECK_WORKERS:-8} - - TASK_LANE_ACCOUNT_SYNC_WORKERS=${TASK_LANE_ACCOUNT_SYNC_WORKERS:-6} + - TASK_LANE_ACCOUNT_SYNC_WORKERS=${TASK_LANE_ACCOUNT_SYNC_WORKERS:-10} - TASK_LANE_OVERVIEW_QUERY_WORKERS=${TASK_LANE_OVERVIEW_QUERY_WORKERS:-10} - TASK_LANE_QUERY_BATCH_WORKERS=${TASK_LANE_QUERY_BATCH_WORKERS:-10} - TASK_LANE_PAYMENT_QR_WORKERS=${TASK_LANE_PAYMENT_QR_WORKERS:-12} diff --git a/pyproject.toml b/pyproject.toml index dd97cd4..6823584 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "live-hub-py" -version = "0.4.13" +version = "0.4.14" description = "直播账号运营 Web 后台" readme = "README.md" requires-python = ">=3.12,<3.13" diff --git a/tests/test_account_protection_service.py b/tests/test_account_protection_service.py index faf1972..01c8b32 100644 --- a/tests/test_account_protection_service.py +++ b/tests/test_account_protection_service.py @@ -48,14 +48,18 @@ def _account( ) -def _presale_task(account: Account, *, execution_status: str = "bound") -> PlatformFulfillmentTask: +def _presale_task( + account: Account, *, execution_status: str = "bound" +) -> PlatformFulfillmentTask: return PlatformFulfillmentTask( source_order_id=f"order-{account.id or account.username}", source_order_item_id=f"item-{account.id or account.username}", sale_mode="presale", account_id=account.id, binding_status=( - "bound" if execution_status not in {"completed", "cancelled"} else "not_started" + "bound" + if execution_status not in {"completed", "cancelled"} + else "not_started" ), execution_status=execution_status, ) @@ -207,9 +211,7 @@ def test_protection_target_reports_ck_health_and_shortage(db): assert presale["ck_invalid"] == 1 assert presale["target_shortage"] == 2 assert presale["needs_attention"] is True - assert { - alert["code"] for alert in status["alerts"] - } >= { + assert {alert["code"] for alert in status["alerts"]} >= { "account_protection_ck_affected", "account_protection_target_shortage", } @@ -563,7 +565,7 @@ def test_cascading_priority_quota_reservation_p0_p1_p2_p3(db): ck_status="valid", checked_at=NOW, ) - p1.cookie_updated_at = NOW - timedelta(days=6, hours=20) + p1.cookie_updated_at = NOW - timedelta(days=6, hours=23) p2 = _account("tier-p2", ck_status="invalid", points=500) p3 = _account("tier-p3", ck_status="invalid", points=0) db.add_all([p0, p1, p2, p3]) diff --git a/tests/test_automation_registry_guard.py b/tests/test_automation_registry_guard.py index 06ffd2d..9052713 100644 --- a/tests/test_automation_registry_guard.py +++ b/tests/test_automation_registry_guard.py @@ -31,7 +31,7 @@ def test_list_jobs_registers_default_jobs_when_api_process_registry_is_empty(db) keys = set(jobs) assert "account_sync_queue" in keys assert "account_supply_replenishment" in keys - assert jobs["account_sync_queue"]["batch_size"] == 40 + assert jobs["account_sync_queue"]["batch_size"] == 60 assert result["scheduler"]["enabled"] is not None diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 71fabf3..db8dbe4 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -45,7 +45,7 @@ TESTS_DIR = Path(__file__).resolve().parent PROJECT_ROOT = TESTS_DIR.parent VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" -HEAD_REVISION = "20261128_0138" +HEAD_REVISION = "20261128_0139" # 迁移新增、但不声明在模型里的复合查询索引。 EXTRA_INDEXES = ( diff --git a/uv.lock b/uv.lock index baf1ec5..c341216 100644 --- a/uv.lock +++ b/uv.lock @@ -381,7 +381,7 @@ wheels = [ [[package]] name = "live-hub-py" -version = "0.4.13" +version = "0.4.14" source = { editable = "." } dependencies = [ { name = "alembic" }, diff --git a/web/backend/migrations/migration_manifest.json b/web/backend/migrations/migration_manifest.json index 5238200..6d09766 100644 --- a/web/backend/migrations/migration_manifest.json +++ b/web/backend/migrations/migration_manifest.json @@ -553,6 +553,10 @@ "20261128_0138_account_sync_backfill_cadence.py": { "revision": "20261128_0138", "sha256": "12b7cc588be5da8009e8c7090087ed4ad1d5a3723b2fe8e97584208550feced4" + }, + "20261128_0139_scale_account_recovery_jobs.py": { + "revision": "20261128_0139", + "sha256": "42d1bb6dc3e22738a2f69054089610d42f9f0e06b280221d2554365c704a610f" } } } diff --git a/web/backend/migrations/versions/20261128_0139_scale_account_recovery_jobs.py b/web/backend/migrations/versions/20261128_0139_scale_account_recovery_jobs.py new file mode 100644 index 0000000..7d8e78a --- /dev/null +++ b/web/backend/migrations/versions/20261128_0139_scale_account_recovery_jobs.py @@ -0,0 +1,62 @@ +"""scale account recovery jobs and capability refresh throughput + +Revision ID: 20261128_0139 +Revises: 20261128_0138 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + +revision: str = "20261128_0139" +down_revision: str | None = "20261128_0138" +branch_labels: str | None = None +depends_on: str | None = None + + +def upgrade() -> None: + if not sa.inspect(op.get_bind()).has_table("automation_jobs"): + return + op.execute( + sa.text( + """ + UPDATE automation_jobs + SET batch_size = 60, + updated_at = CURRENT_TIMESTAMP + WHERE `key` = 'account_sync_queue' + AND batch_size = 40 + """ + ) + ) + op.execute( + sa.text( + """ + UPDATE automation_jobs + SET batch_size = 20, + run_interval_seconds = 300, + updated_at = CURRENT_TIMESTAMP + WHERE `key` = 'ck_relogin' + AND batch_size = 10 + AND run_interval_seconds = 600 + """ + ) + ) + op.execute( + sa.text( + """ + UPDATE automation_jobs + SET batch_size = 30, + run_interval_seconds = 300, + updated_at = CURRENT_TIMESTAMP + WHERE `key` = 'account_onboarding' + AND batch_size = 20 + AND run_interval_seconds = 600 + """ + ) + ) + + +def downgrade() -> None: + """Keep the faster recovery cadence when older code is rolled back.""" + return diff --git a/web/backend/services/automation/__init__.py b/web/backend/services/automation/__init__.py index 2482731..6edf4d4 100644 --- a/web/backend/services/automation/__init__.py +++ b/web/backend/services/automation/__init__.py @@ -125,8 +125,8 @@ def register_default_jobs() -> None: interval_seconds=60, run_interval_seconds=60, # 有效 CK 的 storehome 快照默认 6 小时过期;1 万多个账号需要 - # 至少约 30 条/分钟才能在一个 TTL 周期内补完,留出 40 条余量。 - batch_size=40, + # 至少约 30 条/分钟才能在一个 TTL 周期内补完,留出 60 条余量。 + batch_size=60, concurrency=3, run=run_account_sync_queue_job, default_enabled=True, diff --git a/web/backend/services/automation/jobs/account_capability_refresh.py b/web/backend/services/automation/jobs/account_capability_refresh.py index 23c7d86..3afdcfe 100644 --- a/web/backend/services/automation/jobs/account_capability_refresh.py +++ b/web/backend/services/automation/jobs/account_capability_refresh.py @@ -37,7 +37,7 @@ from ...platform_fulfillment.policy import ( ) JOB_KEY = "account_capability_refresh" -DEFAULT_BACKFILL_RESERVED_SLOTS = 8 +DEFAULT_BACKFILL_RESERVED_SLOTS = 20 def _pending_demand_rows( diff --git a/web/backend/services/login_quota_service.py b/web/backend/services/login_quota_service.py index 5344744..bbc5629 100644 --- a/web/backend/services/login_quota_service.py +++ b/web/backend/services/login_quota_service.py @@ -2,9 +2,9 @@ from __future__ import annotations +import re from dataclasses import dataclass from datetime import UTC, date, datetime, timedelta -import re from zoneinfo import ZoneInfo from sqlalchemy import and_, func, or_, select @@ -46,7 +46,9 @@ _MANUAL_BYPASS_CATEGORIES = frozenset( CATEGORY_ACCOUNT_CHECK, } ) -_REFRESH_LOOKAHEAD = timedelta(hours=24) +# 只为下一小时可能到期的履约保护 CK 预留预算。按 24 小时总需求预留会把 +# 每小时额度全部锁死,导致首次登录和普通重登长期拿不到任何名额。 +_REFRESH_LOOKAHEAD = timedelta(hours=2) # 自动化登录优先级(数值越小优先级越高): # P0 履约保护失效重登 > P1 履约快过期续期 > P2 高价值重登 > P3 普通重登 > P4 首次登录 @@ -120,7 +122,9 @@ def humanize_login_message(message: str | None) -> str: lambda match: f"{category_label(match.group(1))}今日登录预算已用完", text, ) - text = text.replace("极验 slide 验证失败: forbidden", "极验滑块验证失败: 触发风控拦截") + text = text.replace( + "极验 slide 验证失败: forbidden", "极验滑块验证失败: 触发风控拦截" + ) text = text.replace("极验 slide 验证失败: fail", "极验滑块验证失败: 滑块未通过") text = text.replace("极验 slide 验证失败:", "极验滑块验证失败:") text = text.replace("极验 slide 验证失败", "极验滑块验证失败") @@ -291,7 +295,9 @@ def _category_limit(setting: LoginQuotaSetting, category: str) -> int: CATEGORY_RELOGIN_HIGH: int(setting.relogin_high_value_daily_limit), CATEGORY_RELOGIN_ORDINARY: int(setting.relogin_ordinary_daily_limit), CATEGORY_RELOGIN_PROTECTED: int( - getattr(setting, "relogin_protected_daily_limit", setting.daily_attempt_limit) + getattr( + setting, "relogin_protected_daily_limit", setting.daily_attempt_limit + ) ), CATEGORY_LEASE_REFRESH: int( getattr(setting, "lease_refresh_daily_limit", setting.daily_attempt_limit)