feat(fulfillment): 新增直播账号履约工作台
This commit is contained in:
@@ -22,6 +22,12 @@ from .jobs.douyu_tasks_retention import (
|
||||
from .jobs.douyu_tasks_retention import (
|
||||
run_douyu_tasks_retention,
|
||||
)
|
||||
from .jobs.platform_fulfillment_lease_reaper import (
|
||||
JOB_KEY as PLATFORM_FULFILLMENT_LEASE_REAPER_KEY,
|
||||
)
|
||||
from .jobs.platform_fulfillment_lease_reaper import (
|
||||
run_platform_fulfillment_lease_reaper,
|
||||
)
|
||||
from .jobs.points_expire import JOB_KEY as POINTS_EXPIRE_KEY
|
||||
from .jobs.points_expire import run_points_expire_check
|
||||
from .scheduler import (
|
||||
@@ -43,6 +49,7 @@ __all__ = [
|
||||
"CK_HEALTH_KEY",
|
||||
"CK_RELOGIN_KEY",
|
||||
"DOUYU_TASKS_RETENTION_KEY",
|
||||
"PLATFORM_FULFILLMENT_LEASE_REAPER_KEY",
|
||||
"POINTS_EXPIRE_KEY",
|
||||
"JobSpec",
|
||||
"automation_enabled",
|
||||
@@ -57,6 +64,7 @@ __all__ = [
|
||||
"run_douyu_tasks_retention",
|
||||
"run_due_jobs",
|
||||
"run_job_now",
|
||||
"run_platform_fulfillment_lease_reaper",
|
||||
"run_points_expire_check",
|
||||
"scheduler_status",
|
||||
"start_scheduler",
|
||||
@@ -140,3 +148,14 @@ def register_default_jobs() -> None:
|
||||
run=run_ck_health,
|
||||
)
|
||||
)
|
||||
register_job(
|
||||
JobSpec(
|
||||
key=PLATFORM_FULFILLMENT_LEASE_REAPER_KEY,
|
||||
title="直播账号履约租约回收",
|
||||
interval_seconds=60,
|
||||
run_interval_seconds=60,
|
||||
batch_size=500,
|
||||
concurrency=1,
|
||||
run=run_platform_fulfillment_lease_reaper,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
"""回收过期的直播账号履约租约。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import AccountLease, AutomationJob, PlatformFulfillmentTask
|
||||
from ...platform_fulfillment.account_selector import (
|
||||
expire_platform_fulfillment_leases,
|
||||
)
|
||||
|
||||
JOB_KEY = "platform_fulfillment_lease_reaper"
|
||||
|
||||
|
||||
def run_platform_fulfillment_lease_reaper(
|
||||
db: Session,
|
||||
job: AutomationJob,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
moment = now or datetime.now(UTC)
|
||||
released_ids = {
|
||||
lease_id
|
||||
for (lease_id,) in db.query(AccountLease.id)
|
||||
.filter(
|
||||
AccountLease.purpose == "platform_fulfillment",
|
||||
AccountLease.status == "active",
|
||||
AccountLease.expires_at <= moment,
|
||||
)
|
||||
.limit(max(1, int(job.batch_size or 500)))
|
||||
.all()
|
||||
}
|
||||
expired = expire_platform_fulfillment_leases(
|
||||
db,
|
||||
now=moment,
|
||||
limit=max(1, int(job.batch_size or 500)),
|
||||
)
|
||||
affected = 0
|
||||
if released_ids:
|
||||
rows = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(PlatformFulfillmentTask.lease_id.in_(released_ids))
|
||||
.all()
|
||||
)
|
||||
for task in rows:
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
continue
|
||||
task.execution_status = "manual_required"
|
||||
task.issue_code = "lease_expired"
|
||||
task.last_error = "账号租约已过期,请重新分配账号"
|
||||
task.updated_at = moment
|
||||
affected += 1
|
||||
db.commit()
|
||||
return {
|
||||
"scanned": expired,
|
||||
"expired": expired,
|
||||
"affected_tasks": affected,
|
||||
"message": f"直播账号履约租约回收 {expired} 条",
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["JOB_KEY", "run_platform_fulfillment_lease_reaper"]
|
||||
Reference in New Issue
Block a user