Files
MoviePilot/app/monitor/snapshot.py
Aqr-K a2e70b443d fix(monitor,transfer): 修复 FUSE 挂载无响应导致的监控冻死、整理链锁死与漏件 (#6276)
* wip(v3): 移植监控与整理韧性修复到 v3 基线

包含:监控看门狗隔离/挂载探测、整理队列持久化、文件系统子进程代理、
写入原子化。迁移重挂到 v3 链 8a4c7e1d2f90 -> 7f5c1d2e3a4b -> e3d9f4b7c806。
tmdb 相关测试尚未通过,待定位。

* fix(v3): 修正移植引入的 16 项测试失败

- poller.py:合并时我方保留的行仍用旧变量名 merged_snapshot,而 v3 已统一
  改名为 current_snapshot,导致 NameError 被外层 except 吞掉、快照从未保存
- smb.py:采纳 f-string 拆分写法,恢复 Python 3.11 可解析
- dispatcher 测试:历史查重由 _should_skip_by_history 统一承担,mock 点随之调整
- tmdb 缓存测试:补充 v3 新增的 media_source/media_id 字段
- tmdb 重试测试:为 fake 补充 match_multi/async_match_multi

尚余 3 项与 v3 识别流程的连接失败处理有关,待单独判断。

* fix(v3): 测试适配 v3 的 media_source/media_id 重构

v3 将媒体标识从 tmdbid 统一重构为 media_source + media_id,recognize_media
的 tmdbid 参数已被 **kwargs 静默吞掉——传了也不生效,流程会误降级到名称搜索。
tmdb 重试用例改用新参数后恢复正确路径。

同时修正 fake 的 match_multi 语义:真实实现(tmdbapi.match_multi)吞掉所有
异常并返回 None,连接失败与「未找到」在该路径上本就不可区分,fake 需保持一致。

至此移植引入的 19 项失败全部清零。

---------

Co-authored-by: Aqr-K <Aqr-K@users.noreply.github.com>
2026-08-13 08:19:54 +08:00

168 lines
6.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import time
from typing import Dict, List, Optional, Tuple
from app.core.cache import FileCache
from app.core.config import settings
from app.log import logger
class SnapshotStore:
"""
远程目录监控快照的存取与比对。
"""
VERSION = 2
def __init__(self, cache: Optional[FileCache] = None):
"""
初始化快照存储。
:param cache: 快照文件缓存,默认使用 CACHE_PATH/snapshots
"""
self._cache = cache if cache is not None else FileCache(base=settings.CACHE_PATH / "snapshots")
def save(self, storage: str, snapshot: Dict, file_count: int = 0,
last_snapshot_time: Optional[float] = None,
snapshot_time: Optional[float] = None) -> bool:
"""
保存快照到文件缓存。
:param storage: 存储名称
:param snapshot: 快照数据
:param file_count: 文件数量,用于调整监控间隔
:param last_snapshot_time: 上次快照时间戳
:param snapshot_time: 强制指定的增量游标,用于部分路径失败时固定游标不前进
:return: 是否保存成功
"""
try:
if snapshot_time is None:
# 取「上次游标」与「本轮最大 mtime」的较大者本轮全是旧文件时
# 游标不能回退,否则已处理过的变更会被重新判定为新增
snapshot_time = max(
last_snapshot_time or 0,
max((item.get('modify_time', 0) for item in snapshot.values()), default=0)
)
if not snapshot_time:
snapshot_time = time.time()
snapshot_data = {
'version': self.VERSION,
'timestamp': snapshot_time,
'file_count': file_count,
'snapshot': snapshot
}
cache_key = f"{storage}_snapshot"
snapshot_json = json.dumps(snapshot_data, ensure_ascii=False, indent=2)
self._cache.set(cache_key, snapshot_json.encode('utf-8'), region="snapshots")
logger.debug(f"快照已保存到缓存: {storage}")
return True
except Exception as e:
logger.error(f"保存快照失败: {e}")
return False
def load_checked(self, storage: str) -> Tuple[Optional[Dict], bool]:
"""
从文件缓存加载快照,并区分「快照不存在」与「读取失败」。
读取失败时不能当作首次快照处理,否则会静默丢弃已有基线。
:param storage: 存储名称
:return: (快照数据或None, 是否读取成功)
"""
try:
cache_key = f"{storage}_snapshot"
snapshot_data = self._cache.get(cache_key, region="snapshots")
if snapshot_data:
data = json.loads(snapshot_data.decode('utf-8'))
logger.debug(f"成功加载快照: {storage}, 包含 {len(data.get('snapshot', {}))} 个文件")
return data, True
logger.debug(f"快照文件不存在: {storage}")
return None, True
except Exception as e:
logger.error(f"加载快照失败: {e}")
return None, False
def load(self, storage: str) -> Optional[Dict]:
"""
从文件缓存加载快照。
:param storage: 存储名称
:return: 快照数据或None
"""
data, _ = self.load_checked(storage)
return data
def reset(self, storage: str) -> bool:
"""
重置快照,强制下次扫描时重新建立基准。
:param storage: 存储名称
:return: 是否成功
"""
try:
cache_key = f"{storage}_snapshot"
if self._cache.exists(cache_key, region="snapshots"):
self._cache.delete(cache_key, region="snapshots")
logger.info(f"快照已重置: {storage}")
return True
logger.debug(f"快照文件不存在,无需重置: {storage}")
return True
except Exception as e:
logger.error(f"重置快照失败: {storage} - {e}")
return False
@staticmethod
def compare(old_snapshot: Dict, new_snapshot: Dict) -> Dict[str, List]:
"""
比对快照,找出变化的文件(只处理新增和修改,不处理删除)。
:param old_snapshot: 旧快照
:param new_snapshot: 新快照
:return: 变化信息
"""
changes = {
'added': [],
'modified': []
}
old_files = set(old_snapshot.keys())
new_files = set(new_snapshot.keys())
# 新增文件
changes['added'] = list(new_files - old_files)
# 修改文件(大小或时间变化)
for file_path in old_files & new_files:
old_info = old_snapshot[file_path]
new_info = new_snapshot[file_path]
# 检查文件大小变化
old_size = old_info.get('size', 0) if isinstance(old_info, dict) else old_info
new_size = new_info.get('size', 0) if isinstance(new_info, dict) else new_info
# 检查修改时间变化(如果有的话)
old_time = old_info.get('modify_time', 0) if isinstance(old_info, dict) else 0
new_time = new_info.get('modify_time', 0) if isinstance(new_info, dict) else 0
# 支持文件唯一标识的存储器可用它识别同大小且修改时间未变化的替换文件。
# 旧快照缺少 fileid 时保持保守,避免升级后首次补齐元数据触发全量重整。
old_fileid = old_info.get('fileid') if isinstance(old_info, dict) else None
new_fileid = new_info.get('fileid') if isinstance(new_info, dict) else None
if (
old_size != new_size
or (old_time and new_time and old_time != new_time)
or (old_fileid and new_fileid and old_fileid != new_fileid)
):
changes['modified'].append(file_path)
return changes
@staticmethod
def adjust_interval(file_count: int) -> int:
"""
根据文件数量动态调整监控间隔。
:param file_count: 文件数量
:return: 监控间隔(分钟)
"""
if file_count < 100:
return 5 # 5分钟
elif file_count < 500:
return 10 # 10分钟
elif file_count < 1000:
return 15 # 15分钟
else:
return 30 # 30分钟