Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(material): isolate failed terms with serial search workers

Rudy Celeklid17ed3e7f7b4
diff --git a/app/services/material.py b/app/services/material.py
index 27ae372c..da2492c3 100644
--- a/app/services/material.py
+++ b/app/services/material.py
@@ -1925,11 +1925,19 @@ def _search_terms_in_parallel(
if workers == 1:
results = []
for search_term in search_terms:
- items = search_videos(
- search_term=search_term,
- minimum_duration=minimum_duration,
- video_aspect=video_aspect,
- )
+ try:
+ items = search_videos(
+ search_term=search_term,
+ minimum_duration=minimum_duration,
+ video_aspect=video_aspect,
+ )
+ except Exception as exc:
+ logger.error(
+ "failed to search material videos: "
+ f"search_term={search_term!r}, "
+ f"error={type(exc).__name__}"
+ )
+ items = []
logger.info(f"found {len(items)} videos for '{search_term}'")
results.append((search_term, items))
return results
diff --git a/test/services/test_serial_material_search.py b/test/services/test_serial_material_search.py
new file mode 100644
index 00000000..6d53fb37
--- /dev/null
+++ b/test/services/test_serial_material_search.py
@@ -0,0 +1,30 @@
+from unittest.mock import Mock
+
+import pytest
+from app.models.schema import MaterialInfo, VideoAspect
+from app.services import material
+
+
[email protected]("workers", [1, 2])
+def test_failed_term_does_not_abort_remaining_stock_search(monkeypatch, workers):
+ monkeypatch.setattr(material, "_get_material_concurrency", lambda: workers)
+ healthy = MaterialInfo(url="https://example.test/clip.mp4", duration=5)
+ def find_term(search_term, *args, **kwargs):
+ if search_term == "bad term":
+ raise ValueError("malformed provider response")
+ return [healthy]
+
+ search = Mock(side_effect=find_term)
+ result = material._search_terms_in_parallel(
+ ["bad term", "healthy term"], search, 5, VideoAspect.portrait
+ )
+ assert result == [("bad term", []), ("healthy term", [healthy])]
+ assert search.call_count == 2
+
+
+def test_serial_success_preserves_order_and_empty_results(monkeypatch):
+ monkeypatch.setattr(material, "_get_material_concurrency", lambda: 1)
+ healthy = MaterialInfo(url="https://example.test/clip.mp4", duration=5)
+ assert material._search_terms_in_parallel(
+ ["empty", "healthy"], Mock(side_effect=[[], [healthy]]), 5, VideoAspect.portrait
+ ) == [("empty", []), ("healthy", [healthy])]