From 089b007a003b5c40d04819f924b3a2b23fb829de Mon Sep 17 00:00:00 2001 From: developer-wlj Date: Wed, 2 Sep 2026 20:04:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(state):=20=E6=B7=BB=E5=8A=A0Windows?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将Windows重启逻辑封装为_staticmethod - 实现了独立的命令行执行方法cmd - 添加了可执行文件存在性检查 - 统一了重启操作的返回格式 - 改进了错误处理和消息提示机制 --- app/runtime/state.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/runtime/state.py b/app/runtime/state.py index f7cfa4608..8042e6de6 100644 --- a/app/runtime/state.py +++ b/app/runtime/state.py @@ -14,7 +14,7 @@ import psutil from app.runtime.settings import get_runtime_setting from app.runtime.log import logger from app.runtime.reload import ConfigReloadMixin -from app.foundation.environment import is_docker +from app.foundation.environment import is_windows,is_frozen,is_docker class SystemHelper(ConfigReloadMixin): @@ -59,7 +59,7 @@ class SystemHelper(ConfigReloadMixin): """ 判断是否可以内部重启 """ - return is_docker() or SystemHelper._is_local_cli_managed() + return is_docker() or SystemHelper._is_local_cli_managed() or (is_windows() and not is_frozen()) @staticmethod def _load_runtime_file(path: Path) -> Optional[dict]: @@ -246,11 +246,34 @@ class SystemHelper(ConfigReloadMixin): except OSError as err: logger.warning(f"清理内置重启标记失败: {err}") + @staticmethod + def _windows_restart() -> tuple[bool, str]: + """ + 执行Windows重启操作 + """ + def cmd(command: list) -> Tuple[bool, str]: + try: + subprocess.run(command, shell=True) + return True, "" + except Exception as error: + return False, f"cmd命令{command}执行失败.原因:{str(error)}" + + mp_exe = str(Path(__file__).parents[3].parent / "MoviePilot-V3.exe") + if not Path(mp_exe).exists(): + return False, f"{mp_exe} 文件不存在, 无法执行重启" + success, message = cmd(["start", "", mp_exe, "-c", "restart"]) + if not success: + return False, message + return True, "" + @staticmethod def restart() -> Tuple[bool, str]: """ 执行Docker重启操作 """ + if not is_frozen() and is_windows(): + success, message = SystemHelper._windows_restart() + return success, message if not is_docker(): if not SystemHelper._is_local_cli_managed(): return False, "当前实例不是由 moviepilot CLI 启动,无法执行内建重启!"