优化(直播履约): 即时选号并收敛订单快照同步
This commit is contained in:
@@ -4,10 +4,12 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import threading
|
||||
from collections.abc import Iterable
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...database import SessionLocal
|
||||
@@ -21,6 +23,9 @@ DEFAULT_RETRY_SECONDS = 45
|
||||
_stop_event = threading.Event()
|
||||
_thread: threading.Thread | None = None
|
||||
_lock = threading.Lock()
|
||||
_pending_lock = threading.Lock()
|
||||
_pending_task_ids: set[int] = set()
|
||||
_wake_event = threading.Event()
|
||||
|
||||
|
||||
def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
|
||||
@@ -31,13 +36,50 @@ def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
|
||||
return max(minimum, min(maximum, value))
|
||||
|
||||
|
||||
def wake_auto_allocator(task_ids: Iterable[int] | None = None) -> None:
|
||||
"""立即唤醒选号 worker;传入任务 ID 时优先处理这些新任务。"""
|
||||
normalized = {int(task_id) for task_id in (task_ids or []) if int(task_id) > 0}
|
||||
if normalized:
|
||||
with _pending_lock:
|
||||
_pending_task_ids.update(normalized)
|
||||
_wake_event.set()
|
||||
|
||||
|
||||
def _take_pending_task_ids(limit: int) -> list[int]:
|
||||
with _pending_lock:
|
||||
selected = sorted(_pending_task_ids)[:limit]
|
||||
for task_id in selected:
|
||||
_pending_task_ids.discard(task_id)
|
||||
return selected
|
||||
|
||||
|
||||
def _has_pending_task_ids() -> bool:
|
||||
with _pending_lock:
|
||||
return bool(_pending_task_ids)
|
||||
|
||||
|
||||
def _schedule_retry(
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
moment: datetime,
|
||||
retry_seconds: int,
|
||||
) -> None:
|
||||
task = db.get(PlatformFulfillmentTask, int(task_id))
|
||||
if task is None or task.execution_status in {"completed", "cancelled"}:
|
||||
return
|
||||
task.next_allocate_at = moment + timedelta(seconds=retry_seconds)
|
||||
db.commit()
|
||||
|
||||
|
||||
def run_auto_allocation_once(
|
||||
db: Session,
|
||||
*,
|
||||
limit: int | None = None,
|
||||
now: datetime | None = None,
|
||||
task_ids: list[int] | None = None,
|
||||
) -> dict[str, int]:
|
||||
"""扫描 ready 且尚未分配账号的任务,逐笔调用统一选号逻辑。"""
|
||||
"""领取已到期的 ready 任务;新任务不再等待固定 45 秒。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
batch_size = limit or _env_int(
|
||||
"PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH",
|
||||
@@ -51,18 +93,34 @@ def run_auto_allocation_once(
|
||||
10,
|
||||
3600,
|
||||
)
|
||||
# MySQL DATETIME 默认只保存到秒,写入时会按微秒四舍五入;
|
||||
# 给 1 秒容差,避免刚写入的 next_allocate_at 被 worker 当成“未来任务”。
|
||||
due_cutoff = moment + timedelta(seconds=1)
|
||||
query = db.query(PlatformFulfillmentTask).filter(
|
||||
PlatformFulfillmentTask.execution_status == "ready",
|
||||
PlatformFulfillmentTask.account_id.is_(None),
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
PlatformFulfillmentTask.updated_at <= moment - timedelta(seconds=retry_seconds),
|
||||
or_(
|
||||
PlatformFulfillmentTask.next_allocate_at.is_(None),
|
||||
PlatformFulfillmentTask.next_allocate_at <= due_cutoff,
|
||||
),
|
||||
)
|
||||
normalized_ids = [
|
||||
int(task_id) for task_id in dict.fromkeys(task_ids or []) if int(task_id) > 0
|
||||
]
|
||||
if normalized_ids:
|
||||
query = query.filter(PlatformFulfillmentTask.id.in_(normalized_ids))
|
||||
query = service._apply_order_match_cutoff(
|
||||
query,
|
||||
service._order_match_started_at(db),
|
||||
)
|
||||
candidates = (
|
||||
query.order_by(PlatformFulfillmentTask.updated_at.asc()).limit(batch_size).all()
|
||||
query.order_by(
|
||||
PlatformFulfillmentTask.next_allocate_at.asc(),
|
||||
PlatformFulfillmentTask.id.asc(),
|
||||
)
|
||||
.limit(batch_size)
|
||||
.all()
|
||||
)
|
||||
if not candidates:
|
||||
return {"checked": 0, "allocated": 0, "failed": 0}
|
||||
@@ -71,20 +129,41 @@ def run_auto_allocation_once(
|
||||
allocated = 0
|
||||
failed = 0
|
||||
for task in candidates:
|
||||
candidate_task_id = int(task.id)
|
||||
try:
|
||||
detail: dict[str, Any] = service.allocate_task(
|
||||
db,
|
||||
task_id=int(task.id),
|
||||
task_id=candidate_task_id,
|
||||
current=actor,
|
||||
)
|
||||
if detail.get("task", {}).get("account"):
|
||||
allocated += 1
|
||||
allocated_task = db.get(PlatformFulfillmentTask, candidate_task_id)
|
||||
if allocated_task is not None:
|
||||
allocated_task.next_allocate_at = None
|
||||
db.commit()
|
||||
else:
|
||||
failed += 1
|
||||
_schedule_retry(
|
||||
db,
|
||||
task_id=candidate_task_id,
|
||||
moment=moment,
|
||||
retry_seconds=retry_seconds,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 单个任务异常不能中断整批
|
||||
db.rollback()
|
||||
failed += 1
|
||||
logger.warning("直播账号履约自动选号失败 task_id={}: {}", task.id, exc)
|
||||
_schedule_retry(
|
||||
db,
|
||||
task_id=candidate_task_id,
|
||||
moment=moment,
|
||||
retry_seconds=retry_seconds,
|
||||
)
|
||||
logger.warning(
|
||||
"直播账号履约自动选号失败 task_id={}: {}",
|
||||
candidate_task_id,
|
||||
exc,
|
||||
)
|
||||
return {
|
||||
"checked": len(candidates),
|
||||
"allocated": allocated,
|
||||
@@ -99,10 +178,18 @@ def _run() -> None:
|
||||
5,
|
||||
3600,
|
||||
)
|
||||
while not _stop_event.wait(interval):
|
||||
while not _stop_event.is_set():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
result = run_auto_allocation_once(db)
|
||||
pending_ids = _take_pending_task_ids(
|
||||
_env_int(
|
||||
"PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH",
|
||||
DEFAULT_BATCH_SIZE,
|
||||
1,
|
||||
20,
|
||||
)
|
||||
)
|
||||
result = run_auto_allocation_once(db, task_ids=pending_ids or None)
|
||||
if result["checked"]:
|
||||
logger.info("直播账号履约自动选号: {}", result)
|
||||
except Exception as exc: # noqa: BLE001 - worker 不能因单轮异常退出
|
||||
@@ -110,6 +197,12 @@ def _run() -> None:
|
||||
logger.warning("直播账号履约自动选号轮次失败: {}", exc)
|
||||
finally:
|
||||
db.close()
|
||||
if _stop_event.is_set():
|
||||
break
|
||||
if _has_pending_task_ids():
|
||||
continue
|
||||
_wake_event.wait(interval)
|
||||
_wake_event.clear()
|
||||
|
||||
|
||||
def start_platform_fulfillment_auto_allocator() -> bool:
|
||||
@@ -118,6 +211,9 @@ def start_platform_fulfillment_auto_allocator() -> bool:
|
||||
if _thread is not None and _thread.is_alive():
|
||||
return False
|
||||
_stop_event.clear()
|
||||
_wake_event.clear()
|
||||
with _pending_lock:
|
||||
_pending_task_ids.clear()
|
||||
_thread = threading.Thread(
|
||||
target=_run,
|
||||
name="platform-fulfillment-auto-allocator",
|
||||
@@ -130,6 +226,7 @@ def start_platform_fulfillment_auto_allocator() -> bool:
|
||||
def stop_platform_fulfillment_auto_allocator() -> None:
|
||||
global _thread
|
||||
_stop_event.set()
|
||||
_wake_event.set()
|
||||
with _lock:
|
||||
thread = _thread
|
||||
_thread = None
|
||||
|
||||
Reference in New Issue
Block a user