利用 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(redis): resume persisted task queue on API startup
Rudy Celeklif863ce48f4a5
diff --git a/app/asgi.py b/app/asgi.pyindex bfb2b201..a3f222dd 100644--- a/app/asgi.py+++ b/app/asgi.py@@ -41,6 +41,14 @@ async def application_lifespan(_: FastAPI):from app.services import task as task_servicetask_service.recover_interrupted_cross_posts()++ # Redis queue entries persist across API restarts. No task_done callback+ # exists in the new process to dispatch them, so fill its worker slots now.+ from app.controllers.manager.redis_manager import RedisTaskManager+ from app.controllers.v1 import video as video_controller++ if isinstance(video_controller.task_manager, RedisTaskManager):+ video_controller.task_manager.resume_queued_tasks()try:yieldfinally:diff --git a/app/controllers/manager/redis_manager.py b/app/controllers/manager/redis_manager.pyindex fff077f2..4a5b4bec 100644--- a/app/controllers/manager/redis_manager.py+++ b/app/controllers/manager/redis_manager.py@@ -30,6 +30,16 @@ class RedisTaskManager(TaskManager):def create_queue(self):return "task_queue"+ def resume_queued_tasks(self):+ """Dispatch persisted queue entries when a new API process starts.++ Queued Redis items survive a restart, but no worker calls check_queue+ until an active task finishes. Fill each available local worker slot so+ an idle process does not leave old requests stuck in processing.+ """+ for _ in range(max(0, self.max_concurrent_tasks - self.current_tasks)):+ self.check_queue()+def enqueue(self, task: Dict):task_with_serializable_params = task.copy()# task.copy() 只复制最外层字典;如果直接改写嵌套 kwargs,会把调用方diff --git a/test/services/test_controller_video.py b/test/services/test_controller_video.pyindex 24eac1ba..559c0830 100644--- a/test/services/test_controller_video.py+++ b/test/services/test_controller_video.py@@ -14,6 +14,7 @@ from fastapi.testclient import TestClientfrom app import asgifrom app.config import configfrom app.controllers.manager.base_manager import TaskQueueFullError+from app.controllers.manager.redis_manager import RedisTaskManagerfrom app.controllers.v1 import video as video_controllerfrom app.models import constfrom app.models.exception import HttpException@@ -62,6 +63,26 @@ class TestVideoControllerHelpers(unittest.TestCase):recover.assert_called_once_with()+ def test_fastapi_startup_resumes_persisted_redis_queue(self):+ """A restart must dispatch queued Redis work before serving requests."""+ from app.services import task as task_service++ with patch("app.controllers.manager.redis_manager.redis.Redis.from_url"):+ manager = RedisTaskManager(2, "redis://localhost:6379/0")++ with (+ patch.object(video_controller, "task_manager", manager),+ patch.object(manager, "check_queue") as check_queue,+ patch.object(task_service, "recover_interrupted_cross_posts"),+ ):+ async def run_lifespan():+ async with asgi.application_lifespan(asgi.app):+ pass++ asyncio.run(run_lifespan())++ self.assertEqual(check_queue.call_count, 2)+def test_sanitize_upload_filename_rejects_empty_name(self):"""空文件名和目录占位符不能进入服务端存储路径。"""for filename in ("", ".", "..", "/"):diff --git a/test/services/test_task_manager.py b/test/services/test_task_manager.pyindex 90dfd516..15dce2cb 100644--- a/test/services/test_task_manager.py+++ b/test/services/test_task_manager.py@@ -227,6 +227,22 @@ class TestRedisTaskManager(unittest.TestCase):)from_url.assert_called_once_with("redis://localhost:6379/0")+ def test_resume_queued_tasks_fills_available_slots_after_restart(self):+ """Persisted Redis entries should run without waiting for a new API request."""+ self.manager.max_concurrent_tasks = 2+ self.redis_client.llen.return_value = 2+ self.redis_client.lpop.side_effect = [+ _queued_payload("start", task_id="first", params=_video_params()),+ _queued_payload("start", task_id="second", params=_video_params()),+ ]++ with patch.object(self.manager, "execute_task") as execute_task:+ self.manager.resume_queued_tasks()++ self.assertEqual(execute_task.call_count, 2)+ self.assertEqual(self.manager.current_tasks, 2)+ self.assertEqual(self.redis_client.lpop.call_count, 2)+def test_enqueue_serializes_video_params_without_mutating_task(self):"""Redis 只能存 JSON;VideoParams 应转换成字典,但原任务仍需保留模型,