mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 23:47:41 +08:00
feat:识别容器是否重置
This commit is contained in:
@@ -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.routers_initializer import init_routers
|
||||||
from app.startup.scheduler_initializer import stop_scheduler, init_scheduler, init_plugin_scheduler
|
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.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()
|
init_plugin_scheduler()
|
||||||
# 重新注册命令
|
# 重新注册命令
|
||||||
restart_command()
|
restart_command()
|
||||||
|
# 设置系统已修改标志
|
||||||
|
SystemUtils.set_system_modified()
|
||||||
# 重启完成
|
# 重启完成
|
||||||
SystemChain().restart_finish()
|
SystemChain().restart_finish()
|
||||||
|
|
||||||
@@ -53,7 +56,7 @@ async def lifespan(app: FastAPI):
|
|||||||
# 初始化内存管理
|
# 初始化内存管理
|
||||||
init_memory_manager()
|
init_memory_manager()
|
||||||
# 插件同步到本地
|
# 插件同步到本地
|
||||||
sync_plugins_task = asyncio.create_task(init_plugin_system())
|
sync_plugins_task = asyncio.create_task(init_extra())
|
||||||
try:
|
try:
|
||||||
# 在此处 yield,表示应用已经启动,控制权交回 FastAPI 主事件循环
|
# 在此处 yield,表示应用已经启动,控制权交回 FastAPI 主事件循环
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import shutil
|
|||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.core.plugin import PluginManager
|
from app.core.plugin import PluginManager
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
|
from app.utils.system import SystemUtils
|
||||||
|
|
||||||
|
|
||||||
async def sync_plugins() -> bool:
|
async def sync_plugins() -> bool:
|
||||||
@@ -81,8 +82,13 @@ def stop_plugins():
|
|||||||
|
|
||||||
def backup_plugins():
|
def backup_plugins():
|
||||||
"""
|
"""
|
||||||
备份插件到用户配置目录
|
备份插件到用户配置目录(仅docker环境)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# 非docker环境不处理
|
||||||
|
if not SystemUtils.is_docker():
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 使用绝对路径确保准确性
|
# 使用绝对路径确保准确性
|
||||||
plugins_dir = settings.ROOT_PATH / "app" / "plugins"
|
plugins_dir = settings.ROOT_PATH / "app" / "plugins"
|
||||||
@@ -124,8 +130,13 @@ def backup_plugins():
|
|||||||
|
|
||||||
def restore_plugins():
|
def restore_plugins():
|
||||||
"""
|
"""
|
||||||
从备份恢复插件到app/plugins目录,恢复完成后删除备份
|
从备份恢复插件到app/plugins目录,恢复完成后删除备份(仅docker环境)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# 非docker环境不处理
|
||||||
|
if not SystemUtils.is_docker():
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 使用绝对路径确保准确性
|
# 使用绝对路径确保准确性
|
||||||
plugins_dir = settings.ROOT_PATH / "app" / "plugins"
|
plugins_dir = settings.ROOT_PATH / "app" / "plugins"
|
||||||
@@ -135,30 +146,33 @@ def restore_plugins():
|
|||||||
logger.info("插件备份目录不存在,跳过恢复")
|
logger.info("插件备份目录不存在,跳过恢复")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 确保插件目录存在
|
# 系统被重置才恢复插件
|
||||||
plugins_dir.mkdir(parents=True, exist_ok=True)
|
if SystemUtils.is_system_reset():
|
||||||
|
|
||||||
# 遍历备份目录,恢复所有内容
|
# 确保插件目录存在
|
||||||
restored_count = 0
|
plugins_dir.mkdir(parents=True, exist_ok=True)
|
||||||
for item in backup_dir.iterdir():
|
|
||||||
target_path = plugins_dir / item.name
|
|
||||||
|
|
||||||
# 如果是目录,且目录内有内容
|
# 遍历备份目录,恢复所有内容
|
||||||
if item.is_dir() and any(item.iterdir()):
|
restored_count = 0
|
||||||
if target_path.exists():
|
for item in backup_dir.iterdir():
|
||||||
shutil.rmtree(target_path)
|
target_path = plugins_dir / item.name
|
||||||
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} 个项目")
|
# 如果是目录,且目录内有内容
|
||||||
|
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:
|
try:
|
||||||
shutil.rmtree(backup_dir)
|
shutil.rmtree(backup_dir)
|
||||||
logger.info(f"已删除插件备份目录: {backup_dir}")
|
logger.info(f"已删除插件备份目录: {backup_dir}")
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ from app import schemas
|
|||||||
|
|
||||||
|
|
||||||
class SystemUtils:
|
class SystemUtils:
|
||||||
|
"""
|
||||||
|
系统工具类,提供系统相关的操作和信息获取方法。
|
||||||
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def execute(cmd: str) -> str:
|
def execute(cmd: str) -> str:
|
||||||
@@ -562,3 +565,24 @@ class SystemUtils:
|
|||||||
if unique_id:
|
if unique_id:
|
||||||
return unique_id
|
return unique_id
|
||||||
return None
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user