fix: 修复设置系统脱节、队列计数漂移、资源泄露等全量问题
- 统一设置系统:create_scheduler_service 读取 DB 设置覆盖默认值 - 修复 ProxyRepository.update_score 误删所有无效代理的 SQL - ValidationQueue:修复 Worker 计数漂移与启动恢复任务饿死 - SchedulerService:移除 drain() 阻塞,主循环可正常响应 stop - TaskService:在调度器周期内自动清理过期任务,防止内存泄漏 - lifespan/conftest:规范关闭顺序,消除 Event loop closed 警告 - Repository:异常日志增加 exc_info,今日新增按 created_at 统计 - ValidatorService:防止 HTTP session 重复关闭,移除 SOCKS 多余 close - 前端:补全 pluginsStore.isEmpty,ProxyList 最低分数上限改为 100 - 删除 config.py 中冗余的 cors_origins_list property
This commit is contained in:
@@ -6,7 +6,7 @@ from app.core.db import init_db, get_db
|
||||
from app.core.config import settings as app_settings
|
||||
from app.core.log import logger
|
||||
from app.api.deps import create_scheduler_service
|
||||
from app.repositories.settings_repo import SettingsRepository
|
||||
from app.repositories.settings_repo import SettingsRepository, DEFAULT_SETTINGS
|
||||
|
||||
settings_repo = SettingsRepository()
|
||||
|
||||
@@ -17,33 +17,48 @@ async def lifespan(app: FastAPI):
|
||||
# 初始化数据库
|
||||
await init_db()
|
||||
|
||||
# 创建调度器并挂载到 app.state
|
||||
scheduler_service = create_scheduler_service()
|
||||
# 加载设置并决定是否启动调度器
|
||||
db_settings = DEFAULT_SETTINGS.copy()
|
||||
try:
|
||||
async with get_db() as db:
|
||||
db_settings = await settings_repo.get_all(db)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load settings on startup: {e}")
|
||||
|
||||
# 创建调度器并挂载到 app.state(使用 DB 设置覆盖默认值)
|
||||
scheduler_service = create_scheduler_service(db_settings)
|
||||
app.state.scheduler_service = scheduler_service
|
||||
app.state.validation_queue = scheduler_service.validation_queue
|
||||
|
||||
# 加载设置并决定是否启动调度器
|
||||
try:
|
||||
async with get_db() as db:
|
||||
settings = await settings_repo.get_all(db)
|
||||
scheduler_service.interval_minutes = settings.get(
|
||||
"validate_interval_minutes", app_settings.validator_timeout
|
||||
)
|
||||
if settings.get("auto_validate", True):
|
||||
if db_settings.get("auto_validate", True):
|
||||
try:
|
||||
await scheduler_service.start()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load settings on startup: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start scheduler on startup: {e}")
|
||||
|
||||
logger.info("API server started")
|
||||
yield
|
||||
|
||||
# 关闭调度器
|
||||
if scheduler_service._validate_task and not scheduler_service._validate_task.done():
|
||||
scheduler_service._validate_task.cancel()
|
||||
try:
|
||||
await scheduler_service._validate_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
scheduler_service.cancel_validate_task()
|
||||
await scheduler_service.stop()
|
||||
await scheduler_service.validation_queue.validator.close()
|
||||
|
||||
# 关闭验证器 HTTP session
|
||||
try:
|
||||
await scheduler_service.validation_queue.validator.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 关闭所有插件的 HTTP 客户端
|
||||
from app.core.plugin_system.registry import registry
|
||||
for plugin in registry.list_plugins():
|
||||
if hasattr(plugin, "close"):
|
||||
try:
|
||||
await plugin.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 给 aiosqlite / aiohttp 后台线程留出收尾时间
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
logger.info("API server shutdown")
|
||||
|
||||
Reference in New Issue
Block a user