refactor: route monitor and resource settings through runtime

This commit is contained in:
jxxghp
2026-08-23 01:59:37 +08:00
parent 2c6e8181c2
commit b4e003f098
11 changed files with 68 additions and 47 deletions
+6 -3
View File
@@ -6,7 +6,6 @@ from typing import Any, Dict, List, Optional, Tuple
from app.chain.transfer import TransferChain
from app.runtime.cache import TTLCache
from app.runtime.config import settings
from app.application.directory import DirectoryHelper
from app.application.history import (
HistoryGateAction,
@@ -21,6 +20,7 @@ from app.runtime.log import logger
from app.adapters.system.fsproxy import fsproxy
from app.schemas.workflow import FileItem
from app.schemas.types import MediaType
from app.runtime.settings import get_runtime_setting
class TransferDispatcher:
@@ -39,7 +39,10 @@ class TransferDispatcher:
:param cache: 去重缓存,默认使用 10 秒 TTL 缓存
"""
self.all_exts = all_exts if all_exts is not None else (
settings.RMT_MEDIAEXT + settings.RMT_SUBEXT + settings.RMT_AUDIOEXT)
get_runtime_setting("RMT_MEDIAEXT")
+ get_runtime_setting("RMT_SUBEXT")
+ get_runtime_setting("RMT_AUDIOEXT")
)
self._cache = cache if cache is not None else TTLCache(region="monitor", maxsize=1024, ttl=10)
self._lock = Lock()
# 历史查询失败待重试的文件
@@ -76,7 +79,7 @@ class TransferDispatcher:
"""
判断监控事件路径是否需要进入整理链。
"""
if self._has_suffix_in(file_path, settings.DOWNLOAD_TMPEXT):
if self._has_suffix_in(file_path, get_runtime_setting("DOWNLOAD_TMPEXT")):
return False
return self._has_suffix_in(file_path, self.all_exts)
+3 -3
View File
@@ -7,7 +7,6 @@ from typing import Any, Callable, Dict, List, Optional, Tuple
from apscheduler.schedulers.background import BackgroundScheduler
from app.runtime.config import settings
from app.application.directory import DirectoryHelper
from app.application.messaging.message import MessageHelper
from app.runtime.log import logger
@@ -21,6 +20,7 @@ from app.schemas.types import SystemConfigKey
from app.runtime.reload import ConfigReloadMixin
from app.foundation.singleton import SingletonClass
from app.adapters.system.host import SystemUtils
from app.runtime.settings import get_runtime_setting
class Monitor(ConfigReloadMixin, metaclass=SingletonClass):
@@ -172,7 +172,7 @@ class Monitor(ConfigReloadMixin, metaclass=SingletonClass):
logger.info(f"找到 {len(monitor_dirs)} 个目录监控配置")
# 启动定时服务进程
self._scheduler = BackgroundScheduler(timezone=settings.TZ)
self._scheduler = BackgroundScheduler(timezone=get_runtime_setting("TZ"))
mon_storages: Dict[str, List[Path]] = {}
# 本地监控启动结果计数,用于输出真实的启动总结
@@ -285,7 +285,7 @@ class Monitor(ConfigReloadMixin, metaclass=SingletonClass):
# 网络/FUSE 挂载轮询降频,减少监控自身对挂载后端的持续 stat 压力
poll_delay_ms = None
if use_polling and SystemUtils.is_network_filesystem(mon_path):
poll_delay_ms = (settings.MONITOR_POLL_DELAY_NETWORK
poll_delay_ms = (get_runtime_setting("MONITOR_POLL_DELAY_NETWORK")
or LocalDirectoryWatcher.POLL_DELAY_NETWORK_MS)
logger.info(f"检测到网络文件系统,轮询扫描间隔调整为 {poll_delay_ms}ms: {mon_path}")
+4 -2
View File
@@ -3,8 +3,8 @@ import time
from typing import Dict, List, Optional, Tuple
from app.runtime.cache import FileCache
from app.runtime.config import settings
from app.runtime.log import logger
from app.runtime.settings import get_runtime_setting
class SnapshotStore:
@@ -18,7 +18,9 @@ class SnapshotStore:
初始化快照存储。
:param cache: 快照文件缓存,默认使用 CACHE_PATH/snapshots
"""
self._cache = cache if cache is not None else FileCache(base=settings.CACHE_PATH / "snapshots")
self._cache = cache if cache is not None else FileCache(
base=get_runtime_setting("CACHE_PATH") / "snapshots"
)
def save(self, storage: str, snapshot: Dict, file_count: int = 0,
last_snapshot_time: Optional[float] = None,
+2 -2
View File
@@ -2,9 +2,9 @@ import platform
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from app.runtime.config import settings
from app.runtime.log import logger
from app.adapters.system.host import SystemUtils
from app.runtime.settings import get_runtime_setting
def count_directory_entries(directory: Path, max_check: int = 10000) -> Tuple[int, int]:
@@ -123,7 +123,7 @@ def decide_monitor_mode(directory: Path,
# 检查网络文件系统
if SystemUtils.is_network_filesystem(directory):
if not settings.MONITOR_NETWORK_FAST_MODE:
if not get_runtime_setting("MONITOR_NETWORK_FAST_MODE"):
return True, "检测到网络文件系统,建议使用兼容模式", None, None
# 用户已确认该挂载支持 inotify,继续走快速模式的系统限制检查
logger.info(f"检测到网络文件系统,但已配置允许快速模式: {directory}")
+4 -2
View File
@@ -7,8 +7,8 @@ from typing import Any, Optional
from watchfiles import Change, DefaultFilter, watch
from app.runtime.config import settings
from app.runtime.log import logger
from app.runtime.settings import get_runtime_setting
@dataclass(frozen=True)
@@ -285,7 +285,9 @@ class LocalDirectoryWatcher:
配置,配置热更新后无需重建监控线程即可生效;解析失败或未配置时回退默认值。
:return: 重扫轮次延迟秒数元组
"""
return self._parse_rescan_delays(getattr(settings, "MONITOR_RESCAN_DELAYS", None))
return self._parse_rescan_delays(
get_runtime_setting("MONITOR_RESCAN_DELAYS")
)
@classmethod
def _parse_rescan_delays(cls, raw: Optional[str]) -> tuple[int, ...]: