diff --git a/app/startup/lifecycle.py b/app/startup/lifecycle.py index 2929e396..8d426355 100644 --- a/app/startup/lifecycle.py +++ b/app/startup/lifecycle.py @@ -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 diff --git a/app/startup/plugins_initializer.py b/app/startup/plugins_initializer.py index 8d015245..03fb52b5 100644 --- a/app/startup/plugins_initializer.py +++ b/app/startup/plugins_initializer.py @@ -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}") diff --git a/app/utils/system.py b/app/utils/system.py index f375570a..c3643fb7 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -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