Skip to content

Commit

Permalink
fix(plugin): optimize exist check and cleanup on installation failure
Browse files Browse the repository at this point in the history
  • Loading branch information
InfinityPacer committed Oct 17, 2024
1 parent bcc48e8 commit 346c6dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/core/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def sync(self) -> List[str]:
"""
安装本地不存在的在线插件
"""

def install_plugin(plugin):
start_time = time.time()
state, msg = self.pluginhelper.install(pid=plugin.id, repo_url=plugin.repo_url)
Expand Down Expand Up @@ -719,7 +720,8 @@ def is_plugin_exists(pid: str) -> bool:
# 构建包名
package_name = f"app.plugins.{pid.lower()}"
# 检查包是否存在
package_exists = importlib.util.find_spec(package_name) is not None
spec = importlib.util.find_spec(package_name)
package_exists = spec is not None and spec.origin is not None
logger.debug(f"{pid} exists: {package_exists}")
return package_exists
except Exception as e:
Expand Down
8 changes: 6 additions & 2 deletions app/helper/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
import traceback
from pathlib import Path
from typing import Dict, Tuple, Optional, List, Any
from typing import Any, Dict, List, Optional, Tuple

from cachetools import TTLCache, cached

Expand Down Expand Up @@ -151,7 +151,7 @@ def install_report(self) -> bool:
def install(self, pid: str, repo_url: str, package_version: str = None) -> Tuple[bool, str]:
"""
安装插件,包括依赖安装和文件下载,相关资源支持自动降级策略
1. 检查并获取插件的指定版本,确认版本兼容性
1. 检查并获取插件的指定版本,确认版本兼容性
2. 从 GitHub 获取文件列表(包括 requirements.txt)
3. 删除旧的插件目录
4. 下载并预安装 requirements.txt 中的依赖(如果存在)
Expand Down Expand Up @@ -215,7 +215,9 @@ def install(self, pid: str, repo_url: str, package_version: str = None) -> Tuple
logger.info(f"{pid} 准备开始下载插件文件")
success, message = self.__download_files(pid.lower(), file_list, user_repo, package_version, True)
if not success:
self.__remove_old_plugin(pid.lower())
logger.error(f"{pid} 下载插件文件失败:{message}")
logger.warning(f"{pid} 已清理对应插件目录,请尝试重新安装")
return False, message
else:
logger.info(f"{pid} 下载插件文件成功")
Expand All @@ -224,7 +226,9 @@ def install(self, pid: str, repo_url: str, package_version: str = None) -> Tuple
dependencies_exist, success, message = self.__install_dependencies_if_required(pid)
if dependencies_exist:
if not success:
self.__remove_old_plugin(pid.lower())
logger.error(f"{pid} 依赖安装失败:{message}")
logger.warning(f"{pid} 已清理对应插件目录,请尝试重新安装")
else:
logger.info(f"{pid} 依赖安装成功")

Expand Down

0 comments on commit 346c6dd

Please sign in to comment.