利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(material): reclaim temp files orphaned by interrupted cache writes
c02062707bd3a5b1b5c
diff --git a/app/services/material_cache.py b/app/services/material_cache.pyindex 77cc0c58..9da09386 100644--- a/app/services/material_cache.py+++ b/app/services/material_cache.py@@ -23,6 +23,11 @@ MATERIAL_SEARCH_CACHE_TTL_SECONDS = 24 * 60 * 60_CACHE_FORMAT_VERSION = 2_CACHE_CLEANUP_INTERVAL_SECONDS = 60 * 60_CACHE_FILE_PATTERN = re.compile(r"^[0-9a-f]{64}\.json$")+# 保存缓存时先写入 NamedTemporaryFile(delete=False),再用 os.replace 发布。+# 进程被强制终止(Ctrl+C、容器停止、断电)时 Python 的异常兜底没有机会执行,+# 会留下一个没有被替换的中间文件;它不匹配上面的缓存文件模式,必须单独识别+# 才能回收,否则会永久累积在缓存目录里。+_CACHE_TEMP_FILE_PATTERN = re.compile(r"^\.[0-9a-f]{64}-[a-z0-9_]+\.tmp$")# API 默认允许多个视频任务并发执行。固定数量的锁分片可以让相同搜索条件共用# 一个锁,同时避免按关键词永久保存 Lock 导致内存持续增长。它只负责合并当前@@ -386,11 +391,12 @@ def cleanup_expired_material_search_cache(force: bool = False,) -> int:"""- 低频清理没有再次被查询到的过期搜索缓存。+ 低频清理没有再次被查询到的过期搜索缓存,以及中断写入遗留的临时文件。正常写入路径每小时最多扫描一次目录,避免每次搜索都产生线性目录遍历;- ``force`` 仅供测试或显式维护调用。只删除 SHA-256 命名的 JSON 文件,不会- 触碰用户放入目录的其它文件。+ ``force`` 仅供测试或显式维护调用。只删除 SHA-256 命名的 JSON 缓存文件和本+ 模块自己生成的 ``.tmp`` 中间文件,不会触碰用户放入目录的其它文件。两者共用+ 同一套过期判定,因此仍在写入中的临时文件不会被并发清理误删。"""global _last_cleanup_monotonic@@ -420,7 +426,10 @@ def cleanup_expired_material_search_cache(failed_count = 0with entries:for entry in entries:- if not _CACHE_FILE_PATTERN.fullmatch(entry.name):+ if not (+ _CACHE_FILE_PATTERN.fullmatch(entry.name)+ or _CACHE_TEMP_FILE_PATTERN.fullmatch(entry.name)+ ):continuetry:if not entry.is_file(follow_symlinks=False):diff --git a/test/services/test_material_cache.py b/test/services/test_material_cache.pyindex d06eddf1..663eea49 100644--- a/test/services/test_material_cache.py+++ b/test/services/test_material_cache.py@@ -592,6 +592,55 @@ class TestMaterialSearchCache(unittest.TestCase):self.assertTrue(fresh_path.exists())self.assertTrue(unrelated_path.exists())+ def test_cleanup_removes_orphaned_temp_files(self):+ """+ 进程被强制终止时,NamedTemporaryFile(delete=False) 留下的中间文件没有+ 机会被 os.replace 消费,也不会触发异常兜底删除。它必须和过期缓存一起+ 回收,否则每次异常中断都会在缓存目录里永久累积一个残留文件。+ """+ orphan_path = Path(self.temp_dir.name) / (+ f".{self._cache_path().stem}-m429jzwe.tmp"+ )+ orphan_path.write_text('{"version":2,"items":[]}', encoding="utf-8")+ unrelated_path = Path(self.temp_dir.name) / "notes.tmp"+ unrelated_path.write_text("keep", encoding="utf-8")++ now = 2_000_000_000.0+ stale_mtime = now - material_cache.MATERIAL_SEARCH_CACHE_TTL_SECONDS - 1+ os.utime(orphan_path, (stale_mtime, stale_mtime))+ os.utime(unrelated_path, (stale_mtime, stale_mtime))++ deleted = material_cache.cleanup_expired_material_search_cache(+ now=now,+ force=True,+ )++ self.assertEqual(deleted, 1)+ self.assertFalse(orphan_path.exists())+ # 前缀不匹配的其它文件属于用户,不能被清理逻辑删除。+ self.assertTrue(unrelated_path.exists())++ def test_cleanup_keeps_recent_temp_files(self):+ """+ 并发搜索时另一个进程可能正在写临时文件。清理必须复用缓存的过期判定,+ 让尚未超期的中间文件保持原样,避免删掉正在等待 os.replace 的文件。+ """+ in_flight_path = Path(self.temp_dir.name) / (+ f".{self._cache_path().stem}-m429jzwe.tmp"+ )+ in_flight_path.write_text('{"version":2,"items":', encoding="utf-8")++ now = 2_000_000_000.0+ os.utime(in_flight_path, (now - 1, now - 1))++ deleted = material_cache.cleanup_expired_material_search_cache(+ now=now,+ force=True,+ )++ self.assertEqual(deleted, 0)+ self.assertTrue(in_flight_path.exists())+if __name__ == "__main__":unittest.main()