利用 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(cache): keep fresh downloads during cleanup
Rudy Celekli9ac475362b47
diff --git a/app/services/cache_manager.py b/app/services/cache_manager.pyindex a1c1280a..849ce6ab 100644--- a/app/services/cache_manager.py+++ b/app/services/cache_manager.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport osimport re+import statimport timefrom dataclasses import dataclassfrom typing import Iterator@@ -52,6 +53,9 @@ class _VideoCacheEntry:name: strsize: intmtime: float+ device: int+ inode: int+ mtime_ns: intdef video_cache_dir() -> str:@@ -103,6 +107,9 @@ def _iter_video_cache_entries(include_temp: bool = False) -> Iterator[_VideoCachname=entry.name,size=stat_result.st_size,mtime=stat_result.st_mtime,+ device=stat_result.st_dev,+ inode=stat_result.st_ino,+ mtime_ns=stat_result.st_mtime_ns,)@@ -210,6 +217,20 @@ def clean_video_cache(max_age_days: int | None = None) -> VideoCacheCleanupResulor os.path.islink(entry.path)):raise ValueError("cache file is outside the managed directory")+ try:+ current = os.stat(entry.path, follow_symlinks=False)+ except FileNotFoundError:+ # Another cleanup already removed the scanned candidate.+ continue+ if not stat.S_ISREG(current.st_mode) or (+ current.st_dev,+ current.st_ino,+ current.st_mtime_ns,+ current.st_size,+ ) != (entry.device, entry.inode, entry.mtime_ns, entry.size):+ # A downloader may atomically publish a new file at this name+ # after the scan. Do not delete the fresh replacement.+ continueos.unlink(entry.path)deleted_count += 1deleted_size += entry.sizediff --git a/test/services/test_cache_manager.py b/test/services/test_cache_manager.pyindex 552332ba..48affb8d 100644--- a/test/services/test_cache_manager.py+++ b/test/services/test_cache_manager.py@@ -94,6 +94,34 @@ class TestVideoCacheManager(unittest.TestCase):self.assertTrue(first.exists())self.assertFalse(second.exists())+ def test_cleanup_preserves_file_replaced_after_scan(self):+ """A fresh atomic download must survive a cleanup that scanned its old file."""+ now = 2_000_000_000.0+ cache_file = self._create_cache_file("a" * 32, 10, now - 40 * 86400)+ original_iterator = cache_manager._iter_video_cache_entries++ def replace_after_scan(include_temp=False):+ scanned_entries = list(original_iterator(include_temp=include_temp))+ replacement = self.cache_dir / "new-download.tmp"+ replacement.write_bytes(b"new complete video")+ os.utime(replacement, (now, now))+ os.replace(replacement, cache_file)+ yield from scanned_entries++ with (+ patch.object(cache_manager.time, "time", return_value=now),+ patch.object(+ cache_manager,+ "_iter_video_cache_entries",+ side_effect=replace_after_scan,+ ),+ ):+ result = cache_manager.clean_video_cache(30)++ self.assertEqual(result.deleted_count, 0)+ self.assertEqual(result.failed_count, 0)+ self.assertEqual(cache_file.read_bytes(), b"new complete video")+def test_cleanup_reclaims_only_stale_atomic_downloads(self):"""An interrupted download must not accumulate without deleting an active one."""now = 2_000_000_000.0