fix(knowledge): list_collections 增加异常保护与元数据重载

1. 遍历集合时 try/except 捕获 ChromaDB 异常(NotFoundError/InternalError),
   跳过失效集合并自动清理元数据,防止 /collections 500
2. 每次调用从磁盘重载 kb_metadata.json,确保多 worker 进程间状态一致,
   修复 GUNICORN_WORKERS>1 时返回数量波动问题
This commit is contained in:
lacerate551
2026-06-09 13:32:51 +08:00
parent db887d2215
commit b923e63bcd

View File

@@ -348,6 +348,8 @@ class CollectionMixin:
- department: 所属部门
- description: 描述
"""
# 从磁盘重新加载元数据,确保多 worker 进程间状态一致
self._metadata = self._load_metadata()
result = []
# 扫描 base_path 下的所有子目录作为向量库
@@ -382,16 +384,31 @@ class CollectionMixin:
self._save_metadata()
stale_collections = []
for name, info in self._metadata.get("collections", {}).items():
collection = self.get_collection(name)
result.append(CollectionInfo(
name=name,
display_name=info.get("display_name", name),
document_count=collection.count() if collection else 0,
created_at=info.get("created_at", ""),
department=info.get("department", ""),
description=info.get("description", "")
))
try:
collection = self.get_collection(name)
result.append(CollectionInfo(
name=name,
display_name=info.get("display_name", name),
document_count=collection.count() if collection else 0,
created_at=info.get("created_at", ""),
department=info.get("department", ""),
description=info.get("description", "")
))
except Exception as e:
logger.warning(
f"跳过异常向量库 '{name}': {e},可能是 ChromaDB 数据目录已丢失"
)
stale_collections.append(name)
# 清理元数据中指向已失效集合的条目
if stale_collections:
for name in stale_collections:
self._metadata.get("collections", {}).pop(name, None)
logger.info(f"清理失效向量库元数据: {name}")
self._save_metadata()
return result