feat: 业务库切换 MySQL + 修复定时任务运行详情递归与重名 500
- 业务 ORM 数据支持 MySQL 后端(asyncmy),新增 PortableJSON/
BeijingDateTime 可移植列类型、迁移脚本与切库脚本
- 修复 GET /api/scheduled-tasks/runs/{id} 的 _load_run 无限递归
(误调用自身,应委托 service.get_run)导致的 RecursionError 500
- 定时任务名按用户唯一:create/update/admin_update 主动校验,
冲突抛 DuplicateTaskNameError,路由转 409 而非裸 IntegrityError 500
- 补充回归测试 test_scheduled_task_run_lookup / _name_uniqueness
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
d6f018e581
commit
358d5c72a6
40 files changed
+2128
-190
No files matched your search
+38
-1
@@ -36,6 +36,19 @@ _RUN_ATTEMPT_TIMEOUT_SECONDS = 30 * 60 # 30 minutes
|
||||
_MAX_RUN_RETRIES = 3
|
||||
|
||||
|
||||
class DuplicateTaskNameError(Exception):
|
||||
"""A user already owns a scheduled task with the requested name.
|
||||
|
||||
Task names are unique per user (``uq_scheduled_tasks_user_name``). The
|
||||
service raises this instead of letting the raw DB ``IntegrityError`` bubble
|
||||
up so the API layer can return a friendly 409 rather than a 500.
|
||||
"""
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
self.name = name
|
||||
super().__init__(f"A scheduled task named {name!r} already exists")
|
||||
|
||||
|
||||
@dataclass
|
||||
class _RuntimeUser:
|
||||
id: str
|
||||
@@ -366,6 +379,20 @@ class ScheduledTaskService:
|
||||
async def update_delivery_profile(self, user_id: str, data: dict[str, Any]) -> dict[str, Any]:
|
||||
return await self.store.update_delivery_profile(user_id, data)
|
||||
|
||||
async def _assert_name_available(self, user_id: str, name: str, *, exclude_task_id: str | None = None) -> None:
|
||||
"""Raise :class:`DuplicateTaskNameError` if ``user_id`` already owns ``name``.
|
||||
|
||||
Enforces ``uq_scheduled_tasks_user_name`` proactively so callers get a
|
||||
clean conflict error instead of a raw DB ``IntegrityError``. The check
|
||||
compares the trimmed name; ``exclude_task_id`` skips the task being
|
||||
renamed so an unchanged name on update is not flagged as a collision.
|
||||
"""
|
||||
for existing in await self.store.list_tasks(user_id):
|
||||
if exclude_task_id is not None and existing.get("task_id") == exclude_task_id:
|
||||
continue
|
||||
if (existing.get("name") or "").strip() == name:
|
||||
raise DuplicateTaskNameError(name)
|
||||
|
||||
async def create_task(
|
||||
self,
|
||||
*,
|
||||
@@ -382,6 +409,8 @@ class ScheduledTaskService:
|
||||
tz = _normalize_timezone(timezone)
|
||||
cron_expr = _schedule_to_cron(schedule)
|
||||
next_run_at = _next_run_at(cron_expr, tz)
|
||||
final_name = name.strip() or "Scheduled task"
|
||||
await self._assert_name_available(user_id, final_name)
|
||||
scheduler_thread = await self.ensure_scheduler_thread(user_id)
|
||||
context = dict(execution_context or {})
|
||||
if execution_agent_name:
|
||||
@@ -394,7 +423,7 @@ class ScheduledTaskService:
|
||||
"source_thread_id": source_thread_id,
|
||||
"source_agent_name": source_agent_name,
|
||||
"execution_agent_name": execution_agent_name,
|
||||
"name": name.strip() or "Scheduled task",
|
||||
"name": final_name,
|
||||
"prompt": prompt.strip(),
|
||||
"schedule_text": schedule.strip(),
|
||||
"cron_expr": cron_expr,
|
||||
@@ -417,6 +446,10 @@ class ScheduledTaskService:
|
||||
for key in ("name", "prompt", "enabled", "execution_agent_name"):
|
||||
if key in data and data[key] is not None:
|
||||
update_data[key] = data[key]
|
||||
# Names are matched against the per-user uniqueness constraint, so keep
|
||||
# the stored value trimmed to match create_task's normalization.
|
||||
if isinstance(update_data.get("name"), str):
|
||||
update_data["name"] = update_data["name"].strip() or "Scheduled task"
|
||||
timezone = data.get("timezone")
|
||||
if timezone is not None:
|
||||
update_data["timezone"] = _normalize_timezone(timezone)
|
||||
@@ -443,6 +476,8 @@ class ScheduledTaskService:
|
||||
if existing is None:
|
||||
return None
|
||||
update_data = self._build_task_update(data, existing)
|
||||
if "name" in update_data:
|
||||
await self._assert_name_available(user_id, update_data["name"], exclude_task_id=task_id)
|
||||
return await self.store.update_task(task_id, user_id, update_data)
|
||||
|
||||
async def delete_task(self, user_id: str, task_id: str) -> bool:
|
||||
@@ -463,6 +498,8 @@ class ScheduledTaskService:
|
||||
if existing is None:
|
||||
return None
|
||||
update_data = self._build_task_update(data, existing)
|
||||
if "name" in update_data:
|
||||
await self._assert_name_available(existing["user_id"], update_data["name"], exclude_task_id=task_id)
|
||||
return await self.store.update_task_any(task_id, update_data)
|
||||
|
||||
async def admin_delete_task(self, task_id: str) -> bool:
|
||||
|
||||
Reference in new issue
Block a user