diff --git a/app/chain/__init__.py b/app/chain/__init__.py index c63cc00c..ded8ff99 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -65,6 +65,15 @@ class ChainBase(metaclass=ABCMeta): del cache gc.collect() + @staticmethod + def remove_cache(filename: str) -> None: + """ + 删除本地缓存 + """ + cache_path = settings.TEMP_PATH / filename + if cache_path.exists(): + Path(cache_path).unlink() + def run_module(self, method: str, *args, **kwargs) -> Any: """ 运行包含该方法的所有模块,然后返回结果 diff --git a/app/chain/system.py b/app/chain/system.py index 75c0ab98..0619a6c7 100644 --- a/app/chain/system.py +++ b/app/chain/system.py @@ -1,7 +1,13 @@ +import json +import re +from pathlib import Path from typing import Union from app.chain import ChainBase +from app.core.config import settings +from app.log import logger from app.schemas import Notification, MessageChannel +from app.utils.http import RequestUtils from app.utils.system import SystemUtils @@ -10,6 +16,8 @@ class SystemChain(ChainBase): 系统级处理链 """ + _restart_file = "__system_restart__" + def remote_clear_cache(self, channel: MessageChannel, userid: Union[int, str]): """ 清理系统缓存 @@ -22,6 +30,94 @@ class SystemChain(ChainBase): """ 重启系统 """ - self.post_message(Notification(channel=channel, - title=f"系统正在重启,请耐心等候!", userid=userid)) + if channel and userid: + self.post_message(Notification(channel=channel, + title="系统正在重启,请耐心等候!", userid=userid)) + # 保存重启信息 + self.save_cache({ + "channel": channel.value, + "userid": userid + }, self._restart_file) SystemUtils.restart() + + def version(self, channel: MessageChannel, userid: Union[int, str]): + """ + 查看当前版本、远程版本 + """ + release_version = self.__get_release_version() + local_version = self.__get_local_version() + if release_version == local_version: + title = f"当前版本:{local_version},已是最新版本" + else: + title = f"当前版本:{local_version},远程版本:{release_version}" + + self.post_message(Notification(channel=channel, + title=title, userid=userid)) + + def restart_finish(self): + """ + 如通过交互命令重启, + 重启完发送msg + """ + restart_channel = self.load_cache(self._restart_file) + logger.info(f"获取到重启msg=> {restart_channel}") + if restart_channel: + # 发送重启完成msg + if not isinstance(restart_channel, dict): + restart_channel = json.loads(restart_channel) + + # 版本号 + release_version = self.__get_release_version() + local_version = self.__get_local_version() + if release_version == local_version: + title = f"当前版本:{local_version}" + else: + title = f"当前版本:{local_version},远程版本:{release_version}" + + channel = next( + (channel for channel in MessageChannel.__members__.values() if + channel.value == restart_channel.get('channel')), None) + + self.post_message(Notification(channel=channel, + title=f"系统已重启完成!{title}", + userid=restart_channel.get('userid'))) + self.remove_cache(self._restart_file) + + @staticmethod + def __get_release_version(): + """ + 获取最新版本 + """ + version_res = RequestUtils().get_res( + "https://api.github.com/repos/jxxghp/MoviePilot/releases/latest") + if not version_res: + version_res = RequestUtils(proxies=settings.PROXY).get_res( + "https://api.github.com/repos/jxxghp/MoviePilot/releases/latest") + if version_res: + ver_json = version_res.json() + version = f"{ver_json['tag_name']}" + return version + else: + return None + + @staticmethod + def __get_local_version(): + """ + 查看当前版本 + """ + version_file = Path(__file__).parents[2] / "version.py" + if version_file.exists(): + try: + with open(version_file, 'rb') as f: + version = f.read() + pattern = r"v(\d+\.\d+\.\d+)" + match = re.search(pattern, str(version)) + + if match: + version = match.group(1) + return f"v{version}" + else: + logger.warn("未找到版本号") + return None + except Exception as err: + logger.error(f"加载版本文件 {version_file} 出错:{err}") diff --git a/app/command.py b/app/command.py index e4b5a1f8..5bd730a1 100644 --- a/app/command.py +++ b/app/command.py @@ -142,6 +142,12 @@ class Command(metaclass=Singleton): "description": "重启系统", "category": "管理", "data": {} + }, + "/version": { + "func": SystemChain(self._db).version, + "description": "当前版本", + "category": "管理", + "data": {} } } # 汇总插件命令 @@ -163,6 +169,8 @@ class Command(metaclass=Singleton): self._thread = Thread(target=self.__run) # 启动事件处理线程 self._thread.start() + # 重启msg + SystemChain(self._db).restart_finish() def __run(self): """