diff --git a/web/routers/tasks.py b/web/routers/tasks.py index e26b54b..a8beef5 100644 --- a/web/routers/tasks.py +++ b/web/routers/tasks.py @@ -12,6 +12,7 @@ 这些端点读写 core.state 的共享任务表(与任务执行侧同一批对象),因此 看到的是同一份任务状态。 """ +import asyncio import time from typing import Annotated, Any @@ -27,6 +28,7 @@ from core.state import ( REQUEST_STATS, STATS_LOCK, ) +from services import tasks as services_tasks from web import ratelimit from web.deps import SiteUser, require_login from web.request_utils import ( @@ -40,18 +42,6 @@ LoginUser = Annotated[SiteUser, Depends(require_login)] -def _touch_device(device_id: str) -> None: - """记录设备心跳(与 core.state 的 DEVICE_LAST_SEEN 共享)。""" - device_id = str(device_id or '')[:64] - if not device_id: - return - with DEVICE_LAST_SEEN_LOCK: - if len(DEVICE_LAST_SEEN) >= 5000: - for d in sorted(DEVICE_LAST_SEEN, key=lambda k: DEVICE_LAST_SEEN.get(k, 0.0))[:2500]: - DEVICE_LAST_SEEN.pop(d, None) - DEVICE_LAST_SEEN[device_id] = time.time() - - def _owned_by(task: dict[str, Any], user: SiteUser) -> bool: return int(task.get('user_id') or 0) == int(user.id) @@ -183,7 +173,7 @@ async def task_heartbeat(user: LoginUser, request: Request): device_id = str(data.get('device_id') or '').strip()[:64] if not device_id: return error('缺少 device_id 参数') - _touch_device(device_id) + await asyncio.to_thread(services_tasks._touch_device, device_id) resumed = 0 with DOWNLOAD_TASKS_LOCK: for task in DOWNLOAD_TASKS.values(): @@ -209,7 +199,7 @@ async def task_list(user: LoginUser, request: Request): device_id = str(data.get('device_id') or '').strip()[:64] if not device_id: return error('缺少 device_id 参数') - _touch_device(device_id) + await asyncio.to_thread(services_tasks._touch_device, device_id) my_tasks = [] with DOWNLOAD_TASKS_LOCK: for tid, task in DOWNLOAD_TASKS.items():