mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 08:26:53 +08:00
fix(plugin): serialize dynamic route projection (#6441)
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
"""FastAPI 动态插件路由适配器。"""
|
||||
|
||||
import asyncio
|
||||
from concurrent.futures import Future, TimeoutError as FutureTimeoutError
|
||||
from threading import Lock
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
@@ -9,6 +12,8 @@ from fastapi.routing import APIRoute
|
||||
class FastAPIDynamicRouteRegistry:
|
||||
"""在 FastAPI 上注册插件自由响应路由,并维护 OpenAPI 缓存。"""
|
||||
|
||||
_dispatch_admission_timeout = 5.0
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app: FastAPI,
|
||||
@@ -19,6 +24,7 @@ class FastAPIDynamicRouteRegistry:
|
||||
prefix: str,
|
||||
protected_routes: set[str],
|
||||
log: Any,
|
||||
event_loop: Callable[[], asyncio.AbstractEventLoop | None] | None = None,
|
||||
) -> None:
|
||||
"""注入应用、插件投影、认证依赖和日志端口。"""
|
||||
self._app = app
|
||||
@@ -29,9 +35,62 @@ class FastAPIDynamicRouteRegistry:
|
||||
self._prefix = prefix
|
||||
self._protected_routes = protected_routes
|
||||
self._logger = log
|
||||
self._event_loop = event_loop
|
||||
|
||||
def update(self, plugin_id: Optional[str], action: str) -> None:
|
||||
"""按插件生命周期新增或移除动态路由。"""
|
||||
"""在主事件循环中按插件生命周期新增或移除动态路由。"""
|
||||
if self._event_loop is None:
|
||||
self._update(plugin_id, action)
|
||||
return
|
||||
target_loop = self._event_loop()
|
||||
if (
|
||||
target_loop is None
|
||||
or not target_loop.is_running()
|
||||
or target_loop.is_closed()
|
||||
):
|
||||
raise RuntimeError("主事件循环未运行,无法更新插件动态路由")
|
||||
try:
|
||||
current_loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
current_loop = None
|
||||
if current_loop is target_loop:
|
||||
self._update(plugin_id, action)
|
||||
return
|
||||
|
||||
completed: Future[None] = Future()
|
||||
dispatch_lock = Lock()
|
||||
dispatch_started = False
|
||||
dispatch_abandoned = False
|
||||
|
||||
def apply_update() -> None:
|
||||
"""在目标 loop 的单个回调中完成路由表与 OpenAPI 投影切换。"""
|
||||
nonlocal dispatch_started
|
||||
with dispatch_lock:
|
||||
if dispatch_abandoned:
|
||||
return
|
||||
dispatch_started = True
|
||||
try:
|
||||
self._update(plugin_id, action)
|
||||
except BaseException as error:
|
||||
completed.set_exception(error)
|
||||
else:
|
||||
completed.set_result(None)
|
||||
|
||||
target_loop.call_soon_threadsafe(apply_update)
|
||||
try:
|
||||
completed.result(timeout=self._dispatch_admission_timeout)
|
||||
except FutureTimeoutError as error:
|
||||
with dispatch_lock:
|
||||
if not dispatch_started:
|
||||
dispatch_abandoned = True
|
||||
raise RuntimeError(
|
||||
"主事件循环未及时接收插件动态路由更新"
|
||||
) from error
|
||||
# 回调一旦开始便不可撤销,等待确定终态以免失败回滚后发生迟到写入。
|
||||
completed.result()
|
||||
|
||||
def _update(self, plugin_id: Optional[str], action: str) -> None:
|
||||
"""执行不可中断的路由表与 OpenAPI 投影更新。"""
|
||||
if action not in {"add", "remove"}:
|
||||
raise ValueError("Action must be 'add' or 'remove'")
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ from app.adapters.web.security.access import (
|
||||
verify_token,
|
||||
)
|
||||
from app.application.security.token import create_access_token, decode_access_token
|
||||
from app.runtime.config import global_vars
|
||||
from app.runtime.settings import RuntimeSettingsCompat
|
||||
|
||||
settings = RuntimeSettingsCompat()
|
||||
@@ -393,6 +394,7 @@ def create_app() -> FastAPI:
|
||||
"/redoc",
|
||||
},
|
||||
log=logger,
|
||||
event_loop=lambda: global_vars.CURRENT_EVENT_LOOP,
|
||||
))
|
||||
|
||||
return _app
|
||||
|
||||
@@ -71,6 +71,7 @@ LegacyPluginImportPreparer = Callable[..., None]
|
||||
PluginInstallReporter = Callable[..., None]
|
||||
SiteAuthLevelProvider = Callable[[], int]
|
||||
PluginCatalogFactory = Callable[["PluginManager"], Any]
|
||||
PluginRouteRefresher = Callable[[str], None]
|
||||
|
||||
|
||||
def _ignore_legacy_diagnostics(**_kwargs) -> None:
|
||||
@@ -109,6 +110,11 @@ def _warn_if_plugin_enabled_gil(
|
||||
)
|
||||
|
||||
|
||||
def _unavailable_plugin_route_refresher(_plugin_id: str) -> None:
|
||||
"""在 HTTP 组合尚未装配时拒绝发布不完整的热重载投影。"""
|
||||
raise RuntimeError("插件动态路由刷新器尚未由启动组合根装配")
|
||||
|
||||
|
||||
_legacy_diagnostics_configurator: LegacyDiagnosticsConfigurator = (
|
||||
_ignore_legacy_diagnostics
|
||||
)
|
||||
@@ -119,6 +125,7 @@ _legacy_plugin_import_preparer: LegacyPluginImportPreparer = (
|
||||
_plugin_install_reporter: PluginInstallReporter = _ignore_legacy_diagnostics
|
||||
_site_auth_level_provider: SiteAuthLevelProvider = _unavailable_site_auth_level
|
||||
_plugin_catalog_factory: PluginCatalogFactory = _unavailable_plugin_catalog_factory
|
||||
_plugin_route_refresher: PluginRouteRefresher = _unavailable_plugin_route_refresher
|
||||
|
||||
|
||||
def configure_plugin_legacy_import_services(
|
||||
@@ -158,6 +165,12 @@ def configure_plugin_catalog_factory(factory: PluginCatalogFactory) -> None:
|
||||
_plugin_catalog_factory = factory
|
||||
|
||||
|
||||
def configure_plugin_route_refresher(refresher: PluginRouteRefresher) -> None:
|
||||
"""由启动组合根注入热重载后的动态路由投影刷新器。"""
|
||||
global _plugin_route_refresher
|
||||
_plugin_route_refresher = refresher
|
||||
|
||||
|
||||
@observe_compat_facade("PluginManager")
|
||||
class PluginManager(ConfigReloadMixin, metaclass=Singleton):
|
||||
"""插件管理器"""
|
||||
@@ -656,7 +669,7 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
|
||||
monitor_suppressed=self.is_plugin_monitor_suppressed,
|
||||
local_candidate=self._get_local_plugin_candidate_from_path,
|
||||
sync_local=self._sync_local_plugin_if_installed,
|
||||
reload_plugin=self.reload_plugin_tree,
|
||||
reload_plugin=self._reload_plugin_tree_from_monitor,
|
||||
dependency_manifest_status=(
|
||||
get_plugin_system().dependency_manifest_status
|
||||
),
|
||||
@@ -664,6 +677,18 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
|
||||
log=logger,
|
||||
).run()
|
||||
|
||||
def _reload_plugin_tree_from_monitor(
|
||||
self,
|
||||
plugin_id: str,
|
||||
) -> PluginRuntimeStatus:
|
||||
"""重载源码树,并发布源插件及虚拟实例的动态路由投影。"""
|
||||
with self.mutation("热重载插件路由"):
|
||||
status = self.reload_plugin_tree(plugin_id)
|
||||
reload_targets = self.get_plugin_reload_targets(plugin_id)
|
||||
for reload_target in reload_targets:
|
||||
_plugin_route_refresher(reload_target)
|
||||
return status
|
||||
|
||||
def _get_federated_plugin_change(
|
||||
self,
|
||||
event_path: Path,
|
||||
|
||||
@@ -32,6 +32,7 @@ from app.application.agentdata import get_agent_task_port
|
||||
from app.application.database import get_database_governance
|
||||
from app.application.outbox import dispatch_pending_outbox
|
||||
from app.application.plugin.runtime import get_plugin_manager
|
||||
from app.application.plugin.routes import register_plugin_api
|
||||
from app.application.configuration import (
|
||||
SchedulerRuntimeConfig,
|
||||
get_configured_system_config,
|
||||
@@ -152,6 +153,8 @@ class Scheduler(ConfigReloadMixin, metaclass=SingletonClass):
|
||||
self._auth_count = 0
|
||||
# 用户认证失败消息发送
|
||||
self._auth_message = False
|
||||
# 插件已按认证结果重建,但动态路由尚未完成投影时保留重试状态。
|
||||
self._auth_plugin_routes_pending = False
|
||||
|
||||
async def on_config_changed(self) -> None:
|
||||
"""
|
||||
@@ -2054,6 +2057,9 @@ class Scheduler(ConfigReloadMixin, metaclass=SingletonClass):
|
||||
"""
|
||||
config = get_scheduler_runtime_config()
|
||||
if SitesHelper().auth_level >= 2:
|
||||
if self._auth_plugin_routes_pending:
|
||||
register_plugin_api()
|
||||
self._auth_plugin_routes_pending = False
|
||||
return
|
||||
# 最大重试次数
|
||||
__max_try__ = 30
|
||||
@@ -2088,6 +2094,9 @@ class Scheduler(ConfigReloadMixin, metaclass=SingletonClass):
|
||||
# 认证通过后重新初始化插件
|
||||
get_plugin_manager().init_config()
|
||||
self.init_plugin_jobs()
|
||||
self._auth_plugin_routes_pending = True
|
||||
register_plugin_api()
|
||||
self._auth_plugin_routes_pending = False
|
||||
|
||||
else:
|
||||
self._auth_count += 1
|
||||
|
||||
@@ -15,6 +15,7 @@ from app.runtime.extensions.plugin_manager import (
|
||||
configure_plugin_catalog_factory,
|
||||
configure_plugin_install_reporter,
|
||||
configure_plugin_legacy_import_services,
|
||||
configure_plugin_route_refresher,
|
||||
configure_plugin_resource_import_preparer,
|
||||
configure_site_auth_level_provider,
|
||||
)
|
||||
@@ -95,6 +96,7 @@ def configure_plugin_services() -> None:
|
||||
lambda: get_configured_system_config().get(SystemConfigKey.UserInstalledPlugins) or []
|
||||
)
|
||||
configure_plugin_catalog_factory(_build_plugin_catalog)
|
||||
configure_plugin_route_refresher(register_plugin_api)
|
||||
configure_plugin_system(PluginSystemServices(
|
||||
market=market_client,
|
||||
package=PluginPackageManager(plugin_helper),
|
||||
|
||||
Reference in New Issue
Block a user