feat(fulfillment): 实时刷新斗鱼账号并支持租约时长设置
This commit is contained in:
@@ -14,6 +14,7 @@ from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import Account, AccountEvent, AccountLease, User
|
||||
from ..account_lifecycle import MANUAL_STATES
|
||||
|
||||
VALIDATION_LEASE_MINUTES = 5
|
||||
BINDING_LEASE_MINUTES = 120
|
||||
@@ -109,6 +110,62 @@ def eligible_accounts(
|
||||
]
|
||||
|
||||
|
||||
def live_candidate_accounts(
|
||||
db: Session,
|
||||
*,
|
||||
limit: int = 20,
|
||||
) -> list[Account]:
|
||||
"""选出值得实时检测的账号,不读取 CK/积分/开通等缓存结论。"""
|
||||
owner_ids = select(User.id).where(User.role.in_(POOL_OWNER_ROLES))
|
||||
active_lease = (
|
||||
select(AccountLease.id)
|
||||
.where(AccountLease.active_account_id == Account.id)
|
||||
.exists()
|
||||
)
|
||||
return (
|
||||
db.query(Account)
|
||||
.filter(
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||
~active_lease,
|
||||
)
|
||||
.order_by(
|
||||
Account.ck_checked_at.is_(None).desc(),
|
||||
Account.ck_checked_at.asc(),
|
||||
Account.points.is_(None).desc(),
|
||||
Account.points.asc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
def has_recent_exchange(
|
||||
db: Session,
|
||||
*,
|
||||
account_id: int,
|
||||
commodity_id: str,
|
||||
now: datetime | None = None,
|
||||
) -> bool:
|
||||
moment = _now(now)
|
||||
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
|
||||
return (
|
||||
db.query(AccountEvent.id)
|
||||
.filter(
|
||||
AccountEvent.account_id == account_id,
|
||||
AccountEvent.event_type == "exchange",
|
||||
AccountEvent.subject_key == commodity_id,
|
||||
AccountEvent.occurred_at >= since,
|
||||
)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
|
||||
def lease_account(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -116,7 +173,7 @@ def lease_account(
|
||||
holder_user_id: int,
|
||||
task_id: int,
|
||||
commodity_id: str,
|
||||
ttl_minutes: int = VALIDATION_LEASE_MINUTES,
|
||||
ttl_minutes: int | None = None,
|
||||
now: datetime | None = None,
|
||||
) -> AccountLease | None:
|
||||
"""尝试领取一个账号;撞唯一索引时返回 None。"""
|
||||
@@ -130,7 +187,11 @@ def lease_account(
|
||||
batch_id=f"pf:{task_id}",
|
||||
status="active",
|
||||
leased_at=moment,
|
||||
expires_at=moment + timedelta(minutes=max(1, int(ttl_minutes))),
|
||||
expires_at=(
|
||||
None
|
||||
if ttl_minutes is None
|
||||
else moment + timedelta(minutes=max(1, int(ttl_minutes)))
|
||||
),
|
||||
release_reason="",
|
||||
created_at=moment,
|
||||
)
|
||||
@@ -150,12 +211,16 @@ def renew_lease(
|
||||
db: Session,
|
||||
lease: AccountLease,
|
||||
*,
|
||||
ttl_minutes: int = BINDING_LEASE_MINUTES,
|
||||
ttl_minutes: int | None = None,
|
||||
now: datetime | None = None,
|
||||
) -> AccountLease:
|
||||
if lease.status != "active":
|
||||
raise ValueError("租约已结束,不能续期")
|
||||
lease.expires_at = _now(now) + timedelta(minutes=max(1, int(ttl_minutes)))
|
||||
lease.expires_at = (
|
||||
None
|
||||
if ttl_minutes is None
|
||||
else _now(now) + timedelta(minutes=max(1, int(ttl_minutes)))
|
||||
)
|
||||
db.commit()
|
||||
return lease
|
||||
|
||||
@@ -188,6 +253,7 @@ def expire_platform_fulfillment_leases(
|
||||
.filter(
|
||||
AccountLease.purpose == "platform_fulfillment",
|
||||
AccountLease.status == "active",
|
||||
AccountLease.expires_at.isnot(None),
|
||||
AccountLease.expires_at <= moment,
|
||||
)
|
||||
.order_by(AccountLease.id.asc())
|
||||
@@ -210,7 +276,9 @@ __all__ = [
|
||||
"AccountCandidate",
|
||||
"eligible_accounts",
|
||||
"expire_platform_fulfillment_leases",
|
||||
"has_recent_exchange",
|
||||
"lease_account",
|
||||
"live_candidate_accounts",
|
||||
"release_lease",
|
||||
"renew_lease",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user