fix(douyu): 统一外部协作账号租约
This commit is contained in:
@@ -290,14 +290,45 @@ def lease_account(
|
||||
now: datetime | None = None,
|
||||
) -> AccountLease | None:
|
||||
"""尝试领取一个账号;撞唯一索引时返回 None。"""
|
||||
moment = _now(now)
|
||||
lease = AccountLease(
|
||||
account_id=candidate.account_id,
|
||||
return acquire_account_lease(
|
||||
db,
|
||||
account_id=int(candidate.account_id),
|
||||
holder_user_id=holder_user_id,
|
||||
scope="elite",
|
||||
purpose="platform_fulfillment",
|
||||
commodity_id=commodity_id,
|
||||
batch_id=f"pf:{task_id}",
|
||||
ttl_minutes=ttl_minutes,
|
||||
now=now,
|
||||
)
|
||||
|
||||
|
||||
def acquire_account_lease(
|
||||
db: Session,
|
||||
*,
|
||||
account_id: int,
|
||||
holder_user_id: int,
|
||||
scope: str,
|
||||
purpose: str,
|
||||
commodity_id: str = "",
|
||||
batch_id: str = "",
|
||||
ttl_minutes: int | None = None,
|
||||
now: datetime | None = None,
|
||||
commit: bool = True,
|
||||
) -> AccountLease | None:
|
||||
"""获取账号租约;调用方可在自己的事务内复用。
|
||||
|
||||
唯一索引负责并发互斥。撞到已有活动租约时返回 ``None``,由调用方结合
|
||||
``active_lease_for_account`` 决定是沿用合法占用的租约还是提示冲突。
|
||||
"""
|
||||
moment = _now(now)
|
||||
lease = AccountLease(
|
||||
account_id=int(account_id),
|
||||
holder_user_id=holder_user_id,
|
||||
scope=str(scope or "elite")[:16],
|
||||
purpose=str(purpose or "exchange")[:24],
|
||||
commodity_id=str(commodity_id or "")[:64],
|
||||
batch_id=str(batch_id or "")[:64],
|
||||
status="active",
|
||||
leased_at=moment,
|
||||
expires_at=(
|
||||
@@ -316,30 +347,54 @@ def lease_account(
|
||||
if lease in db:
|
||||
db.expunge(lease)
|
||||
return None
|
||||
event_detail: dict[str, object] = {
|
||||
"lease_id": lease.id,
|
||||
"holder_user_id": holder_user_id,
|
||||
"commodity_id": commodity_id,
|
||||
"purpose": purpose,
|
||||
"batch_id": batch_id,
|
||||
}
|
||||
if purpose == "platform_fulfillment" and batch_id.startswith("pf:"):
|
||||
event_detail["task_id"] = int(batch_id.removeprefix("pf:"))
|
||||
emit(
|
||||
db,
|
||||
account_id=int(candidate.account_id),
|
||||
account_id=int(account_id),
|
||||
event_type=EVENT_LEASE_ACQUIRED,
|
||||
title=f"履约占用:任务 #{task_id}",
|
||||
title=(
|
||||
f"履约占用:任务 #{batch_id.removeprefix('pf:')}"
|
||||
if purpose == "platform_fulfillment"
|
||||
else f"业务占用:{purpose}"
|
||||
),
|
||||
category=CATEGORY_OPS,
|
||||
detail={
|
||||
"lease_id": lease.id,
|
||||
"task_id": task_id,
|
||||
"holder_user_id": holder_user_id,
|
||||
"commodity_id": commodity_id,
|
||||
"purpose": "platform_fulfillment",
|
||||
},
|
||||
occurred_at=moment,
|
||||
detail=event_detail,
|
||||
source="lease",
|
||||
subject_type="task",
|
||||
subject_key=str(task_id),
|
||||
subject_type="task" if purpose == "platform_fulfillment" else "lease",
|
||||
subject_key=batch_id,
|
||||
actor_type="user",
|
||||
actor_id=holder_user_id,
|
||||
source_ref=f"account_lease_acquired:{lease.id}",
|
||||
)
|
||||
db.commit()
|
||||
if commit:
|
||||
db.commit()
|
||||
return lease
|
||||
|
||||
|
||||
def active_lease_for_account(
|
||||
db: Session, account_id: int | None
|
||||
) -> AccountLease | None:
|
||||
if not account_id:
|
||||
return None
|
||||
return (
|
||||
db.query(AccountLease)
|
||||
.filter(
|
||||
AccountLease.account_id == int(account_id),
|
||||
AccountLease.status == "active",
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def renew_lease(
|
||||
db: Session,
|
||||
lease: AccountLease,
|
||||
@@ -364,6 +419,7 @@ def release_lease(
|
||||
*,
|
||||
reason: str,
|
||||
now: datetime | None = None,
|
||||
commit: bool = True,
|
||||
) -> bool:
|
||||
if lease is None or lease.status != "active":
|
||||
return False
|
||||
@@ -387,7 +443,8 @@ def release_lease(
|
||||
actor_type="system",
|
||||
source_ref=f"account_lease_released:{lease.id}",
|
||||
)
|
||||
db.commit()
|
||||
if commit:
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
@@ -442,6 +499,8 @@ __all__ = [
|
||||
"EXCHANGE_COOLDOWN_DAYS",
|
||||
"VALIDATION_LEASE_MINUTES",
|
||||
"AccountCandidate",
|
||||
"acquire_account_lease",
|
||||
"active_lease_for_account",
|
||||
"capability_candidate_accounts",
|
||||
"eligible_accounts",
|
||||
"expire_platform_fulfillment_leases",
|
||||
|
||||
Reference in New Issue
Block a user