mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 07:27:15 +08:00
refactor(lifecycle): add graceful support and remove signal handling
This commit is contained in:
+2
-1
@@ -20,7 +20,8 @@ from app.db.init import init_db, update_db
|
|||||||
|
|
||||||
# uvicorn服务
|
# uvicorn服务
|
||||||
Server = uvicorn.Server(Config(app, host=settings.HOST, port=settings.PORT,
|
Server = uvicorn.Server(Config(app, host=settings.HOST, port=settings.PORT,
|
||||||
reload=settings.DEV, workers=multiprocessing.cpu_count()))
|
reload=settings.DEV, workers=multiprocessing.cpu_count(),
|
||||||
|
timeout_graceful_shutdown=5))
|
||||||
|
|
||||||
|
|
||||||
def start_tray():
|
def start_tray():
|
||||||
|
|||||||
@@ -14,18 +14,24 @@ async def lifespan(app: FastAPI):
|
|||||||
定义应用的生命周期事件
|
定义应用的生命周期事件
|
||||||
"""
|
"""
|
||||||
print("Starting up...")
|
print("Starting up...")
|
||||||
|
# 启动模块
|
||||||
start_modules(app)
|
start_modules(app)
|
||||||
|
# 初始化路由
|
||||||
init_routers(app)
|
init_routers(app)
|
||||||
|
# 初始化插件
|
||||||
plugin_init_task = asyncio.create_task(init_plugins_async())
|
plugin_init_task = asyncio.create_task(init_plugins_async())
|
||||||
try:
|
try:
|
||||||
|
# 在此处 yield,表示应用已经启动,控制权交回 FastAPI 主事件循环
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
print("Shutting down...")
|
print("Shutting down...")
|
||||||
try:
|
try:
|
||||||
|
# 取消插件初始化
|
||||||
plugin_init_task.cancel()
|
plugin_init_task.cancel()
|
||||||
await plugin_init_task
|
await plugin_init_task
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
print("Plugin installation task cancelled.")
|
print("Plugin installation task cancelled.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error during plugin installation shutdown: {e}")
|
print(f"Error during plugin installation shutdown: {e}")
|
||||||
|
# 清理模块
|
||||||
shutdown_modules(app)
|
shutdown_modules(app)
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import signal
|
|
||||||
import sys
|
import sys
|
||||||
from types import FrameType
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
from app.core.config import settings, global_vars
|
from app.core.config import global_vars, settings
|
||||||
from app.core.module import ModuleManager
|
from app.core.module import ModuleManager
|
||||||
from app.utils.system import SystemUtils
|
from app.utils.system import SystemUtils
|
||||||
|
|
||||||
@@ -89,27 +87,12 @@ def check_auth():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def singal_handle():
|
|
||||||
"""
|
|
||||||
监听停止信号
|
|
||||||
"""
|
|
||||||
|
|
||||||
def stop_event(signum: int, _: FrameType):
|
|
||||||
"""
|
|
||||||
SIGTERM信号处理
|
|
||||||
"""
|
|
||||||
print(f"接收到停止信号:{signum},正在停止系统...")
|
|
||||||
global_vars.stop_system()
|
|
||||||
|
|
||||||
# 设置信号处理程序
|
|
||||||
signal.signal(signal.SIGTERM, stop_event)
|
|
||||||
signal.signal(signal.SIGINT, stop_event)
|
|
||||||
|
|
||||||
|
|
||||||
def shutdown_modules(_: FastAPI):
|
def shutdown_modules(_: FastAPI):
|
||||||
"""
|
"""
|
||||||
服务关闭
|
服务关闭
|
||||||
"""
|
"""
|
||||||
|
# 停止信号
|
||||||
|
global_vars.stop_system()
|
||||||
# 停止模块
|
# 停止模块
|
||||||
ModuleManager().stop()
|
ModuleManager().stop()
|
||||||
# 停止插件
|
# 停止插件
|
||||||
@@ -159,5 +142,3 @@ def start_modules(_: FastAPI):
|
|||||||
start_frontend()
|
start_frontend()
|
||||||
# 检查认证状态
|
# 检查认证状态
|
||||||
check_auth()
|
check_auth()
|
||||||
# 监听停止信号
|
|
||||||
singal_handle()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user