fix memory manager startup

This commit is contained in:
jxxghp
2025-06-03 16:19:38 +08:00
parent 4be8c70f23
commit 079a747210
3 changed files with 27 additions and 21 deletions
+2 -18
View File
@@ -1,7 +1,7 @@
import gc
import re import re
import traceback import traceback
from typing import Dict, List, Union, Optional from typing import Dict, List, Union, Optional
import gc
from cachetools import cached, TTLCache from cachetools import cached, TTLCache
@@ -12,10 +12,10 @@ from app.core.context import TorrentInfo, Context, MediaInfo
from app.core.metainfo import MetaInfo from app.core.metainfo import MetaInfo
from app.db.site_oper import SiteOper from app.db.site_oper import SiteOper
from app.db.systemconfig_oper import SystemConfigOper from app.db.systemconfig_oper import SystemConfigOper
from app.helper.memory import memory_optimized, clear_large_objects
from app.helper.rss import RssHelper from app.helper.rss import RssHelper
from app.helper.sites import SitesHelper from app.helper.sites import SitesHelper
from app.helper.torrent import TorrentHelper from app.helper.torrent import TorrentHelper
from app.helper.memory import MemoryManager, memory_optimized, clear_large_objects
from app.log import logger from app.log import logger
from app.schemas import Notification from app.schemas import Notification
from app.schemas.types import SystemConfigKey, MessageChannel, NotificationType, MediaType from app.schemas.types import SystemConfigKey, MessageChannel, NotificationType, MediaType
@@ -39,11 +39,6 @@ class TorrentsChain(ChainBase, metaclass=Singleton):
self.systemconfig = SystemConfigOper() self.systemconfig = SystemConfigOper()
self.mediachain = MediaChain() self.mediachain = MediaChain()
self.torrenthelper = TorrentHelper() self.torrenthelper = TorrentHelper()
# 初始化内存管理器
self.memory_manager = MemoryManager()
# 设置内存阈值和启动监控
self.memory_manager.set_threshold(settings.CACHE_CONF['memory'])
self.memory_manager.start_monitoring()
def __del__(self): def __del__(self):
""" """
@@ -93,8 +88,6 @@ class TorrentsChain(ChainBase, metaclass=Singleton):
logger.info(f'开始清理种子缓存数据 ...') logger.info(f'开始清理种子缓存数据 ...')
self.remove_cache(self._spider_file) self.remove_cache(self._spider_file)
self.remove_cache(self._rss_file) self.remove_cache(self._rss_file)
# 强制垃圾回收
self.memory_manager.force_gc()
logger.info(f'种子缓存数据清理完成') logger.info(f'种子缓存数据清理完成')
@cached(cache=TTLCache(maxsize=64, ttl=300)) @cached(cache=TTLCache(maxsize=64, ttl=300))
@@ -189,8 +182,6 @@ class TorrentsChain(ChainBase, metaclass=Singleton):
indexers = self.siteshelper.get_indexers() indexers = self.siteshelper.get_indexers()
# 需要刷新的站点domain # 需要刷新的站点domain
domains = [] domains = []
# 处理计数器,用于定期内存检查
processed_count = 0
# 遍历站点缓存资源 # 遍历站点缓存资源
for indexer in indexers: for indexer in indexers:
@@ -256,15 +247,8 @@ class TorrentsChain(ChainBase, metaclass=Singleton):
torrents_cache[domain] = torrents_cache[domain][-settings.CACHE_CONF["torrents"]:] torrents_cache[domain] = torrents_cache[domain][-settings.CACHE_CONF["torrents"]:]
# 清理旧对象 # 清理旧对象
clear_large_objects(*old_contexts) clear_large_objects(*old_contexts)
# 优化:清理不再需要的临时变量 # 优化:清理不再需要的临时变量
del meta, mediainfo, context del meta, mediainfo, context
# 每处理一定数量的种子后检查内存
processed_count += 1
if processed_count % 10 == 0:
self.memory_manager.check_memory_and_cleanup()
# 回收资源 # 回收资源
del torrents del torrents
# 定期执行垃圾回收 # 定期执行垃圾回收
+6 -3
View File
@@ -4,8 +4,8 @@ from contextlib import asynccontextmanager
from fastapi import FastAPI from fastapi import FastAPI
from app.core.config import global_vars from app.core.config import global_vars
from app.chain.torrents import TorrentsChain
from app.startup.command_initializer import init_command, stop_command, restart_command from app.startup.command_initializer import init_command, stop_command, restart_command
from app.startup.memory_initializer import init_memory_manager, stop_memory_manager
from app.startup.modules_initializer import init_modules, stop_modules from app.startup.modules_initializer import init_modules, stop_modules
from app.startup.monitor_initializer import stop_monitor, init_monitor from app.startup.monitor_initializer import stop_monitor, init_monitor
from app.startup.plugins_initializer import init_plugins, stop_plugins, sync_plugins from app.startup.plugins_initializer import init_plugins, stop_plugins, sync_plugins
@@ -35,8 +35,6 @@ async def lifespan(app: FastAPI):
init_modules() init_modules()
# 初始化路由 # 初始化路由
init_routers(app) init_routers(app)
# 资源缓存
TorrentsChain()
# 初始化插件 # 初始化插件
init_plugins() init_plugins()
# 初始化定时器 # 初始化定时器
@@ -47,6 +45,8 @@ async def lifespan(app: FastAPI):
init_command() init_command()
# 初始化工作流 # 初始化工作流
init_workflow() init_workflow()
# 初始化内存管理
init_memory_manager()
# 插件同步到本地 # 插件同步到本地
sync_plugins_task = asyncio.create_task(init_plugin_system()) sync_plugins_task = asyncio.create_task(init_plugin_system())
try: try:
@@ -56,6 +56,7 @@ async def lifespan(app: FastAPI):
print("Shutting down...") print("Shutting down...")
# 停止信号 # 停止信号
global_vars.stop_system() global_vars.stop_system()
# 取消同步插件任务
try: try:
sync_plugins_task.cancel() sync_plugins_task.cancel()
await sync_plugins_task await sync_plugins_task
@@ -63,6 +64,8 @@ async def lifespan(app: FastAPI):
pass pass
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
# 停止内存管理器
stop_memory_manager()
# 停止工作流 # 停止工作流
stop_workflow() stop_workflow()
# 停止命令 # 停止命令
+19
View File
@@ -0,0 +1,19 @@
from app.core.config import settings
from app.helper.memory import MemoryManager
def init_memory_manager():
"""
初始化内存监控器
"""
memory_manager = MemoryManager()
# 设置内存阈值和启动监控
memory_manager.set_threshold(settings.CACHE_CONF['memory'])
memory_manager.start_monitoring()
def stop_memory_manager():
"""
停止内存监控器
"""
MemoryManager().stop_monitoring()