提交 3cb1cf62 authored 作者: 陈泽健's avatar 陈泽健

fix(service-monitor): 修复定时任务并发执行导致 run_id 冲突

问题:多个定时任务在同一秒触发时(如都在 09:00),
使用相同的 run_id(时间戳+"sched"),导致:
- 远程工作目录冲突
- _runs 字典冲突
- 后执行的覆盖前一个,只生成一份报告

修复:
- run_inspection_sync: run_id 加入 target_id 前缀 + uuid
- run_inspection: run_id 加入 uuid 替代 threading.get_ident()

影响:
- 定时任务可安全并发执行
- SSE 巡检也获得更强的唯一性保证

测试:167 个测试全绿
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
上级 7b0e7dcd
...@@ -20,6 +20,7 @@ import threading ...@@ -20,6 +20,7 @@ import threading
import time import time
import traceback import traceback
import urllib.parse import urllib.parse
import uuid
from datetime import datetime from datetime import datetime
from typing import Dict, Optional from typing import Dict, Optional
...@@ -122,7 +123,8 @@ def run_inspection(target_id: str, suite: str): ...@@ -122,7 +123,8 @@ def run_inspection(target_id: str, suite: str):
{"event": "cancelled", "run_id"} {"event": "cancelled", "run_id"}
""" """
from datetime import datetime as _dt from datetime import datetime as _dt
run_id = _dt.now().strftime("%Y%m%d_%H%M%S_") + str(threading.get_ident())[-4:] # 使用 uuid 避免 run_id 冲突(同一秒多请求)
run_id = _dt.now().strftime("%Y%m%d_%H%M%S_") + str(uuid.uuid4())[:6]
target = target_service.get_target(target_id) target = target_service.get_target(target_id)
if not target: if not target:
...@@ -226,7 +228,8 @@ def run_inspection_sync(target_id: str, suite: str) -> dict: ...@@ -226,7 +228,8 @@ def run_inspection_sync(target_id: str, suite: str) -> dict:
返回: {"success": bool, "report_id": str, "summary": dict, "finished_at": str} 返回: {"success": bool, "report_id": str, "summary": dict, "finished_at": str}
""" """
from datetime import datetime as _dt from datetime import datetime as _dt
run_id = _dt.now().strftime("%Y%m%d_%H%M%S_") + "sched" # 使用 target_id + uuid 避免 run_id 冲突(多个定时任务同一秒触发)
run_id = _dt.now().strftime("%Y%m%d_%H%M%S_") + f"{target_id[:4]}_" + str(uuid.uuid4())[:6]
target = target_service.get_target(target_id) target = target_service.get_target(target_id)
if not target: if not target:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论