diff --git a/app/chain/system.py b/app/chain/system.py index 7b3fe69f..8930d4c3 100644 --- a/app/chain/system.py +++ b/app/chain/system.py @@ -1,4 +1,5 @@ import json +import os import re from typing import Union @@ -16,6 +17,7 @@ class SystemChain(ChainBase): """ _restart_file = "__system_restart__" + _update_file = "__system_update__" def remote_clear_cache(self, channel: MessageChannel, userid: Union[int, str]): """ @@ -39,6 +41,25 @@ class SystemChain(ChainBase): }, self._restart_file) SystemUtils.restart() + def update(self, channel: MessageChannel, userid: Union[int, str]): + """ + 重启系统 + """ + if channel and userid: + self.post_message(Notification(channel=channel, + title="系统正在更新,请耐心等候!", userid=userid)) + # 保存重启信息 + self.save_cache({ + "channel": channel.value, + "userid": userid + }, self._update_file) + + # 重启系统 + os.system("bash /usr/local/bin/mp_update") + self.post_message(Notification(channel=channel, + title="暂无新版本!", userid=userid)) + self.remove_cache(self._update_file) + def version(self, channel: MessageChannel, userid: Union[int, str]): """ 查看当前版本、远程版本 @@ -58,13 +79,35 @@ class SystemChain(ChainBase): 如通过交互命令重启, 重启完发送msg """ + cache_file, action, channel, userid = None, None, None, None + # 重启消息 restart_channel = self.load_cache(self._restart_file) - logger.info(f"获取到重启msg=> {restart_channel}") if restart_channel: + cache_file = self._restart_file + action = "重启" # 发送重启完成msg if not isinstance(restart_channel, dict): restart_channel = json.loads(restart_channel) + channel = next( + (channel for channel in MessageChannel.__members__.values() if + channel.value == restart_channel.get('channel')), None) + userid = restart_channel.get('userid') + # 更新消息 + update_channel = self.load_cache(self._update_file) + if update_channel: + cache_file = self._update_file + action = "更新" + # 发送重启完成msg + if not isinstance(update_channel, dict): + update_channel = json.loads(update_channel) + channel = next( + (channel for channel in MessageChannel.__members__.values() if + channel.value == update_channel.get('channel')), None) + userid = update_channel.get('userid') + + # 发送消息 + if channel and userid: # 版本号 release_version = self.__get_release_version() local_version = self.__get_local_version() @@ -72,15 +115,10 @@ class SystemChain(ChainBase): 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) + title=f"系统已{action}完成!{title}", + userid=userid)) + self.remove_cache(cache_file) @staticmethod def __get_release_version(): diff --git a/app/command.py b/app/command.py index 5bd730a1..34740489 100644 --- a/app/command.py +++ b/app/command.py @@ -148,6 +148,12 @@ class Command(metaclass=Singleton): "description": "当前版本", "category": "管理", "data": {} + }, + "/update": { + "func": SystemChain(self._db).update, + "description": "更新系统", + "category": "管理", + "data": {} } } # 汇总插件命令