From da4dd63fd3c331bda6eca065b1d536862034d62e Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Mon, 1 Sep 2025 11:38:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugin=5Fsystem):=20=E5=AE=9E=E7=8E=B0=20o?= =?UTF-8?q?n=5Fplugin=5Floaded=20=E5=BC=82=E6=AD=A5=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在插件成功加载和注册后,添加一个新的生命周期钩子 `on_plugin_loaded`。 此钩子允许插件在加载完成后执行异步的初始化任务,例如建立数据库连接、加载数据或启动后台服务。通过使用 `asyncio.create_task`,确保了钩子的执行不会阻塞插件的加载流程,从而提高了系统的响应性和健壮性。 --- src/plugin_system/core/plugin_manager.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/plugin_system/core/plugin_manager.py b/src/plugin_system/core/plugin_manager.py index dd15ca5a3..2794e2fc5 100644 --- a/src/plugin_system/core/plugin_manager.py +++ b/src/plugin_system/core/plugin_manager.py @@ -197,6 +197,18 @@ class PluginManager: if plugin_instance.register_plugin(): self.loaded_plugins[plugin_name] = plugin_instance self._show_plugin_components(plugin_name) + + # 检查并调用 on_plugin_loaded 钩子(如果存在) + if hasattr(plugin_instance, "on_plugin_loaded") and callable( + getattr(plugin_instance, "on_plugin_loaded") + ): + logger.debug(f"为插件 '{plugin_name}' 调用 on_plugin_loaded 钩子") + try: + # 使用 asyncio.create_task 确保它不会阻塞加载流程 + asyncio.create_task(plugin_instance.on_plugin_loaded()) + except Exception as e: + logger.error(f"调用插件 '{plugin_name}' 的 on_plugin_loaded 钩子时出错: {e}") + return True, 1 else: self.failed_plugins[plugin_name] = "插件注册失败"