mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-04 23:17:20 +08:00
refactor: 优化启停逻辑
This commit is contained in:
@@ -12,3 +12,9 @@ def stop_command():
|
|||||||
停止命令
|
停止命令
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def restart_command():
|
||||||
|
"""
|
||||||
|
重启命令
|
||||||
|
"""
|
||||||
|
Command().init_commands()
|
||||||
+24
-18
@@ -3,27 +3,25 @@ from contextlib import asynccontextmanager
|
|||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from core.config import global_vars
|
||||||
from app.startup.workflow_initializer import init_workflow, stop_workflow
|
from app.startup.workflow_initializer import init_workflow, stop_workflow
|
||||||
from app.startup.modules_initializer import init_modules, stop_modules
|
from app.startup.modules_initializer import init_modules, stop_modules
|
||||||
from app.startup.plugins_initializer import init_plugins, stop_plugins
|
from app.startup.plugins_initializer import init_plugins, stop_plugins, sync_plugins
|
||||||
from app.startup.routers_initializer import init_routers
|
from app.startup.routers_initializer import init_routers
|
||||||
from core.config import global_vars
|
from app.startup.command_initializer import init_command, stop_command, restart_command
|
||||||
from startup.command_initializer import init_command, stop_command
|
from app.startup.monitor_initializer import stop_monitor, init_monitor
|
||||||
from startup.monitor_initializer import stop_monitor, init_monitor
|
from app.startup.scheduler_initializer import stop_scheduler, init_scheduler, restart_scheduler, init_plugin_scheduler
|
||||||
from startup.scheduler_initializer import stop_scheduler, init_scheduler
|
|
||||||
|
|
||||||
|
|
||||||
async def init_extra_system():
|
async def init_plugin_system():
|
||||||
"""
|
"""
|
||||||
初始化额外的系统(依赖于插件初始化完成)
|
同步插件及重启相关依赖服务
|
||||||
"""
|
"""
|
||||||
await init_plugins()
|
if await sync_plugins():
|
||||||
# 启动监控器
|
# 重新注册插件定时服务
|
||||||
init_monitor()
|
init_plugin_scheduler()
|
||||||
# 启动定时器
|
# 重新注册命令
|
||||||
init_scheduler()
|
restart_command()
|
||||||
# 启动命令
|
|
||||||
init_command()
|
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
@@ -36,10 +34,18 @@ async def lifespan(app: FastAPI):
|
|||||||
init_modules()
|
init_modules()
|
||||||
# 初始化路由
|
# 初始化路由
|
||||||
init_routers(app)
|
init_routers(app)
|
||||||
|
# 初始化定时器
|
||||||
|
init_scheduler()
|
||||||
|
# 初始化监控器
|
||||||
|
init_monitor()
|
||||||
|
# 初始化命令
|
||||||
|
init_command()
|
||||||
# 初始化工作流
|
# 初始化工作流
|
||||||
init_workflow()
|
init_workflow()
|
||||||
# 初始化插件依赖系统
|
# 初始化插件
|
||||||
extra_init_task = asyncio.create_task(init_extra_system())
|
init_plugins()
|
||||||
|
# 插件同步到本地
|
||||||
|
sync_plugins_task = asyncio.create_task(sync_plugins())
|
||||||
try:
|
try:
|
||||||
# 在此处 yield,表示应用已经启动,控制权交回 FastAPI 主事件循环
|
# 在此处 yield,表示应用已经启动,控制权交回 FastAPI 主事件循环
|
||||||
yield
|
yield
|
||||||
@@ -48,8 +54,8 @@ async def lifespan(app: FastAPI):
|
|||||||
# 停止信号
|
# 停止信号
|
||||||
global_vars.stop_system()
|
global_vars.stop_system()
|
||||||
try:
|
try:
|
||||||
extra_init_task.cancel()
|
sync_plugins_task.cancel()
|
||||||
await extra_init_task
|
await sync_plugins_task
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from app.core.plugin import PluginManager
|
|||||||
from app.log import logger
|
from app.log import logger
|
||||||
|
|
||||||
|
|
||||||
async def init_plugins():
|
async def sync_plugins() -> bool:
|
||||||
"""
|
"""
|
||||||
初始化安装插件,并动态注册后台任务及API
|
初始化安装插件,并动态注册后台任务及API
|
||||||
"""
|
"""
|
||||||
@@ -18,16 +18,19 @@ async def init_plugins():
|
|||||||
# 判断是否需要进行插件初始化
|
# 判断是否需要进行插件初始化
|
||||||
if not sync_result and not resolved_dependencies:
|
if not sync_result and not resolved_dependencies:
|
||||||
logger.debug("没有新的插件同步到本地或缺失依赖项需要安装")
|
logger.debug("没有新的插件同步到本地或缺失依赖项需要安装")
|
||||||
|
return False
|
||||||
|
|
||||||
# 继续执行后续的插件初始化步骤
|
# 继续执行后续的插件初始化步骤
|
||||||
logger.info("正在初始化所有插件")
|
logger.info("正在重新初始化插件")
|
||||||
# 安装完成后初始化插件
|
# 重新初始化插件
|
||||||
plugin_manager.start()
|
plugin_manager.init_config()
|
||||||
# 插件启动后注册插件API
|
# 重新注册插件API
|
||||||
register_plugin_api()
|
register_plugin_api()
|
||||||
logger.info("所有插件初始化完成")
|
logger.info("所有插件初始化完成")
|
||||||
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"插件初始化过程中出现异常: {e}")
|
logger.error(f"插件初始化过程中出现异常: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def execute_task(loop, task_func, task_name):
|
async def execute_task(loop, task_func, task_name):
|
||||||
@@ -54,6 +57,14 @@ def register_plugin_api():
|
|||||||
plugin.register_plugin_api()
|
plugin.register_plugin_api()
|
||||||
|
|
||||||
|
|
||||||
|
def init_plugins():
|
||||||
|
"""
|
||||||
|
初始化插件
|
||||||
|
"""
|
||||||
|
PluginManager().start()
|
||||||
|
register_plugin_api()
|
||||||
|
|
||||||
|
|
||||||
def stop_plugins():
|
def stop_plugins():
|
||||||
"""
|
"""
|
||||||
停止插件
|
停止插件
|
||||||
|
|||||||
@@ -12,3 +12,15 @@ def stop_scheduler():
|
|||||||
停止定时器
|
停止定时器
|
||||||
"""
|
"""
|
||||||
Scheduler().stop()
|
Scheduler().stop()
|
||||||
|
|
||||||
|
def restart_scheduler():
|
||||||
|
"""
|
||||||
|
重启定时器
|
||||||
|
"""
|
||||||
|
Scheduler().init()
|
||||||
|
|
||||||
|
def init_plugin_scheduler():
|
||||||
|
"""
|
||||||
|
初始化插件定时器
|
||||||
|
"""
|
||||||
|
Scheduler().init_plugin_jobs()
|
||||||
|
|||||||
Reference in New Issue
Block a user