优化(账号自动化): 受保护账号极验重登允许按上限重试
This commit is contained in:
@@ -22,6 +22,7 @@ from datetime import UTC, datetime, timedelta
|
|||||||
from tests.support.db import get_test_engine, make_test_session
|
from tests.support.db import get_test_engine, make_test_session
|
||||||
from web.backend.models import (
|
from web.backend.models import (
|
||||||
Account,
|
Account,
|
||||||
|
AccountLease,
|
||||||
AccountWorkItem,
|
AccountWorkItem,
|
||||||
AutomationJob,
|
AutomationJob,
|
||||||
LoginAttempt,
|
LoginAttempt,
|
||||||
@@ -92,6 +93,21 @@ class _Base:
|
|||||||
self.session.commit()
|
self.session.commit()
|
||||||
return account
|
return account
|
||||||
|
|
||||||
|
def protect(self, account):
|
||||||
|
now = datetime.now(UTC)
|
||||||
|
self.session.add(
|
||||||
|
AccountLease(
|
||||||
|
account_id=account.id,
|
||||||
|
holder_user_id=self.admin.id,
|
||||||
|
scope="elite",
|
||||||
|
purpose="platform_fulfillment",
|
||||||
|
status="active",
|
||||||
|
leased_at=now,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.session.commit()
|
||||||
|
return account
|
||||||
|
|
||||||
def make_attempt(
|
def make_attempt(
|
||||||
self,
|
self,
|
||||||
account,
|
account,
|
||||||
@@ -450,6 +466,64 @@ class TestSelection(_Base):
|
|||||||
self.make_account("credential", relogin_failure_kind="credential")
|
self.make_account("credential", relogin_failure_kind="credential")
|
||||||
assert self._ids(job) == [keep.id]
|
assert self._ids(job) == [keep.id]
|
||||||
|
|
||||||
|
def test_protected_captcha_retries_until_attempt_cap(self):
|
||||||
|
job = self.job()
|
||||||
|
retryable = self.protect(
|
||||||
|
self.make_account("protected-captcha", relogin_failure_kind="captcha")
|
||||||
|
)
|
||||||
|
exhausted = self.protect(
|
||||||
|
self.make_account(
|
||||||
|
"protected-exhausted",
|
||||||
|
relogin_attempts=2,
|
||||||
|
relogin_failure_kind="captcha",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.make_account(
|
||||||
|
"ordinary-captcha",
|
||||||
|
relogin_failure_kind="captcha",
|
||||||
|
)
|
||||||
|
|
||||||
|
ids = self._ids(job)
|
||||||
|
|
||||||
|
assert retryable.id in ids
|
||||||
|
assert exhausted.id not in ids
|
||||||
|
|
||||||
|
def test_protected_captcha_work_item_is_claimable(self):
|
||||||
|
job = self.job()
|
||||||
|
protected = self.protect(
|
||||||
|
self.make_account("protected-captcha", relogin_failure_kind="captcha")
|
||||||
|
)
|
||||||
|
ordinary = self.make_account("ordinary", relogin_failure_kind="")
|
||||||
|
now = datetime.now(UTC)
|
||||||
|
protected_work = enqueue_account_work(
|
||||||
|
self.session,
|
||||||
|
account_id=protected.id,
|
||||||
|
work_type=ck_relogin.WORK_LOGIN_RELOGIN,
|
||||||
|
priority=10,
|
||||||
|
due_at=now - timedelta(hours=1),
|
||||||
|
max_attempts=20,
|
||||||
|
)
|
||||||
|
enqueue_account_work(
|
||||||
|
self.session,
|
||||||
|
account_id=ordinary.id,
|
||||||
|
work_type=ck_relogin.WORK_LOGIN_RELOGIN,
|
||||||
|
priority=30,
|
||||||
|
due_at=now - timedelta(hours=2),
|
||||||
|
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] == [protected_work.id]
|
||||||
|
|
||||||
def test_retryable_kinds_stay_eligible(self):
|
def test_retryable_kinds_stay_eligible(self):
|
||||||
job = self.job()
|
job = self.job()
|
||||||
code = self.make_account("code", relogin_failure_kind="email_code")
|
code = self.make_account("code", relogin_failure_kind="email_code")
|
||||||
@@ -635,6 +709,32 @@ class TestSettlement(_Base):
|
|||||||
assert account.relogin_failure_kind == "captcha"
|
assert account.relogin_failure_kind == "captcha"
|
||||||
assert account.relogin_next_at is None # 不再自动排期
|
assert account.relogin_next_at is None # 不再自动排期
|
||||||
|
|
||||||
|
def test_protected_captcha_retries_once_then_goes_manual(self):
|
||||||
|
account = self.protect(self.make_account("protected-cap"))
|
||||||
|
first_task = self.make_attempt(
|
||||||
|
account,
|
||||||
|
attempt_status="failed",
|
||||||
|
attempt_message="极验验证失败: slide",
|
||||||
|
)
|
||||||
|
first, _ = self._settle([(account, first_task)], max_attempts=2)
|
||||||
|
self.session.refresh(account)
|
||||||
|
|
||||||
|
assert first["retry"] == 1 and first["manual"] == 0
|
||||||
|
assert account.relogin_attempts == 1
|
||||||
|
assert account.relogin_next_at is not None
|
||||||
|
|
||||||
|
second_task = self.make_attempt(
|
||||||
|
account,
|
||||||
|
attempt_status="failed",
|
||||||
|
attempt_message="极验验证失败: slide",
|
||||||
|
)
|
||||||
|
second, _ = self._settle([(account, second_task)], max_attempts=2)
|
||||||
|
self.session.refresh(account)
|
||||||
|
|
||||||
|
assert second["retry"] == 0 and second["manual"] == 1
|
||||||
|
assert account.relogin_attempts == 2
|
||||||
|
assert account.relogin_next_at is None
|
||||||
|
|
||||||
def test_quota_backs_off_to_next_day(self):
|
def test_quota_backs_off_to_next_day(self):
|
||||||
account = self.make_account("quota")
|
account = self.make_account("quota")
|
||||||
task = self.make_attempt(
|
task = self.make_attempt(
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ from .account_capability_service import (
|
|||||||
OFFER_STATE_ACTIVE,
|
OFFER_STATE_ACTIVE,
|
||||||
OFFER_STATE_BEFORE,
|
OFFER_STATE_BEFORE,
|
||||||
)
|
)
|
||||||
from .account_lifecycle import MANUAL_STATES
|
from .account_lifecycle import MANUAL_KINDS, MANUAL_STATES
|
||||||
|
|
||||||
ACTIVE_COLLAB_STATUSES: tuple[str, ...] = (
|
ACTIVE_COLLAB_STATUSES: tuple[str, ...] = (
|
||||||
"incomplete",
|
"incomplete",
|
||||||
@@ -94,6 +94,31 @@ def automation_account_filter(*, cookie_operable: bool = True):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def relogin_failure_kind_filter(
|
||||||
|
*,
|
||||||
|
protected_ids: set[int] | frozenset[int],
|
||||||
|
max_attempts: int,
|
||||||
|
):
|
||||||
|
"""自动重登的失败分型准入。
|
||||||
|
|
||||||
|
普通账号的极验失败仍直接转人工;履约保护账号允许多试到账户次数上限,
|
||||||
|
因为它们持有生效租约/进行中订单,不能因为一次滑块失败就饿死。
|
||||||
|
"""
|
||||||
|
kind = func.coalesce(Account.relogin_failure_kind, "")
|
||||||
|
base = kind.notin_(tuple(sorted(MANUAL_KINDS)))
|
||||||
|
ids = sorted({int(value) for value in protected_ids})
|
||||||
|
if not ids:
|
||||||
|
return base
|
||||||
|
return or_(
|
||||||
|
base,
|
||||||
|
and_(
|
||||||
|
Account.id.in_(ids),
|
||||||
|
kind == "captcha",
|
||||||
|
Account.relogin_attempts < max(1, int(max_attempts)),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def assigned_to_pool_filter():
|
def assigned_to_pool_filter():
|
||||||
return or_(
|
return or_(
|
||||||
Account.assigned_to.is_(None),
|
Account.assigned_to.is_(None),
|
||||||
@@ -190,6 +215,7 @@ __all__ = [
|
|||||||
"fresh_eligible_state_filter",
|
"fresh_eligible_state_filter",
|
||||||
"pool_owner_ids",
|
"pool_owner_ids",
|
||||||
"recent_elite_bind_lock_exists",
|
"recent_elite_bind_lock_exists",
|
||||||
|
"relogin_failure_kind_filter",
|
||||||
"role_change_ready_filter",
|
"role_change_ready_filter",
|
||||||
"valid_ck_filter",
|
"valid_ck_filter",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -44,7 +44,10 @@ from ...account_protection_service import (
|
|||||||
ProtectionSnapshot,
|
ProtectionSnapshot,
|
||||||
load_protection_snapshot,
|
load_protection_snapshot,
|
||||||
)
|
)
|
||||||
from ...account_query_filters import automation_account_filter
|
from ...account_query_filters import (
|
||||||
|
automation_account_filter,
|
||||||
|
relogin_failure_kind_filter,
|
||||||
|
)
|
||||||
from ...account_value import is_high_value_expr, value_tier_expr
|
from ...account_value import is_high_value_expr, value_tier_expr
|
||||||
from ...account_work_queue import (
|
from ...account_work_queue import (
|
||||||
WORK_AGING_SECONDS,
|
WORK_AGING_SECONDS,
|
||||||
@@ -179,8 +182,9 @@ def _eligible_account_query(
|
|||||||
.filter(Account.relogin_attempts < max_attempts)
|
.filter(Account.relogin_attempts < max_attempts)
|
||||||
# 已判定需要人工的类型不再自动重试。空值用 coalesce 兜住历史值。
|
# 已判定需要人工的类型不再自动重试。空值用 coalesce 兜住历史值。
|
||||||
.filter(
|
.filter(
|
||||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
relogin_failure_kind_filter(
|
||||||
tuple(sorted(MANUAL_KINDS))
|
protected_ids=protection.relogin_ids,
|
||||||
|
max_attempts=max_attempts,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -961,6 +965,7 @@ def settle_results(
|
|||||||
"email_quota": 0,
|
"email_quota": 0,
|
||||||
"skipped": 0,
|
"skipped": 0,
|
||||||
}
|
}
|
||||||
|
protected_ids = load_protection_snapshot(db).relogin_ids
|
||||||
for account_id in account_ids:
|
for account_id in account_ids:
|
||||||
entry = fresh.get(account_id)
|
entry = fresh.get(account_id)
|
||||||
if entry is None:
|
if entry is None:
|
||||||
@@ -1007,7 +1012,14 @@ def settle_results(
|
|||||||
account.relogin_failure_kind = kind
|
account.relogin_failure_kind = kind
|
||||||
account.relogin_last_error = (task.attempt_message or "")[:500]
|
account.relogin_last_error = (task.attempt_message or "")[:500]
|
||||||
account.relogin_attempts = int(account.relogin_attempts or 0) + 1
|
account.relogin_attempts = int(account.relogin_attempts or 0) + 1
|
||||||
if kind in MANUAL_KINDS or account.relogin_attempts >= max_attempts:
|
protected_captcha_retry = (
|
||||||
|
kind == "captcha"
|
||||||
|
and int(account.id) in protected_ids
|
||||||
|
and account.relogin_attempts < max_attempts
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
kind in MANUAL_KINDS and not protected_captcha_retry
|
||||||
|
) or account.relogin_attempts >= max_attempts:
|
||||||
# 转人工:极验/凭证问题重试无意义,次数用尽也不该无限撞。
|
# 转人工:极验/凭证问题重试无意义,次数用尽也不该无限撞。
|
||||||
counters["manual"] += 1
|
counters["manual"] += 1
|
||||||
account.relogin_next_at = None
|
account.relogin_next_at = None
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ from ..models import (
|
|||||||
)
|
)
|
||||||
from .account_lifecycle import DEFAULT_CK_LIFETIME, MANUAL_KINDS
|
from .account_lifecycle import DEFAULT_CK_LIFETIME, MANUAL_KINDS
|
||||||
from .account_protection_service import ProtectionSnapshot, load_protection_snapshot
|
from .account_protection_service import ProtectionSnapshot, load_protection_snapshot
|
||||||
from .account_query_filters import automation_account_filter
|
from .account_query_filters import (
|
||||||
|
automation_account_filter,
|
||||||
|
relogin_failure_kind_filter,
|
||||||
|
)
|
||||||
from .account_value import is_high_value_account, is_high_value_expr
|
from .account_value import is_high_value_account, is_high_value_expr
|
||||||
from .douyu_service import LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
from .douyu_service import LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||||
|
|
||||||
@@ -256,6 +259,7 @@ def _pending_protected_invalid_query(
|
|||||||
*,
|
*,
|
||||||
protection: ProtectionSnapshot,
|
protection: ProtectionSnapshot,
|
||||||
now: datetime,
|
now: datetime,
|
||||||
|
max_attempts: int = 2,
|
||||||
):
|
):
|
||||||
relogin_ids = protection.relogin_ids
|
relogin_ids = protection.relogin_ids
|
||||||
if not relogin_ids:
|
if not relogin_ids:
|
||||||
@@ -264,8 +268,9 @@ def _pending_protected_invalid_query(
|
|||||||
Account.id.in_(relogin_ids),
|
Account.id.in_(relogin_ids),
|
||||||
Account.ck_check_status == "invalid",
|
Account.ck_check_status == "invalid",
|
||||||
automation_account_filter(cookie_operable=True),
|
automation_account_filter(cookie_operable=True),
|
||||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
relogin_failure_kind_filter(
|
||||||
tuple(sorted(MANUAL_KINDS))
|
protected_ids=relogin_ids,
|
||||||
|
max_attempts=max_attempts,
|
||||||
),
|
),
|
||||||
or_(
|
or_(
|
||||||
Account.relogin_next_at.is_(None),
|
Account.relogin_next_at.is_(None),
|
||||||
@@ -377,7 +382,12 @@ def _tier_reserve_needed(
|
|||||||
)
|
)
|
||||||
|
|
||||||
p0_pending = _count_available_today(
|
p0_pending = _count_available_today(
|
||||||
_pending_protected_invalid_query(db, protection=protection, now=now),
|
_pending_protected_invalid_query(
|
||||||
|
db,
|
||||||
|
protection=protection,
|
||||||
|
now=now,
|
||||||
|
max_attempts=per_account_limit,
|
||||||
|
),
|
||||||
per_account_limit=per_account_limit,
|
per_account_limit=per_account_limit,
|
||||||
quota_day=quota_day,
|
quota_day=quota_day,
|
||||||
)
|
)
|
||||||
@@ -424,7 +434,12 @@ def get_login_quota_usage_summary(
|
|||||||
per_account_limit = int(setting.per_account_daily_limit)
|
per_account_limit = int(setting.per_account_daily_limit)
|
||||||
|
|
||||||
p0_pending = _count_available_today(
|
p0_pending = _count_available_today(
|
||||||
_pending_protected_invalid_query(db, protection=protection, now=moment),
|
_pending_protected_invalid_query(
|
||||||
|
db,
|
||||||
|
protection=protection,
|
||||||
|
now=moment,
|
||||||
|
max_attempts=per_account_limit,
|
||||||
|
),
|
||||||
per_account_limit=per_account_limit,
|
per_account_limit=per_account_limit,
|
||||||
quota_day=quota_day,
|
quota_day=quota_day,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user