Skip to content

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

fix(state): write Redis task updates atomically

Rudy Celekliba53c366dc2f
diff --git a/app/services/state.py b/app/services/state.py
index 79a6a312..9fcdcc66 100644
--- a/app/services/state.py
+++ b/app/services/state.py
@@ -171,8 +171,13 @@ class RedisState(BaseState):
**kwargs,
}
- for field, value in fields.items():
- self._redis.hset(task_id, field, str(value))
+ # One HSET writes the whole task state atomically. Separate commands
+ # could expose a new state with the previous progress or result fields
+ # to readers, and leave a partially updated record on network failure.
+ self._redis.hset(
+ task_id,
+ mapping={field: str(value) for field, value in fields.items()},
+ )
def get_task(self, task_id: str):
task_data = self._redis.hgetall(task_id)
diff --git a/test/services/test_state.py b/test/services/test_state.py
index cf07f4d5..c7726dfc 100644
--- a/test/services/test_state.py
+++ b/test/services/test_state.py
@@ -5,6 +5,7 @@ import unittest
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
+from unittest.mock import Mock
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
@@ -149,6 +150,27 @@ class TestMemoryState(unittest.TestCase):
class TestRedisState(unittest.TestCase):
+ def test_update_task_writes_all_fields_in_one_redis_command(self):
+ state = RedisState.__new__(RedisState)
+ state._redis = Mock()
+
+ state.update_task(
+ "task-1",
+ state=const.TASK_STATE_COMPLETE,
+ progress=120,
+ videos=["final.mp4"],
+ )
+
+ state._redis.hset.assert_called_once_with(
+ "task-1",
+ mapping={
+ "task_id": "task-1",
+ "state": str(const.TASK_STATE_COMPLETE),
+ "progress": "100",
+ "videos": "['final.mp4']",
+ },
+ )
+
def _build_state(self, batch_sizes):
keys = [f"task:{i}".encode("utf-8") for i in range(sum(batch_sizes))]
batches = []