Skip to content

Commit

Permalink
perf: reboot after installing plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter committed Aug 12, 2024
1 parent b8316bf commit 4c8a5c3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
10 changes: 6 additions & 4 deletions dashboard/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ def install_plugin():
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:
Expand Down Expand Up @@ -258,10 +259,11 @@ def update_plugin():
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:
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def check_env():
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")
)
Expand Down
44 changes: 35 additions & 9 deletions model/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
import shutil
import yaml
import subprocess

from util.updator.plugin_updator import PluginUpdator
from util.io import remove_dir, download_file
Expand Down Expand Up @@ -84,8 +85,28 @@ def check_plugin_dept_update(self, target_plugin: str = None):
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

Expand All @@ -95,10 +116,13 @@ def install_plugin(self, repo_url: str):
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:]
Expand Down Expand Up @@ -158,7 +182,7 @@ def plugin_reload(self):

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])
Expand Down Expand Up @@ -227,10 +251,12 @@ def install_plugin_from_file(self, zip_file_path: str):

# 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
Expand Down

0 comments on commit 4c8a5c3

Please sign in to comment.