fix(fulfillment): 修正自动选号候选池
This commit is contained in:
@@ -304,6 +304,50 @@ class TestAccountSelection(_Base):
|
|||||||
assert detail["task"]["executionStatus"] == "allocated"
|
assert detail["task"]["executionStatus"] == "allocated"
|
||||||
self.refresh_mock.assert_called()
|
self.refresh_mock.assert_called()
|
||||||
|
|
||||||
|
def test_live_candidates_include_support_accounts_and_prefer_valid_cache(self):
|
||||||
|
support = User(
|
||||||
|
username="pool-support",
|
||||||
|
password_hash="h",
|
||||||
|
role="support",
|
||||||
|
is_active=True,
|
||||||
|
)
|
||||||
|
self.session.add(support)
|
||||||
|
self.session.flush()
|
||||||
|
invalid = Account(
|
||||||
|
username="old-invalid",
|
||||||
|
password="p",
|
||||||
|
email="invalid@example.com",
|
||||||
|
email_password="ep",
|
||||||
|
status="invalid",
|
||||||
|
ck_check_status="invalid",
|
||||||
|
ck_checked_at=NOW - timedelta(days=2),
|
||||||
|
cookie="PHPSESSID=invalid",
|
||||||
|
auto_managed=True,
|
||||||
|
)
|
||||||
|
valid = Account(
|
||||||
|
username="support-valid",
|
||||||
|
password="p",
|
||||||
|
email="valid@example.com",
|
||||||
|
email_password="ep",
|
||||||
|
status="active",
|
||||||
|
ck_check_status="valid",
|
||||||
|
ck_checked_at=NOW - timedelta(days=1),
|
||||||
|
cookie="PHPSESSID=valid",
|
||||||
|
elite_opened=True,
|
||||||
|
points=2000,
|
||||||
|
auto_managed=True,
|
||||||
|
assigned_to=support.id,
|
||||||
|
)
|
||||||
|
self.session.add_all([invalid, valid])
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
|
rows = account_selector.live_candidate_accounts(
|
||||||
|
self.session,
|
||||||
|
required_points=1680,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert [row.id for row in rows] == [valid.id, invalid.id]
|
||||||
|
|
||||||
def test_points_short_is_soft_and_does_not_block_allocation(self):
|
def test_points_short_is_soft_and_does_not_block_allocation(self):
|
||||||
account = self.account("points-short", points=100)
|
account = self.account("points-short", points=100)
|
||||||
task = self.task()
|
task = self.task()
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
|
|
||||||
from sqlalchemy import or_, select
|
from sqlalchemy import case, or_, select
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ from ..account_lifecycle import MANUAL_STATES
|
|||||||
VALIDATION_LEASE_MINUTES = 5
|
VALIDATION_LEASE_MINUTES = 5
|
||||||
BINDING_LEASE_MINUTES = 120
|
BINDING_LEASE_MINUTES = 120
|
||||||
EXCHANGE_COOLDOWN_DAYS = 365
|
EXCHANGE_COOLDOWN_DAYS = 365
|
||||||
POOL_OWNER_ROLES = ("super_admin", "operation")
|
POOL_OWNER_ROLES = ("super_admin", "operation", "support")
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@@ -41,6 +41,13 @@ def _now(now: datetime | None = None) -> datetime:
|
|||||||
return now or datetime.now(UTC)
|
return now or datetime.now(UTC)
|
||||||
|
|
||||||
|
|
||||||
|
def _pool_owner_ids():
|
||||||
|
return select(User.id).where(
|
||||||
|
User.role.in_(POOL_OWNER_ROLES),
|
||||||
|
User.is_active.is_(True),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def eligible_accounts(
|
def eligible_accounts(
|
||||||
db: Session,
|
db: Session,
|
||||||
*,
|
*,
|
||||||
@@ -52,7 +59,7 @@ def eligible_accounts(
|
|||||||
"""筛选能人工兑换指定精英宝典商品的账号。"""
|
"""筛选能人工兑换指定精英宝典商品的账号。"""
|
||||||
moment = _now(now)
|
moment = _now(now)
|
||||||
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
|
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
|
||||||
owner_ids = select(User.id).where(User.role.in_(POOL_OWNER_ROLES))
|
owner_ids = _pool_owner_ids()
|
||||||
active_lease = (
|
active_lease = (
|
||||||
select(AccountLease.id)
|
select(AccountLease.id)
|
||||||
.where(AccountLease.active_account_id == Account.id)
|
.where(AccountLease.active_account_id == Account.id)
|
||||||
@@ -114,28 +121,49 @@ def live_candidate_accounts(
|
|||||||
db: Session,
|
db: Session,
|
||||||
*,
|
*,
|
||||||
limit: int = 20,
|
limit: int = 20,
|
||||||
|
required_points: int = 0,
|
||||||
|
commodity_id: str = "",
|
||||||
) -> list[Account]:
|
) -> list[Account]:
|
||||||
"""选出值得实时检测的账号,不读取 CK/积分/开通等缓存结论。"""
|
"""按缓存质量优先排序,但保留实时检测作为最终准入判断。"""
|
||||||
owner_ids = select(User.id).where(User.role.in_(POOL_OWNER_ROLES))
|
owner_ids = _pool_owner_ids()
|
||||||
active_lease = (
|
active_lease = (
|
||||||
select(AccountLease.id)
|
select(AccountLease.id)
|
||||||
.where(AccountLease.active_account_id == Account.id)
|
.where(AccountLease.active_account_id == Account.id)
|
||||||
.exists()
|
.exists()
|
||||||
)
|
)
|
||||||
return (
|
query = db.query(Account).filter(
|
||||||
db.query(Account)
|
Account.cookie.isnot(None),
|
||||||
.filter(
|
Account.cookie != "",
|
||||||
Account.cookie.isnot(None),
|
Account.auto_managed.is_(True),
|
||||||
Account.cookie != "",
|
Account.status.notin_(MANUAL_STATES),
|
||||||
Account.auto_managed.is_(True),
|
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||||
Account.status.notin_(MANUAL_STATES),
|
~active_lease,
|
||||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
)
|
||||||
~active_lease,
|
commodity = commodity_id.strip()
|
||||||
|
if commodity:
|
||||||
|
since = _now() - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
|
||||||
|
exchanged = (
|
||||||
|
select(AccountEvent.id)
|
||||||
|
.where(
|
||||||
|
AccountEvent.account_id == Account.id,
|
||||||
|
AccountEvent.event_type == "exchange",
|
||||||
|
AccountEvent.subject_key == commodity,
|
||||||
|
AccountEvent.occurred_at >= since,
|
||||||
|
)
|
||||||
|
.exists()
|
||||||
)
|
)
|
||||||
.order_by(
|
query = query.filter(~exchanged)
|
||||||
|
return (
|
||||||
|
query.order_by(
|
||||||
|
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||||
|
case((Account.status == "active", 0), else_=1),
|
||||||
|
case((Account.elite_opened.is_(True), 0), else_=1),
|
||||||
|
case(
|
||||||
|
(Account.points >= max(0, int(required_points)), 0),
|
||||||
|
else_=1,
|
||||||
|
),
|
||||||
Account.ck_checked_at.is_(None).desc(),
|
Account.ck_checked_at.is_(None).desc(),
|
||||||
Account.ck_checked_at.asc(),
|
Account.ck_checked_at.asc(),
|
||||||
Account.points.is_(None).desc(),
|
|
||||||
Account.points.asc(),
|
Account.points.asc(),
|
||||||
Account.id.asc(),
|
Account.id.asc(),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ ACTIVE_STATUSES = {
|
|||||||
|
|
||||||
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600"))
|
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600"))
|
||||||
AUTO_SYNC_INTERVAL_SECONDS = 900.0
|
AUTO_SYNC_INTERVAL_SECONDS = 900.0
|
||||||
|
DEFAULT_LIVE_CANDIDATE_LIMIT = 20
|
||||||
|
|
||||||
_SYNC_LOCK = Lock()
|
_SYNC_LOCK = Lock()
|
||||||
_AUTO_SYNC_LOCK = Lock()
|
_AUTO_SYNC_LOCK = Lock()
|
||||||
@@ -888,9 +889,23 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
|||||||
manual_id = str(douyu_config_value("manual_id", config.manual_id))
|
manual_id = str(douyu_config_value("manual_id", config.manual_id))
|
||||||
rid = str(douyu_config_value("rid", config.rid))
|
rid = str(douyu_config_value("rid", config.rid))
|
||||||
act_alias = str(douyu_config_value("bind_act_alias", config.bind_act_alias))
|
act_alias = str(douyu_config_value("bind_act_alias", config.bind_act_alias))
|
||||||
# 单次最多实时检测 5 个,避免一次选号请求被外部接口拖到超时。
|
try:
|
||||||
# 检测时间会写回 ck_checked_at,下一次点击自动轮到下一批。
|
candidate_limit = int(
|
||||||
candidates = live_candidate_accounts(db, limit=5)
|
os.getenv(
|
||||||
|
"PLATFORM_FULFILLMENT_LIVE_CANDIDATE_LIMIT",
|
||||||
|
str(DEFAULT_LIVE_CANDIDATE_LIMIT),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
candidate_limit = DEFAULT_LIVE_CANDIDATE_LIMIT
|
||||||
|
candidate_limit = max(1, min(candidate_limit, 100))
|
||||||
|
# 缓存用于优先排序,最终是否可用仍由下面的逐个实时检测决定。
|
||||||
|
candidates = live_candidate_accounts(
|
||||||
|
db,
|
||||||
|
limit=candidate_limit,
|
||||||
|
required_points=required_points,
|
||||||
|
commodity_id=task.commodity_id,
|
||||||
|
)
|
||||||
diagnostics: list[dict[str, Any]] = []
|
diagnostics: list[dict[str, Any]] = []
|
||||||
qualified: list[tuple[Account, DouyuLiveAccountState]] = []
|
qualified: list[tuple[Account, DouyuLiveAccountState]] = []
|
||||||
selected: AccountCandidate | None = None
|
selected: AccountCandidate | None = None
|
||||||
|
|||||||
Reference in New Issue
Block a user