2026-01-21 23:37:00 +01:00
|
|
|
|
"""
|
2026-03-18 17:18:23 +08:00
|
|
|
|
任务追踪服务:追踪 Celery 任务状态(从 services 迁入 core)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
"""
|
2026-03-19 14:36:14 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
import json
|
|
|
|
|
|
from datetime import datetime, timezone
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from typing import Any, Dict, List
|
2026-01-21 23:37:00 +01:00
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.core.logging import get_logger
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.core.redis import redis_service
|
2026-05-22 13:44:50 +08:00
|
|
|
|
from app.core.runtime_constants import redis_defaults
|
2026-01-21 23:37:00 +01:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
logger = get_logger(__name__)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TaskTracker:
|
|
|
|
|
|
"""任务追踪器,使用 Redis 存储任务状态"""
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
KEY_PREFIX = "task:user:"
|
2026-05-22 13:44:50 +08:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def task_ttl(self) -> int:
|
|
|
|
|
|
return int(redis_defaults.task_tracker_ttl_seconds)
|
|
|
|
|
|
|
|
|
|
|
|
async def _refresh_key_ttl(self, key: str) -> None:
|
|
|
|
|
|
client = await redis_service.get_client()
|
|
|
|
|
|
await client.expire(key, self.task_ttl)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-03-19 14:36:14 +08:00
|
|
|
|
async def add_task(
|
|
|
|
|
|
self, user_id: str, task_id: str, task_type: str = "memoir"
|
|
|
|
|
|
) -> bool:
|
2026-01-21 23:37:00 +01:00
|
|
|
|
try:
|
|
|
|
|
|
client = await redis_service.get_client()
|
|
|
|
|
|
key = f"{self.KEY_PREFIX}{user_id}:tasks"
|
|
|
|
|
|
task_info = {
|
|
|
|
|
|
"task_id": task_id,
|
|
|
|
|
|
"task_type": task_type,
|
|
|
|
|
|
"status": "pending",
|
|
|
|
|
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
|
|
|
|
}
|
|
|
|
|
|
await client.hset(key, task_id, json.dumps(task_info))
|
2026-05-22 13:44:50 +08:00
|
|
|
|
await client.expire(key, self.task_ttl)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.debug("任务已记录: user_id={}, task_id={}", user_id, task_id)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("记录任务失败: {}", e)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return False
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
async def update_task_status(
|
|
|
|
|
|
self, user_id: str, task_id: str, status: str, result: Any = None
|
|
|
|
|
|
) -> bool:
|
2026-01-21 23:37:00 +01:00
|
|
|
|
try:
|
|
|
|
|
|
client = await redis_service.get_client()
|
|
|
|
|
|
key = f"{self.KEY_PREFIX}{user_id}:tasks"
|
|
|
|
|
|
data = await client.hget(key, task_id)
|
|
|
|
|
|
if data:
|
|
|
|
|
|
task_info = json.loads(data)
|
|
|
|
|
|
else:
|
|
|
|
|
|
task_info = {"task_id": task_id}
|
|
|
|
|
|
task_info["status"] = status
|
|
|
|
|
|
task_info["updated_at"] = datetime.now(timezone.utc).isoformat()
|
|
|
|
|
|
if result is not None:
|
|
|
|
|
|
task_info["result"] = result
|
|
|
|
|
|
await client.hset(key, task_id, json.dumps(task_info))
|
2026-05-22 13:44:50 +08:00
|
|
|
|
await self._refresh_key_ttl(key)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("更新任务状态失败: {}", e)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return False
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
async def get_user_tasks(self, user_id: str) -> List[Dict]:
|
|
|
|
|
|
try:
|
|
|
|
|
|
client = await redis_service.get_client()
|
|
|
|
|
|
key = f"{self.KEY_PREFIX}{user_id}:tasks"
|
|
|
|
|
|
tasks_data = await client.hgetall(key)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
return [json.loads(data) for data in tasks_data.values()]
|
2026-01-21 23:37:00 +01:00
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("获取用户任务失败: {}", e)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return []
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
async def get_pending_tasks(self, user_id: str) -> List[Dict]:
|
|
|
|
|
|
tasks = await self.get_user_tasks(user_id)
|
|
|
|
|
|
return [t for t in tasks if t.get("status") in ("pending", "running")]
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
async def check_tasks_status(self, user_id: str) -> Dict:
|
|
|
|
|
|
tasks = await self.get_user_tasks(user_id)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
status_counts: Dict[str, int] = {
|
2026-01-21 23:37:00 +01:00
|
|
|
|
"total": len(tasks),
|
|
|
|
|
|
"pending": 0,
|
|
|
|
|
|
"running": 0,
|
|
|
|
|
|
"success": 0,
|
|
|
|
|
|
"failure": 0,
|
|
|
|
|
|
}
|
|
|
|
|
|
for task in tasks:
|
|
|
|
|
|
status = task.get("status", "pending")
|
|
|
|
|
|
if status in status_counts:
|
|
|
|
|
|
status_counts[status] += 1
|
|
|
|
|
|
status_counts["all_completed"] = (
|
2026-03-18 17:18:23 +08:00
|
|
|
|
status_counts["total"] > 0
|
|
|
|
|
|
and status_counts["pending"] == 0
|
|
|
|
|
|
and status_counts["running"] == 0
|
2026-01-21 23:37:00 +01:00
|
|
|
|
)
|
|
|
|
|
|
return status_counts
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
async def clear_user_tasks(self, user_id: str) -> bool:
|
|
|
|
|
|
try:
|
|
|
|
|
|
client = await redis_service.get_client()
|
|
|
|
|
|
key = f"{self.KEY_PREFIX}{user_id}:tasks"
|
|
|
|
|
|
await client.delete(key)
|
|
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("清除用户任务失败: {}", e)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return False
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-01-21 23:37:00 +01:00
|
|
|
|
async def remove_task(self, user_id: str, task_id: str) -> bool:
|
|
|
|
|
|
try:
|
|
|
|
|
|
client = await redis_service.get_client()
|
|
|
|
|
|
key = f"{self.KEY_PREFIX}{user_id}:tasks"
|
|
|
|
|
|
await client.hdel(key, task_id)
|
2026-05-22 13:44:50 +08:00
|
|
|
|
if await client.exists(key):
|
|
|
|
|
|
await self._refresh_key_ttl(key)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.error("移除任务失败: {}", e)
|
2026-01-21 23:37:00 +01:00
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
task_tracker = TaskTracker()
|