perf(automation): 持久队列驱动并优化履约热路径
This commit is contained in:
@@ -30,6 +30,10 @@ from web.backend.models import (
|
||||
User,
|
||||
)
|
||||
from web.backend.services.account_lifecycle import derive_status
|
||||
from web.backend.services.account_protection_service import (
|
||||
load_protection_snapshot,
|
||||
)
|
||||
from web.backend.services.account_work_queue import enqueue_account_work
|
||||
from web.backend.services.automation import register_default_jobs, reset_registry
|
||||
from web.backend.services.automation.jobs import ck_relogin
|
||||
|
||||
@@ -325,6 +329,21 @@ class TestAdaptiveBatch(_Base):
|
||||
assert decision.batch_size == 10
|
||||
assert decision.action == "shrink"
|
||||
|
||||
def test_legacy_batch_ceiling_no_longer_blocks_adaptive_capacity(self):
|
||||
job = self.job(batch_size=5)
|
||||
job.config = {}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.configured_max == 20
|
||||
assert decision.batch_size == 10
|
||||
|
||||
|
||||
class TestBackoff(_Base):
|
||||
def test_ladder_is_escalating(self):
|
||||
@@ -424,6 +443,87 @@ class TestSelection(_Base):
|
||||
self.make_account(f"a{index}")
|
||||
assert len(self._ids(job)) == 3
|
||||
|
||||
def test_claim_uses_existing_persistent_queue(self):
|
||||
job = self.job()
|
||||
account = self.make_account("queued")
|
||||
now = datetime.now(UTC)
|
||||
work = enqueue_account_work(
|
||||
self.session,
|
||||
account_id=account.id,
|
||||
work_type=ck_relogin.WORK_LOGIN_RELOGIN,
|
||||
priority=30,
|
||||
due_at=now - timedelta(hours=1),
|
||||
max_attempts=20,
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
claimed = ck_relogin._claim_relogin_work(
|
||||
self.session,
|
||||
job,
|
||||
now,
|
||||
max_attempts=2,
|
||||
protection=load_protection_snapshot(self.session),
|
||||
limit=5,
|
||||
)
|
||||
|
||||
assert [item.id for item in claimed] == [work.id]
|
||||
|
||||
def test_replenishment_only_enqueues_due_actionable_accounts(self):
|
||||
job = self.job()
|
||||
ready = self.make_account("ready")
|
||||
self.make_account("captcha", relogin_failure_kind="captcha")
|
||||
self.make_account("future", relogin_next_at=datetime.now(UTC) + timedelta(hours=1))
|
||||
self.make_account("disabled", auto_managed=False)
|
||||
now = datetime.now(UTC)
|
||||
|
||||
added = ck_relogin._enqueue_due_relogin_work(
|
||||
self.session,
|
||||
job,
|
||||
now,
|
||||
max_attempts=2,
|
||||
protection=load_protection_snapshot(self.session),
|
||||
limit=10,
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
queued = self.session.query(AccountWorkItem).all()
|
||||
assert added == 1
|
||||
assert [item.account_id for item in queued] == [ready.id]
|
||||
|
||||
def test_aged_work_item_preempts_new_high_priority_item(self):
|
||||
job = self.job()
|
||||
old = self.make_account("old-work")
|
||||
new = self.make_account("new-work")
|
||||
now = datetime.now(UTC)
|
||||
old_work = enqueue_account_work(
|
||||
self.session,
|
||||
account_id=old.id,
|
||||
work_type=ck_relogin.WORK_LOGIN_RELOGIN,
|
||||
priority=30,
|
||||
due_at=now - timedelta(seconds=ck_relogin.WORK_AGING_SECONDS + 60),
|
||||
max_attempts=20,
|
||||
)
|
||||
enqueue_account_work(
|
||||
self.session,
|
||||
account_id=new.id,
|
||||
work_type=ck_relogin.WORK_LOGIN_RELOGIN,
|
||||
priority=10,
|
||||
due_at=now,
|
||||
max_attempts=20,
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
claimed = ck_relogin._claim_relogin_work(
|
||||
self.session,
|
||||
job,
|
||||
now,
|
||||
max_attempts=2,
|
||||
protection=load_protection_snapshot(self.session),
|
||||
limit=1,
|
||||
)
|
||||
|
||||
assert [item.id for item in claimed] == [old_work.id]
|
||||
|
||||
def test_high_value_is_picked_before_plain(self):
|
||||
"""名额先给第 1 档,其次第 2 档,普通失效号排最后。"""
|
||||
job = self.job(batch_size=3)
|
||||
@@ -674,6 +774,23 @@ class TestLinkageWithHealthJob(_Base):
|
||||
assert relogin["batch_size"] == 10
|
||||
assert payload["pending_relogin"] == 25 # 不被 limit 10 截断
|
||||
|
||||
def test_pending_count_prefers_persistent_queue(self):
|
||||
account = self.make_account(
|
||||
"scheduled",
|
||||
relogin_next_at=datetime.now(UTC) + timedelta(hours=2),
|
||||
)
|
||||
enqueue_account_work(
|
||||
self.session,
|
||||
account_id=account.id,
|
||||
work_type=ck_relogin.WORK_LOGIN_RELOGIN,
|
||||
priority=30,
|
||||
due_at=datetime.now(UTC) + timedelta(hours=2),
|
||||
max_attempts=20,
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
assert ck_relogin.count_pending_relogin(self.session) == 1
|
||||
|
||||
|
||||
class TestSettlementVisibility(_Base):
|
||||
"""回归:结算必须用**新事务**读回 runner 提交的结果。
|
||||
|
||||
Reference in New Issue
Block a user