From 3ff3c9e144df0ff58aa74c65fc88169d276da862 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Wed, 8 Jan 2025 23:32:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=A3=80=E6=B5=8B=E5=88=B0docker?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E7=94=A8=E6=97=B6=E8=87=AA=E5=8A=A8=E7=A6=81?= =?UTF-8?q?=E7=94=A8=E6=9C=AC=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/core_lifecycle.py | 2 +- astrbot/core/star/star_manager.py | 9 ++++++--- packages/python_interpreter/main.py | 10 ++++++++-- tests/test_pipeline.py | 5 +++-- tests/test_plugin_manager.py | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/astrbot/core/core_lifecycle.py b/astrbot/core/core_lifecycle.py index ddb285cc9..1e6948355 100644 --- a/astrbot/core/core_lifecycle.py +++ b/astrbot/core/core_lifecycle.py @@ -53,7 +53,7 @@ class AstrBotCoreLifecycle: ) self.plugin_manager = PluginManager(self.star_context, self.astrbot_config) - self.plugin_manager.reload() + await self.plugin_manager.reload() '''扫描、注册插件、实例化插件类''' await self.provider_manager.initialize() diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 8d33eb3ab..23ba7c412 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -137,7 +137,7 @@ class PluginManager: return metadata - def reload(self): + async def reload(self): '''扫描并加载所有的 Star''' for smd in star_registry: logger.debug(f"尝试终止插件 {smd.name} ...") @@ -188,6 +188,9 @@ class PluginManager: # 通过装饰器的方式注册插件 metadata = star_map[path] metadata.star_cls = metadata.star_cls_type(context=self.context) + # 执行 initialize 函数 + if hasattr(metadata.star_cls, "initialize"): + await metadata.star_cls.initialize() metadata.module = module metadata.root_dir_name = root_dir_name metadata.reserved = reserved @@ -247,7 +250,7 @@ class PluginManager: async def install_plugin(self, repo_url: str): plugin_path = await self.updator.install(repo_url) # reload the plugin - self.reload() + await self.reload() return plugin_path async def uninstall_plugin(self, plugin_name: str): @@ -288,7 +291,7 @@ class PluginManager: raise Exception("该插件是 AstrBot 保留插件,无法更新。") await self.updator.update(plugin) - self.reload() + await self.reload() async def turn_off_plugin(self, plugin_name: str): plugin = self.context.get_registered_star(plugin_name) diff --git a/packages/python_interpreter/main.py b/packages/python_interpreter/main.py index 9768398c7..678062f01 100644 --- a/packages/python_interpreter/main.py +++ b/packages/python_interpreter/main.py @@ -109,6 +109,12 @@ class Main(star.Star): else: with open(PATH, "r") as f: self.config = json.load(f) + + async def initialize(self): + ok = await self.is_docker_available() + if not ok: + logger.warning("Docker 不可用,代码解释器将无法使用吗,astrbot-python-interpreter 将自动禁用。") + self.context._star_manager.turn_off_star(self.__module__) async def file_upload(self, file_path: str): ''' @@ -135,7 +141,7 @@ class Main(star.Star): await docker.version() return True except aiodocker.exceptions.DockerError as e: - logger.error(f"Error when check docker: {e}") + logger.error(f"检查 Docker 可用性时出现问题: {e}") return False async def get_image_name(self) -> str: @@ -352,7 +358,7 @@ class Main(star.Star): if not ok: if traceback: - obs = f"## Observation\When execute the code: ```python\n{code_clean}\n```\n\n Error occured:\n\n{traceback}\n Need to improve/fix the code." + obs = f"## Observation \n When execute the code: ```python\n{code_clean}\n```\n\n Error occured:\n\n{traceback}\n Need to improve/fix the code." else: logger.warning(f"未从沙箱输出中捕获到合法的输出。沙箱输出日志: {logs}") break diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 4d90fae88..b75c314ee 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -117,9 +117,10 @@ def star_context(event_queue, config, db, platform_manager, provider_manager): return star_context @pytest.fixture(scope="module") -def plugin_manager(star_context, config): +@pytest.mark.asyncio +async def plugin_manager(star_context, config): plugin_manager = PluginManager(star_context, config) - plugin_manager.reload() + await plugin_manager.reload() return plugin_manager @pytest.fixture(scope="module") diff --git a/tests/test_plugin_manager.py b/tests/test_plugin_manager.py index 8d7b35568..1d336a57a 100644 --- a/tests/test_plugin_manager.py +++ b/tests/test_plugin_manager.py @@ -27,7 +27,7 @@ def test_plugin_manager_initialization(plugin_manager_pm: PluginManager): @pytest.mark.asyncio async def test_plugin_manager_reload(plugin_manager_pm: PluginManager): - success, err_message = plugin_manager_pm.reload() + success, err_message = await plugin_manager_pm.reload() assert success is True assert err_message is None assert len(star_handlers_registry) > 0 # package