refactor backend module architecture

This commit is contained in:
jxxghp
2026-08-14 15:45:38 +08:00
parent 557cc0e2e3
commit 7b3444c366
716 changed files with 10378 additions and 7709 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
from app.agent import agent_manager
from app.core.config import settings
from app.log import logger
from app.agent.orchestrator import agent_manager
from app.platform.config import settings
from app.platform.log import logger
class AgentInitializer:
+6
View File
@@ -0,0 +1,6 @@
from app.infrastructure.cache import configure_platform_cache
def configure_cache_dependencies() -> None:
"""在导入使用缓存装饰器的业务模块前注册具体缓存适配器。"""
configure_platform_cache()
+30
View File
@@ -0,0 +1,30 @@
from app.domain.context import configure_tmdb_image_url_builder
from app.domain.media import configure_search_source_provider
from app.domain.meta.customization import configure_customization_provider
from app.domain.meta.releasegroup import configure_release_groups_provider
from app.domain.meta.runtime import configure_recognition_runtime
from app.domain.meta.words import configure_custom_words_provider
from app.domain.metainfo import clear_rust_parse_options_cache
from app.infrastructure import rust as rust_accelerator
from app.platform.config import settings
from app.services.recognition import RecognitionRuleService
def configure_domain_dependencies() -> None:
"""在组合根集中注入领域模型需要的配置、持久化规则和加速适配器。"""
rule_service = RecognitionRuleService()
configure_customization_provider(rule_service.get_customization)
configure_release_groups_provider(rule_service.get_release_groups)
configure_custom_words_provider(rule_service.get_custom_words)
configure_search_source_provider(lambda: settings.SEARCH_SOURCE)
configure_tmdb_image_url_builder(settings.TMDB_IMAGE_URL)
configure_recognition_runtime(
media_extensions_provider=lambda: (
*settings.RMT_MEDIAEXT,
*settings.RMT_SUBEXT,
*settings.RMT_AUDIOEXT,
),
audio_extensions_provider=lambda: settings.RMT_AUDIOEXT,
accelerator=rust_accelerator,
)
clear_rust_parse_options_cache()
+16 -5
View File
@@ -5,6 +5,9 @@ from typing import Callable
from fastapi import FastAPI
from app.startup.cache_initializer import configure_cache_dependencies
# 缓存装饰器会在业务模块导入时创建后端,必须先完成适配器装配。
configure_cache_dependencies()
# urllib3-future 覆盖 urllib3 命名空间后删除了 format_header_param,导致 telebot 崩溃,需在加载模块前打补丁
try:
import urllib3.fields as _urllib3_fields
@@ -19,11 +22,12 @@ except Exception:
pass
from app.chain.system import SystemChain
from app.core.config import global_vars, settings
from app.helper.server import MoviePilotServerHelper
from app.helper.system import SystemHelper
from app.log import logger, LoggerManager
from app.platform.config import global_vars, settings
from app.integrations.server import MoviePilotServerHelper
from app.platform.runtime import SystemHelper
from app.platform.log import logger, LoggerManager
from app.startup.command_initializer import init_command, stop_command, restart_command
from app.startup.domain_initializer import configure_domain_dependencies
from app.startup.modules_initializer import init_modules, stop_modules
from app.startup.monitor_initializer import stop_monitor, init_monitor
from app.startup.plugins_initializer import init_plugins, stop_plugins, sync_plugins
@@ -35,7 +39,10 @@ from app.startup.scheduler_initializer import (
)
from app.startup.transfer_initializer import replay_pending_transfers
from app.startup.workflow_initializer import init_workflow, stop_workflow
from app.utils.http import aclose_shared_async_transports
from app.foundation.http import (
aclose_shared_async_transports,
configure_default_user_agent,
)
async def init_extra():
@@ -75,6 +82,10 @@ async def lifespan(app: FastAPI):
定义应用的生命周期事件
"""
print("Starting up...")
# HTTP 基础能力不反向读取平台配置,由启动层注入宿主标识。
configure_default_user_agent(settings.USER_AGENT)
# 领域层只消费显式注入的配置和适配器,不自行读取平台或数据库。
configure_domain_dependencies()
# 存储当前循环
global_vars.set_loop(asyncio.get_event_loop())
# 初始化路由
+57 -15
View File
@@ -2,34 +2,61 @@ import inspect
import sys
from typing import Callable
from app.helper.redis import RedisHelper, AsyncRedisHelper
from app.infrastructure.redis import RedisHelper, AsyncRedisHelper
from app.chain.mediaserver import MediaServerChain
from app.chain.tmdb import TmdbChain
# SitesHelper涉及资源包拉取,提前引入并容错提示
try:
from app.helper.sites import SitesHelper # noqa
from app.infrastructure.sites import SitesHelper # noqa
except ImportError as e:
SitesHelper = None
error_message = f"错误: {str(e)}\n站点认证及索引相关资源导入失败,请尝试重建容器或手动拉取资源"
print(error_message, file=sys.stderr)
sys.exit(1)
from app.utils.system import SystemUtils
from app.log import logger
from app.core.config import settings
from app.core.module import ModuleManager
from app.core.event import EventManager
from app.helper.thread import ThreadHelper
from app.helper.display import DisplayHelper
from app.helper.doh import DohHelper
from app.helper.resource import ResourceHelper
from app.helper.message import MessageHelper, stop_message
from app.helper.server import MoviePilotServerHelper
from app.infrastructure.system import SystemUtils
from app.platform.log import logger
from app.platform.config import settings
from app.extensions.module_manager import ModuleManager
from app.platform.events import EventManager
from app.platform.runtime import SystemHelper
from app.platform.thread import ThreadHelper
from app.infrastructure.display import DisplayHelper
from app.infrastructure.doh import DohHelper
from app.infrastructure.resource import ResourceHelper
from app.messaging.message import MessageHelper, stop_message
from app.integrations.server import MoviePilotServerHelper
from app.db import close_database
from app.db.systemconfig_oper import SystemConfigOper
from app.command import CommandChain
from app.schemas import Notification, NotificationType
from app.schemas.types import SystemConfigKey
from app.startup.agent_initializer import init_agent, stop_agent
from app.security.access import set_superuser_token_payload_provider
from app.security.auth import build_superuser_token_payload
from app.services.image import configure_wallpaper_providers
def configure_wallpaper_services() -> None:
"""把需要 Chain 编排的壁纸来源注入图片服务。"""
configure_wallpaper_providers(
tmdb_wallpaper=lambda: TmdbChain().get_random_wallpager(),
tmdb_wallpapers=lambda count: TmdbChain().get_trending_wallpapers(count),
mediaserver_wallpaper=lambda: MediaServerChain().get_latest_wallpaper(),
mediaserver_wallpapers=lambda count: MediaServerChain().get_latest_wallpapers(
count=count
),
)
def notify_event_error(title: str, message: str) -> None:
"""将事件总线错误转发到系统消息通道。"""
MessageHelper().put(
title=title,
message=message,
role="system",
)
def start_frontend():
@@ -127,6 +154,15 @@ def check_auth():
)
def update_resources() -> None:
"""安装可用资源更新,并由组合根统一决定是否重启进程。"""
if ResourceHelper().check() is not True:
return
restarted, message = SystemHelper.restart()
if not restarted:
logger.error(f"资源更新完成但自动重启失败:{message}")
async def stop_modules():
"""
服务关闭
@@ -158,16 +194,22 @@ async def init_modules():
"""
启动模块
"""
# 应用服务不反向依赖 Chain,由启动组合层注入壁纸来源。
configure_wallpaper_services()
# 认证访问层不反向依赖数据库实现,由启动组合层注入载荷提供器。
set_superuser_token_payload_provider(build_superuser_token_payload)
# 虚拟显示
DisplayHelper()
# DoH
DohHelper()
# 站点管理
SitesHelper()
# 资源包检测
ResourceHelper()
# 资源适配器只负责下载安装,是否重启由启动组合层决定。
update_resources()
# 用户认证
user_auth()
# 事件错误通知由启动组合层接入消息服务。
EventManager().set_error_notifier(notify_event_error)
# 加载模块
ModuleManager()
# 启动事件消费
+23 -3
View File
@@ -1,6 +1,24 @@
from app.core.config import global_vars
from app.core.plugin import PluginManager
from app.log import logger
from app.compat.diagnostics import (
configure_legacy_import_diagnostics,
scan_plugin_legacy_imports,
)
from app.platform.config import global_vars
from app.extensions.plugin_manager import (
PluginManager,
configure_plugin_install_reporter,
configure_plugin_legacy_import_services,
)
from app.integrations.server import MoviePilotServerHelper
from app.platform.log import logger
def _configure_plugin_services() -> None:
"""把兼容诊断和远程上报能力装配到插件管理器。"""
configure_plugin_legacy_import_services(
diagnostics_configurator=configure_legacy_import_diagnostics,
import_scanner=scan_plugin_legacy_imports,
)
configure_plugin_install_reporter(MoviePilotServerHelper.install_plugin_reg)
async def sync_plugins() -> bool:
@@ -8,6 +26,7 @@ async def sync_plugins() -> bool:
初始化安装插件,并动态注册后台任务及API
"""
try:
_configure_plugin_services()
loop = global_vars.loop
plugin_manager = PluginManager()
@@ -60,6 +79,7 @@ def init_plugins():
"""
初始化插件
"""
_configure_plugin_services()
PluginManager().start()
register_plugin_api()
+1 -1
View File
@@ -1,6 +1,6 @@
from fastapi import FastAPI
from app.core.config import settings
from app.platform.config import settings
def init_routers(app: FastAPI):