refactor(core): 统一和改进程序优雅关闭逻辑

将分散的关闭逻辑集中到`MainSystem`中,并由`bot.py`中的顶层异常处理块统一调用。这简化了关闭流程,提高了系统的健壮性和可维护性。

- 将信号处理逻辑从`MainSystem`移除,改由`bot.py`中的`try...finally`块处理,以捕获更广泛的退出场景(如`KeyboardInterrupt`)。
- `graceful_shutdown`函数现在接收`main_system`实例,直接调用其`shutdown`方法,实现了责任的单一化。
- 为`EmojiManager`和`VectorMemoryStorage`添加了`shutdown`/`cleanup`方法,确保其后台任务和资源能被正确清理。
- 调整了`MemorySystem`中对`unified_storage.cleanup()`的调用,使其与接口保持一致。
This commit is contained in:
minecraft1024a
2025-10-04 16:47:55 +08:00
committed by Windpicker-owo
parent 9175207e79
commit a273be76d3
5 changed files with 32 additions and 205 deletions

View File

@@ -402,6 +402,12 @@ class EmojiManager:
logger.info("启动表情包管理器")
def shutdown(self) -> None:
"""关闭EmojiManager取消正在运行的任务"""
if self._scan_task and not self._scan_task.done():
self._scan_task.cancel()
logger.info("表情包扫描任务已取消")
def initialize(self) -> None:
"""初始化数据库连接和表情目录"""

View File

@@ -1425,16 +1425,6 @@ class MemorySystem:
def _fingerprint_key(user_id: str, fingerprint: str) -> str:
return f"{user_id!s}:{fingerprint}"
def get_system_stats(self) -> dict[str, Any]:
"""获取系统统计信息"""
return {
"status": self.status.value,
"total_memories": self.total_memories,
"last_build_time": self.last_build_time,
"last_retrieval_time": self.last_retrieval_time,
"config": asdict(self.config),
}
def _compute_memory_score(self, query_text: str, memory: MemoryChunk, context: dict[str, Any]) -> float:
"""根据查询和上下文为记忆计算匹配分数"""
tokens_query = self._tokenize_text(query_text)
@@ -1542,7 +1532,7 @@ class MemorySystem:
# 保存统一存储数据
if self.unified_storage:
await self.unified_storage.cleanup()
self.unified_storage.cleanup()
logger.info("✅ 简化记忆系统已关闭")

View File

@@ -964,6 +964,11 @@ class VectorMemoryStorage:
logger.info("Vector记忆存储系统已停止")
def cleanup(self):
"""清理资源,兼容旧接口"""
logger.info("正在清理VectorMemoryStorage资源...")
self.stop()
# 全局实例(可选)
_global_vector_storage = None