perf: 检测到docker不可用时自动禁用本插件

This commit is contained in:
Soulter
2025-01-08 23:32:49 +08:00
parent f5a37d82cc
commit 3ff3c9e144
5 changed files with 19 additions and 9 deletions
+1 -1
View File
@@ -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()
+6 -3
View File
@@ -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)
+8 -2
View File
@@ -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
+3 -2
View File
@@ -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")
+1 -1
View File
@@ -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