feat(plugin): support auto bot injection and file-based hot reload

This commit is contained in:
liuwei
2026-04-16 13:54:56 +08:00
parent 041a3f30d8
commit f0414e0dff
4 changed files with 330 additions and 18 deletions

View File

@@ -2,6 +2,8 @@ import importlib
import inspect
import os
import sys
import threading
import time
from typing import Dict, List, Any, Optional, Tuple
from loguru import logger
@@ -11,6 +13,7 @@ from base.plugin_common.message_plugin_interface import MessagePluginInterface
from base.plugin_common.scheduled_plugin_interface import ScheduledPluginInterface
from base.plugin_common.plugin_registry import PluginRegistry
from base.plugin_common.event_system import EventSystem, EventType
from utils.decorator.async_job import async_job
from wechat_ipad import WechatAPIClient
@@ -53,6 +56,14 @@ class PluginManager:
self.plugin_modules = {} # 插件模块字典键为module_name
self.module_to_display = {} # 模块名到显示名的映射
self.system_context = {} # 系统上下文
self.current_bot: Optional[WechatAPIClient] = None
# 热加载相关
self._watcher_thread: Optional[threading.Thread] = None
self._watcher_stop_event = threading.Event()
self._watcher_interval = 2.0
self._module_file_state: Dict[str, Dict[str, float]] = {}
self._watcher_lock = threading.RLock()
self.LOG = logger
# 确保插件目录存在
@@ -71,6 +82,139 @@ class PluginManager:
context: 系统上下文
"""
self.system_context = context
bot = context.get("bot")
if bot is not None:
self.current_bot = bot
def _build_module_file_state(self, module_name: str) -> Optional[Dict[str, float]]:
"""
构建模块的文件状态快照,用于检测文件变更
"""
plugin_folder = os.path.join(self.plugin_dir, module_name)
file_state: Dict[str, float] = {}
if os.path.isdir(plugin_folder):
for root, _, files in os.walk(plugin_folder):
for filename in files:
if not (filename.endswith(".py") or filename.endswith(".toml")):
continue
file_path = os.path.join(root, filename)
try:
file_state[os.path.abspath(file_path)] = os.path.getmtime(file_path)
except OSError:
continue
return file_state if file_state else None
single_file = os.path.join(self.plugin_dir, f"{module_name}.py")
if os.path.exists(single_file):
try:
file_state[os.path.abspath(single_file)] = os.path.getmtime(single_file)
except OSError:
return None
return file_state
return None
def _refresh_module_file_state(self, module_name: str):
state = self._build_module_file_state(module_name)
if state is None:
self._module_file_state.pop(module_name, None)
else:
self._module_file_state[module_name] = state
def _inject_bot_to_plugin(self, plugin: PluginInterface):
bot = self.current_bot or self.system_context.get("bot")
if not bot:
return
if hasattr(plugin, "set_bot"):
try:
plugin.set_bot(bot)
except Exception as e:
self.LOG.error(f"自动注入 bot 到插件 {plugin.name} 失败: {e}")
def start_hot_reload_watcher(self, interval_seconds: float = 2.0):
"""
启动插件目录变更监听线程(轮询)
"""
with self._watcher_lock:
if self._watcher_thread and self._watcher_thread.is_alive():
self.LOG.debug("PluginManager热加载监听线程已运行跳过重复启动")
return
self._watcher_interval = max(float(interval_seconds), 0.5)
self._watcher_stop_event.clear()
# 初始化快照
for module_name in self.discover_plugins():
self._refresh_module_file_state(module_name)
self._watcher_thread = threading.Thread(
target=self._hot_reload_watch_loop,
name="plugin-hot-reload-watcher",
daemon=True,
)
self._watcher_thread.start()
self.LOG.info(f"PluginManager插件热加载监听已启动轮询间隔 {self._watcher_interval}s")
def stop_hot_reload_watcher(self):
"""
停止插件目录变更监听线程
"""
with self._watcher_lock:
if not self._watcher_thread:
return
self._watcher_stop_event.set()
thread = self._watcher_thread
self._watcher_thread = None
if thread.is_alive():
thread.join(timeout=2.0)
self.LOG.info("PluginManager插件热加载监听已停止")
def _hot_reload_watch_loop(self):
while not self._watcher_stop_event.is_set():
try:
discovered = set(self.discover_plugins())
loaded_modules = set(self.module_to_display.keys())
# 1. 新增插件 -> 自动加载并启动
new_modules = discovered - loaded_modules
for module_name in new_modules:
plugin = self.load_plugin(module_name)
if plugin:
self.start_plugin(plugin.name)
self.LOG.info(f"PluginManager检测到新增插件 {module_name},已自动加载")
self._refresh_module_file_state(module_name)
# 2. 已删除插件 -> 自动卸载
removed_modules = loaded_modules - discovered
for module_name in removed_modules:
if self.unload_plugin(module_name):
self.LOG.info(f"PluginManager检测到插件 {module_name} 已删除,已自动卸载")
self._module_file_state.pop(module_name, None)
# 3. 文件变更 -> 自动重载
for module_name in list(self.module_to_display.keys()):
new_state = self._build_module_file_state(module_name)
old_state = self._module_file_state.get(module_name)
if new_state is None:
continue
if old_state is None:
self._module_file_state[module_name] = new_state
continue
if new_state != old_state:
reloaded = self.reload_plugin(module_name)
if reloaded:
self.LOG.info(f"PluginManager检测到插件 {module_name} 文件变更,已自动重载")
self._module_file_state[module_name] = new_state
else:
self.LOG.warning(f"PluginManager插件 {module_name} 自动重载失败")
except Exception as e:
self.LOG.error(f"PluginManager热加载监听异常: {e}", exc_info=True)
time.sleep(self._watcher_interval)
def discover_plugins(self) -> List[str]:
"""
@@ -207,6 +351,7 @@ class PluginManager:
if module_name not in self.module_to_display:
self.module_to_display[module_name] = display_name
self.LOG.debug(f"PluginManager添加缺失的模块映射 {module_name} -> {display_name}")
self._inject_bot_to_plugin(plugin)
return plugin
except Exception as e:
self.LOG.warning(f"获取插件 {display_name} 的模块名时出错: {e}")
@@ -258,12 +403,15 @@ class PluginManager:
# 加载插件配置
if not plugin.load_config():
self.LOG.error(f"PluginManager插件模块 {module_name} 加载配置失败")
async_job.remove_jobs_by_owner(plugin)
return None
# 初始化插件
if not plugin.initialize(self.system_context):
self.LOG.error(f"PluginManager插件模块 {module_name} 初始化失败")
async_job.remove_jobs_by_owner(plugin)
return None
self._inject_bot_to_plugin(plugin)
# 注册插件
PluginRegistry().register(plugin)
@@ -276,6 +424,7 @@ class PluginManager:
# 添加模块名到显示名的映射
self.module_to_display[module_name] = display_name
self._refresh_module_file_state(module_name)
# self.LOG.info(f"PluginManager添加模块映射 {module_name} -> {display_name}")
# 发布插件加载事件
@@ -299,18 +448,22 @@ class PluginManager:
# 加载插件配置
if not plugin.load_config():
self.LOG.error(f"PluginManager插件模块 {module_name} 加载配置失败")
async_job.remove_jobs_by_owner(plugin)
return None
# 修改检查enable状态的代码遍历所有配置节点
for section in plugin._config.values():
if isinstance(section, dict) and not section.get("enable", True):
self.LOG.debug(f"PluginManager插件 {module_name} 已禁用,跳过加载")
async_job.remove_jobs_by_owner(plugin)
return None
# 初始化插件
if not plugin.initialize(self.system_context):
self.LOG.error(f"PluginManager插件模块 {module_name} 初始化失败")
async_job.remove_jobs_by_owner(plugin)
return None
self._inject_bot_to_plugin(plugin)
# 注册插件
PluginRegistry().register(plugin)
@@ -323,6 +476,7 @@ class PluginManager:
# 添加模块名到显示名的映射
self.module_to_display[module_name] = display_name
self._refresh_module_file_state(module_name)
# self.LOG.info(f"PluginManager添加模块映射 {module_name} -> {display_name}")
# 发布插件加载事件
@@ -331,6 +485,9 @@ class PluginManager:
return plugin
except Exception as e:
plugin_obj = locals().get("plugin")
if plugin_obj is not None:
async_job.remove_jobs_by_owner(plugin_obj)
self.LOG.exception(f"PluginManager加载插件模块 {module_name} 失败: {e}", exc_info=True)
return None
@@ -358,6 +515,10 @@ class PluginManager:
return False
plugin.status = PluginStatus.STOPPED # 确保状态更新
removed_jobs = async_job.remove_jobs_by_owner(plugin)
if removed_jobs:
self.LOG.debug(f"PluginManager已移除插件 {display_name} 的定时任务 {removed_jobs}")
# 清理插件资源
if not plugin.cleanup():
self.LOG.debug(f"PluginManager清理插件 {display_name} 资源失败")
@@ -374,6 +535,7 @@ class PluginManager:
if module_name and module_name in self.module_to_display:
del self.module_to_display[module_name]
self.LOG.debug(f"PluginManager清理模块映射 {module_name} -> {display_name}")
self._module_file_state.pop(module_name, None)
# 移除插件实例
del self.plugins[display_name]
@@ -498,6 +660,7 @@ class PluginManager:
是否全部成功卸载
"""
success = True
self.stop_hot_reload_watcher()
# 创建插件名称的副本因为在卸载过程中会修改self.plugins字典
display_names = list(self.plugins.keys())
@@ -574,6 +737,7 @@ class PluginManager:
return None, None
def inject_bot(self, bot: WechatAPIClient):
self.current_bot = bot
self.system_context["bot"] = bot
for name, plugin in self.plugins.items():
# self.LOG.debug(f"plugin name{name}, plugin: {plugin}")