7a8809ce by 柴进

增加定时重建的相关代码

1 parent 02df9569
......@@ -90,6 +90,60 @@ def scheduled_sync():
logger.error(f"❌ 定时同步失败: {e}", exc_info=True)
def check_faiss_index_update():
"""检查FAISS索引文件是否有更新,必要时重载"""
try:
import os
index_path = config['faiss']['index_path']
mapping_path = config['faiss']['mapping_path']
tombstone_path = config['faiss']['tombstone_path']
# 检查所有相关文件是否存在
if not all(os.path.exists(p) for p in [index_path, mapping_path, tombstone_path]):
return
# 获取文件修改时间
current_mtime = max(
os.path.getmtime(index_path),
os.path.getmtime(mapping_path),
os.path.getmtime(tombstone_path)
)
# 初始化 FAISS 管理器
faiss_manager = FAISSManager(
index_path=config['faiss']['index_path'],
mapping_path=config['faiss']['mapping_path'],
tombstone_path=config['faiss']['tombstone_path'],
vector_dim=config['faiss']['vector_dim']
)
# 初始化或检查上次加载时间
if not hasattr(check_faiss_index_update, 'last_mtime'):
check_faiss_index_update.last_mtime = current_mtime
return
# 如果文件有更新,重载索引
if current_mtime > check_faiss_index_update.last_mtime:
logger.info("检测到FAISS索引文件更新,重新加载...")
# 获取重载前的统计信息
old_stats = faiss_manager.get_stats()
if faiss_manager.load_index():
check_faiss_index_update.last_mtime = current_mtime
new_stats = faiss_manager.get_stats()
logger.info("✅ FAISS索引重载成功")
logger.info(f" 向量数: {old_stats['total_vectors']} -> {new_stats['total_vectors']}")
logger.info(f" 墓碑数: {old_stats['tombstone_count']} -> {new_stats['tombstone_count']}")
logger.info(f" 有效向量: {old_stats['effective_vectors']} -> {new_stats['effective_vectors']}")
else:
logger.error("❌ FAISS索引重载失败")
except Exception as e:
logger.error(f"检查FAISS索引更新失败: {e}", exc_info=True)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
......@@ -155,8 +209,17 @@ async def lifespan(app: FastAPI):
id='scheduled_sync',
replace_existing=True
)
# 添加 FAISS 索引检查任务(每5分钟检查一次)
scheduler.add_job(
func=check_faiss_index_update,
trigger=CronTrigger(minute='*/5'),
id='check_faiss_update',
replace_existing=True
)
scheduler.start()
logger.info("定时任务已启动(每天 0:00 和 12:00 执行)")
logger.info("定时任务已启动(每天 0:00 和 12:00 执行,每5分钟检查FAISS索引更新)")
logger.info("Design Image Search 服务启动完成")
......
......@@ -416,7 +416,13 @@ class FAISSManager:
db_path = db_manager
logger.info("开始重建索引(清理墓碑)...")
return self.compact_index(db_path)
result = self.compact_index(db_path)
if result:
logger.info("索引重建完成,内存状态已更新")
# 提示:如果是多进程架构,其他进程需要调用 load_index() 来获取最新索引
return result
def get_stats(self):
"""获取索引统计信息"""
......
......@@ -98,6 +98,7 @@ class DesignDataSync:
SELECT id, design_no, images, utc_modified
FROM saas_design.design
WHERE eps_id = 2
AND DELETE_KEY = 0
AND utc_modified > %s
ORDER BY utc_modified ASC
LIMIT %s
......