From f8949ebead0eca5f3e79335e784e8edb59f2fefb Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 11 Aug 2024 23:23:52 -0400 Subject: [PATCH] perf: reboot after installing plugin --- dashboard/server.py | 10 ++++++---- main.py | 2 +- model/plugin/manager.py | 44 ++++++++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/dashboard/server.py b/dashboard/server.py index 60e880b51..a2fdc2564 100644 --- a/dashboard/server.py +++ b/dashboard/server.py @@ -192,10 +192,11 @@ class AstrBotDashBoard(): try: logger.info(f"正在安装插件 {repo_url}") self.plugin_manager.install_plugin(repo_url) - logger.info(f"安装插件 {repo_url} 成功") + threading.Thread(target=self.astrbot_updator._reboot, args=(2, self.context)).start() + logger.info(f"安装插件 {repo_url} 成功,2秒后重启") return Response( status="success", - message="安装成功~", + message="安装成功,机器人将在 2 秒内重启。", data=None ).__dict__ except Exception as e: @@ -258,10 +259,11 @@ class AstrBotDashBoard(): try: logger.info(f"正在更新插件 {plugin_name}") self.plugin_manager.update_plugin(plugin_name) - logger.info(f"更新插件 {plugin_name} 成功") + threading.Thread(target=self.astrbot_updator._reboot, args=(2, self.context)).start() + logger.info(f"更新插件 {plugin_name} 成功,2秒后重启") return Response( status="success", - message="更新成功~", + message="更新成功,机器人将在 2 秒内重启。", data=None ).__dict__ except Exception as e: diff --git a/main.py b/main.py index 9803ffe0d..3eead7548 100644 --- a/main.py +++ b/main.py @@ -53,7 +53,7 @@ if __name__ == "__main__": check_env() logger = LogManager.GetLogger( - log_name='astrbot', + log_name='astrbot', out_to_console=True, custom_formatter=Formatter('[%(asctime)s| %(name)s - %(levelname)s|%(filename)s:%(lineno)d]: %(message)s', datefmt="%H:%M:%S") ) diff --git a/model/plugin/manager.py b/model/plugin/manager.py index 35fcb90e7..43e9cc30f 100644 --- a/model/plugin/manager.py +++ b/model/plugin/manager.py @@ -5,6 +5,7 @@ import traceback import uuid import shutil import yaml +import subprocess from util.updator.plugin_updator import PluginUpdator from util.io import remove_dir, download_file @@ -84,8 +85,28 @@ class PluginManager(): def update_plugin_dept(self, path): mirror = "https://mirrors.aliyun.com/pypi/simple/" py = sys.executable - os.system(f"{py} -m pip install -r {path} -i {mirror} --quiet") - + # os.system(f"{py} -m pip install -r {path} -i {mirror} --break-system-package --trusted-host mirrors.aliyun.com") + + process = subprocess.Popen(f"{py} -m pip install -r {path} -i {mirror} --break-system-package --trusted-host mirrors.aliyun.com", + stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True) + + while True: + output = process.stdout.readline() + if output == '' and process.poll() is not None: + break + if output: + output = output.strip() + if output.startswith("Requirement already satisfied"): + continue + if output.startswith("Using cached"): + continue + if output.startswith("Looking in indexes"): + continue + logger.info(output) + + rc = process.poll() + + def install_plugin(self, repo_url: str): ppath = self.plugin_store_path @@ -95,10 +116,13 @@ class PluginManager(): plugin_path = self.updator.update(repo_url) with open(os.path.join(plugin_path, "REPO"), "w", encoding='utf-8') as f: f.write(repo_url) + + self.check_plugin_dept_update() - ok, err = self.plugin_reload() - if not ok: - raise Exception(err) + return plugin_path + # ok, err = self.plugin_reload() + # if not ok: + # raise Exception(err) def download_from_repo_url(self, target_path: str, repo_url: str): repo_namespace = repo_url.split("/")[-2:] @@ -158,7 +182,7 @@ class PluginManager(): logger.info(f"正在加载插件 {root_dir_name} ...") - # self.check_plugin_dept_update(cached_plugins, root_dir_name) + self.check_plugin_dept_update(target_plugin=root_dir_name) module = __import__("addons.plugins." + root_dir_name + "." + p, fromlist=[p]) @@ -227,10 +251,12 @@ class PluginManager(): # remove the temp dir remove_dir(temp_dir) + + self.check_plugin_dept_update() - ok, err = self.plugin_reload() - if not ok: - raise Exception(err) + # ok, err = self.plugin_reload() + # if not ok: + # raise Exception(err) def load_plugin_metadata(self, plugin_path: str, plugin_obj = None) -> PluginMetadata: metadata = None