From 162ba9307d1a6a0df3b84d80539fa87436216d95 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 10 Jun 2025 07:09:59 +0800 Subject: [PATCH] fix restart --- app/helper/system.py | 64 +++++++++++++----------------- app/startup/lifecycle.py | 4 +- app/startup/plugins_initializer.py | 5 ++- app/utils/system.py | 21 ---------- 4 files changed, 33 insertions(+), 61 deletions(-) diff --git a/app/helper/system.py b/app/helper/system.py index f9c76aab..88b42a9b 100644 --- a/app/helper/system.py +++ b/app/helper/system.py @@ -1,6 +1,5 @@ import os import signal -import time from pathlib import Path from typing import Tuple @@ -19,6 +18,8 @@ class SystemHelper: 系统工具类,提供系统相关的操作和判断 """ + __system_flag_file = "/var/log/nginx/__moviepilot__" + @eventmanager.register(EventType.ConfigChanged) def handle_config_changed(self, event: Event): """ @@ -75,17 +76,17 @@ class SystemHelper: 检查当前容器是否配置了自动重启策略 """ try: - # 创建 Docker 客户端 - client = docker.DockerClient(base_url=settings.DOCKER_CLIENT_API) + # 获取当前容器ID container_id = SystemHelper._get_container_id() if not container_id: return False - + + # 创建 Docker 客户端 + client = docker.DockerClient(base_url=settings.DOCKER_CLIENT_API) # 获取容器信息 container = client.containers.get(container_id) restart_policy = container.attrs.get('HostConfig', {}).get('RestartPolicy', {}) policy_name = restart_policy.get('Name', 'no') - # 检查是否有有效的重启策略 auto_restart_policies = ['always', 'unless-stopped', 'on-failure'] has_restart_policy = policy_name in auto_restart_policies @@ -137,37 +138,28 @@ class SystemHelper: container_id = SystemHelper._get_container_id() if not container_id: return False, "获取容器ID失败!" - - container = client.containers.get(container_id) - - # 尝试优雅停止:先发送SIGTERM信号,给容器30秒时间优雅停止 - try: - logger.info("发送SIGTERM信号,尝试优雅停止...") - container.kill(signal='SIGTERM') - - # 等待容器优雅停止,最多等待30秒 - for i in range(30): - container.reload() - if container.status != 'running': - logger.info(f"容器已优雅停止 (耗时 {i+1} 秒)") - break - time.sleep(1) - else: - # 30秒后仍未停止,强制停止 - logger.warning("优雅停止超时,强制停止容器...") - container.kill(signal='SIGKILL') - - except Exception as stop_err: - logger.warning(f"优雅停止失败: {str(stop_err)}") - # 直接重启 - container.restart() - return True, "" - - # 启动容器 - logger.info("启动容器...") - container.start() + # 重启容器 + client.containers.get(container_id).restart() return True, "" - + except Exception as docker_err: - print(str(docker_err)) return False, f"重启时发生错误:{str(docker_err)}" + + def set_system_modified(self): + """ + 设置系统已修改标志 + """ + try: + if SystemUtils.is_docker(): + Path(self.__system_flag_file).touch(exist_ok=True) + except Exception as e: + print(f"设置系统修改标志失败: {str(e)}") + + def is_system_reset(self) -> bool: + """ + 检查系统是否已被重置 + :return: 如果系统已重置,返回 True;否则返回 False + """ + if SystemUtils.is_docker(): + return not Path(self.__system_flag_file).exists() + return False diff --git a/app/startup/lifecycle.py b/app/startup/lifecycle.py index 50c96310..c06d2bf8 100644 --- a/app/startup/lifecycle.py +++ b/app/startup/lifecycle.py @@ -12,7 +12,7 @@ from app.startup.plugins_initializer import init_plugins, stop_plugins, sync_plu from app.startup.routers_initializer import init_routers from app.startup.scheduler_initializer import stop_scheduler, init_scheduler, init_plugin_scheduler from app.startup.workflow_initializer import init_workflow, stop_workflow -from app.utils.system import SystemUtils +from app.helper.system import SystemHelper async def init_extra(): @@ -25,7 +25,7 @@ async def init_extra(): # 重新注册命令 restart_command() # 设置系统已修改标志 - SystemUtils.set_system_modified() + SystemHelper().set_system_modified() # 重启完成 SystemChain().restart_finish() diff --git a/app/startup/plugins_initializer.py b/app/startup/plugins_initializer.py index 03fb52b5..4b2f7522 100644 --- a/app/startup/plugins_initializer.py +++ b/app/startup/plugins_initializer.py @@ -5,6 +5,7 @@ from app.core.config import settings from app.core.plugin import PluginManager from app.log import logger from app.utils.system import SystemUtils +from app.helper.system import SystemHelper async def sync_plugins() -> bool: @@ -147,8 +148,8 @@ def restore_plugins(): return # 系统被重置才恢复插件 - if SystemUtils.is_system_reset(): - + if SystemHelper().is_system_reset(): + # 确保插件目录存在 plugins_dir.mkdir(parents=True, exist_ok=True) diff --git a/app/utils/system.py b/app/utils/system.py index 78b9f734..a41dc934 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -565,24 +565,3 @@ class SystemUtils: if unique_id: return unique_id return None - - @staticmethod - def set_system_modified(): - """ - 设置系统已修改标志 - """ - try: - if SystemUtils.is_docker(): - Path("/var/log/nginx/__moviepilot__").touch(exist_ok=True) - except Exception as e: - print(f"设置系统修改标志失败: {str(e)}") - - @staticmethod - def is_system_reset() -> bool: - """ - 检查系统是否已被重置 - :return: 如果系统已重置,返回 True;否则返回 False - """ - if SystemUtils.is_docker(): - return not Path("/var/log/nginx/__moviepilot__").exists() - return False