fix restart

This commit is contained in:
jxxghp
2025-06-10 07:09:59 +08:00
parent 49dae92b8e
commit 162ba9307d
4 changed files with 33 additions and 61 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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)

View File

@@ -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