Przeglądaj źródła

fix(mode_workflow): _next_query_id 加内存预留,批量发起搜索不再重号(行未落库前)

刘文武 6 dni temu
rodzic
commit
86b10a195d
1 zmienionych plików z 14 dodań i 4 usunięć
  1. 14 4
      examples/mode_workflow/server.py

+ 14 - 4
examples/mode_workflow/server.py

@@ -171,11 +171,21 @@ def _task_status(task_id):
     return {"status": t["status"], "log_tail": tail}
 
 
+_ALLOCATED_QIDS = set()       # 已分配但行可能尚未落库的 query_id(异步搜索:POST 即返回,行几分钟后才写)
+_QID_LOCK = threading.Lock()
+
+
 def _next_query_id():
-    """两张搜索表统一编号,避免跨方向撞 ID。"""
-    qs = [q["query_id"] for m in ("process", "tools") for q in db.fetch_queries(m)]
-    nums = [int(q[1:]) for q in qs if q.startswith("q") and q[1:].isdigit()]
-    return f"q{(max(nums) + 1 if nums else 0):04d}"
+    """两张搜索表统一编号,避免跨方向撞 ID。
+    叠加内存预留集:连续发起搜索(如「搜全部达标」批量)时,前一次的行还没落库,
+    仅靠 DB max 会重号;故把已分配号也计入,保证连续分配不撞。"""
+    with _QID_LOCK:
+        qs = [q["query_id"] for m in ("process", "tools") for q in db.fetch_queries(m)]
+        nums = [int(q[1:]) for q in qs if q.startswith("q") and q[1:].isdigit()]
+        nums += [int(q[1:]) for q in _ALLOCATED_QIDS if q[1:].isdigit()]
+        qid = f"q{(max(nums) + 1 if nums else 0):04d}"
+        _ALLOCATED_QIDS.add(qid)
+        return qid
 
 
 # ── Dashboard 聚合 ────────────────────────────────────────────────────────────