feat(fulfillment): 增加账号商品能力索引与生命周期事件

This commit is contained in:
yml
2026-11-18 19:30:59 +08:00
parent dc95d83b32
commit 667634a8df
35 changed files with 3076 additions and 82 deletions
+60 -12
View File
@@ -12,6 +12,7 @@ from core.douyu.activity_client import DouyuActivityClient
from core.douyu.cookie_utils import cookie_value
from ..models import Account, DouyuConfig, DouyuTask, LoginTask
from .account_events_service import CATEGORY_ACTIVITY, EVENT_TASK_STARTED, emit_bulk
SUPPORTED_DOUYU_TASK_TYPES = {
"get_bind_qr": "获取绑定二维码",
@@ -291,19 +292,47 @@ def create_douyu_planned_tasks(
batch_id = uuid.uuid4().hex[:12]
payload = payload or {}
task_rows: list[tuple[DouyuTask, Account]] = []
for account in accounts:
db.add(
DouyuTask(
batch_id=batch_id,
account_id=account.id,
account_assigned_to=account.assigned_to,
task_type=task_type,
handbook_scope=handbook_scope,
status="planned",
message="任务已创建,等待执行",
result={"payload": payload} if payload else None,
created_by=created_by,
)
task = DouyuTask(
batch_id=batch_id,
account_id=account.id,
account_assigned_to=account.assigned_to,
task_type=task_type,
handbook_scope=handbook_scope,
status="planned",
message="任务已创建,等待执行",
result={"payload": payload} if payload else None,
created_by=created_by,
)
db.add(task)
task_rows.append((task, account))
db.flush()
if task_rows:
label = SUPPORTED_DOUYU_TASK_TYPES[task_type]
emit_bulk(
db,
[
{
"account_id": account.id,
"event_type": EVENT_TASK_STARTED,
"title": f"{label}:任务已创建",
"category": CATEGORY_ACTIVITY,
"detail": {
"task_id": task.id,
"task_type": task_type,
"scope": handbook_scope,
"status": "planned",
},
"source": "douyu_task",
"subject_type": "task",
"subject_key": str(task.id),
"actor_type": "user",
"actor_id": created_by,
"source_ref": f"douyu_task_started:{task.id}",
}
for task, account in task_rows
],
)
db.commit()
return batch_id, len(accounts)
@@ -328,10 +357,29 @@ def cleanup_orphan_douyu_tasks(
if not tasks:
return 0
now = datetime.now(UTC)
from .business_events import record_douyu_task_result
for task in tasks:
task.status = "stopped"
task.message = message
task.finished_at = now
if task.account_id:
account = db.query(Account).filter(Account.id == task.account_id).first()
if account is not None:
record_douyu_task_result(
db,
account,
task_id=int(task.id),
task_type=str(task.task_type or ""),
task_label=SUPPORTED_DOUYU_TASK_TYPES.get(
str(task.task_type or ""),
str(task.task_type or "斗鱼任务"),
),
scope=str(task.handbook_scope or ""),
status="stopped",
message=message,
occurred_at=now,
)
db.commit()
return len(tasks)