perf(douyu): 消除任务锁竞争并增加创建背压
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import and_, func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -146,10 +148,42 @@ DOUYU_ACTIVE_PAYMENT_TASK_TYPES = (
|
||||
)
|
||||
|
||||
|
||||
def _env_positive_int(name: str, default: int) -> int:
|
||||
try:
|
||||
value = int(os.getenv(name, str(default)))
|
||||
except ValueError:
|
||||
return default
|
||||
return value if value > 0 else default
|
||||
|
||||
|
||||
DOUYU_MAX_ACTIVE_TASKS = _env_positive_int("DOUYU_MAX_ACTIVE_TASKS", 250)
|
||||
|
||||
|
||||
class DouyuActivePaymentError(ValueError):
|
||||
"""同一账号已有未结束的充值任务。"""
|
||||
|
||||
|
||||
class DouyuTaskBackpressureError(ValueError):
|
||||
"""全局活跃斗鱼任务超过安全上限。"""
|
||||
|
||||
|
||||
def _ensure_active_task_budget(db: Session, additional: int) -> None:
|
||||
"""为任务创建提供全局背压,避免无限线程和连接同时挤入数据库。"""
|
||||
if additional <= 0:
|
||||
return
|
||||
active = (
|
||||
db.query(func.count(DouyuTask.id))
|
||||
.filter(DouyuTask.status.in_(DOUYU_ACTIVE_TASK_STATUSES))
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
if active + additional > DOUYU_MAX_ACTIVE_TASKS:
|
||||
raise DouyuTaskBackpressureError(
|
||||
f"斗鱼任务繁忙:当前活跃 {active} 个,"
|
||||
f"上限 {DOUYU_MAX_ACTIVE_TASKS} 个,请稍后重试"
|
||||
)
|
||||
|
||||
|
||||
def douyu_config_value(field: str, value):
|
||||
"""读取配置值;空值自动回退到默认值。"""
|
||||
default = DOUYU_CONFIG_DEFAULTS[field]
|
||||
@@ -348,6 +382,7 @@ def create_douyu_planned_tasks(
|
||||
raise ValueError("该任务不属于当前工作台")
|
||||
|
||||
accounts = visible_douyu_task_accounts(db, account_ids)
|
||||
_ensure_active_task_budget(db, len(accounts))
|
||||
_lock_payment_accounts(db, accounts, task_type)
|
||||
if (
|
||||
task_type in {"refresh_goods", "refresh_esports_goods", "refresh_xpd_goods"}
|
||||
@@ -375,33 +410,39 @@ def create_douyu_planned_tasks(
|
||||
db.add(task)
|
||||
task_rows.append((task, account))
|
||||
db.flush()
|
||||
events: list[dict] = []
|
||||
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
|
||||
],
|
||||
)
|
||||
events = [
|
||||
{
|
||||
"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()
|
||||
if events:
|
||||
try:
|
||||
emit_bulk(db, events)
|
||||
db.commit()
|
||||
except Exception: # noqa: BLE001 - 时间线不能回滚已经提交的任务
|
||||
db.rollback()
|
||||
logger.exception("批量写入斗鱼任务创建事件失败")
|
||||
return batch_id, len(accounts)
|
||||
|
||||
|
||||
@@ -424,39 +465,73 @@ def cleanup_orphan_douyu_tasks(
|
||||
if not tasks:
|
||||
return 0
|
||||
now = datetime.now(UTC)
|
||||
from .business_events import record_douyu_task_result
|
||||
|
||||
account_ids = {int(task.account_id) for task in tasks if task.account_id}
|
||||
accounts = (
|
||||
{
|
||||
account.id: account
|
||||
for account in db.query(Account).filter(Account.id.in_(account_ids)).all()
|
||||
}
|
||||
if account_ids
|
||||
else {}
|
||||
)
|
||||
event_contexts: list[tuple[dict, Account]] = []
|
||||
platform_task_ids: list[int] = []
|
||||
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 = accounts.get(int(task.account_id)) if task.account_id else None
|
||||
if account is not None:
|
||||
event_contexts.append(
|
||||
(
|
||||
{
|
||||
"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 ""),
|
||||
},
|
||||
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,
|
||||
)
|
||||
)
|
||||
if task.platform_task_id:
|
||||
from .platform_fulfillment.service import record_douyu_operation_result
|
||||
platform_task_ids.append(int(task.id))
|
||||
# 先提交任务终态,再写账号时间线,避免 account_events 与 douyu_tasks
|
||||
# 在并发创建和结束任务时形成反向锁依赖。
|
||||
db.commit()
|
||||
|
||||
from .business_events import record_douyu_task_result
|
||||
|
||||
for context, account in event_contexts:
|
||||
record_douyu_task_result(
|
||||
db,
|
||||
account,
|
||||
task_id=context["task_id"],
|
||||
task_type=context["task_type"],
|
||||
task_label=context["task_label"],
|
||||
scope=context["scope"],
|
||||
status="stopped",
|
||||
message=message,
|
||||
occurred_at=now,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
if platform_task_ids:
|
||||
from .platform_fulfillment.service import record_douyu_operation_result
|
||||
|
||||
for task_id in platform_task_ids:
|
||||
task = db.get(DouyuTask, task_id)
|
||||
if task is None:
|
||||
continue
|
||||
record_douyu_operation_result(
|
||||
db,
|
||||
task,
|
||||
status="stopped",
|
||||
message=message,
|
||||
)
|
||||
db.commit()
|
||||
db.commit()
|
||||
return len(tasks)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user