feat:识别容器是否重置

This commit is contained in:
jxxghp
2025-06-09 09:15:58 +08:00
parent e4e2079917
commit 327d30dcc2
3 changed files with 69 additions and 28 deletions

View File

@@ -13,9 +13,10 @@ 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
async def init_plugin_system():
async def init_extra():
"""
同步插件及重启相关依赖服务
"""
@@ -24,6 +25,8 @@ async def init_plugin_system():
init_plugin_scheduler()
# 重新注册命令
restart_command()
# 设置系统已修改标志
SystemUtils.set_system_modified()
# 重启完成
SystemChain().restart_finish()
@@ -53,7 +56,7 @@ async def lifespan(app: FastAPI):
# 初始化内存管理
init_memory_manager()
# 插件同步到本地
sync_plugins_task = asyncio.create_task(init_plugin_system())
sync_plugins_task = asyncio.create_task(init_extra())
try:
# 在此处 yield表示应用已经启动控制权交回 FastAPI 主事件循环
yield

View File

@@ -4,6 +4,7 @@ import shutil
from app.core.config import settings
from app.core.plugin import PluginManager
from app.log import logger
from app.utils.system import SystemUtils
async def sync_plugins() -> bool:
@@ -81,8 +82,13 @@ def stop_plugins():
def backup_plugins():
"""
备份插件到用户配置目录
备份插件到用户配置目录仅docker环境
"""
# 非docker环境不处理
if not SystemUtils.is_docker():
return
try:
# 使用绝对路径确保准确性
plugins_dir = settings.ROOT_PATH / "app" / "plugins"
@@ -124,8 +130,13 @@ def backup_plugins():
def restore_plugins():
"""
从备份恢复插件到app/plugins目录恢复完成后删除备份
从备份恢复插件到app/plugins目录恢复完成后删除备份仅docker环境
"""
# 非docker环境不处理
if not SystemUtils.is_docker():
return
try:
# 使用绝对路径确保准确性
plugins_dir = settings.ROOT_PATH / "app" / "plugins"
@@ -134,31 +145,34 @@ def restore_plugins():
if not backup_dir.exists():
logger.info("插件备份目录不存在,跳过恢复")
return
# 系统被重置才恢复插件
if SystemUtils.is_system_reset():
# 确保插件目录存在
plugins_dir.mkdir(parents=True, exist_ok=True)
# 确保插件目录存在
plugins_dir.mkdir(parents=True, exist_ok=True)
# 遍历备份目录,恢复所有内容
restored_count = 0
for item in backup_dir.iterdir():
target_path = plugins_dir / item.name
# 如果是目录,且目录内有内容
if item.is_dir() and any(item.iterdir()):
if target_path.exists():
shutil.rmtree(target_path)
shutil.copytree(item, target_path)
logger.info(f"已恢复插件目录: {item.name}")
restored_count += 1
# 如果是文件
elif item.is_file():
shutil.copy2(item, target_path)
logger.info(f"已恢复插件文件: {item.name}")
restored_count += 1
logger.info(f"插件恢复完成,共恢复 {restored_count} 个项目")
# 遍历备份目录,恢复所有内容
restored_count = 0
for item in backup_dir.iterdir():
target_path = plugins_dir / item.name
# 如果是目录,且目录内有内容
if item.is_dir() and any(item.iterdir()):
if target_path.exists():
shutil.rmtree(target_path)
shutil.copytree(item, target_path)
logger.info(f"已恢复插件目录: {item.name}")
restored_count += 1
# 如果是文件
elif item.is_file():
shutil.copy2(item, target_path)
logger.info(f"已恢复插件文件: {item.name}")
restored_count += 1
logger.info(f"插件恢复完成,共恢复 {restored_count} 个项目")
# 恢复完成后删除备份目录
# 删除备份目录
try:
shutil.rmtree(backup_dir)
logger.info(f"已删除插件备份目录: {backup_dir}")

View File

@@ -17,6 +17,9 @@ from app import schemas
class SystemUtils:
"""
系统工具类,提供系统相关的操作和信息获取方法。
"""
@staticmethod
def execute(cmd: str) -> str:
@@ -439,7 +442,7 @@ class SystemUtils:
current_process = psutil.Process()
process_memory = current_process.memory_info().rss
system_memory = psutil.virtual_memory().total
process_memory_percent = (process_memory / system_memory) * 100
process_memory_percent = (process_memory / system_memory) * 100
return [process_memory, int(process_memory_percent)]
@staticmethod
@@ -562,3 +565,24 @@ class SystemUtils:
if unique_id:
return unique_id
return None
@staticmethod
def set_system_modified():
"""
设置系统已修改标志
"""
try:
if SystemUtils.is_docker():
Path("/__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("/__moviepilot__").exists()
return False