利用 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(queue): dispatch waiting task before accepting new work
Rudy Celekli0914b0062e8e
diff --git a/app/controllers/manager/base_manager.py b/app/controllers/manager/base_manager.pyindex 6ceb7a70..079767de 100644--- a/app/controllers/manager/base_manager.py+++ b/app/controllers/manager/base_manager.py@@ -97,34 +97,37 @@ class TaskManager:def check_queue(self):with self.lock:- if (- self.current_tasks < self.max_concurrent_tasks- and not self.is_queue_empty()- ):- task_info = self.dequeue()- if task_info is None:- # dequeue() may skip and discard queue entries that no longer- # pass current validation (see RedisTaskManager.dequeue) and- # return None once nothing usable is left, even though- # is_queue_empty() was False a moment earlier.- return- func = task_info["func"]- args = task_info.get("args", ())- kwargs = task_info.get("kwargs", {})- # 与直接创建任务保持同一计数时机,避免刚出队的任务尚未在线程- # 内计数时,又有新请求绕过队列占用同一个并发名额。- self.current_tasks += 1- try:- self.execute_task(func, *args, **kwargs)- except Exception:- self.current_tasks -= 1- self.enqueue(task_info)- raise+ self._check_queue_locked()++ def _check_queue_locked(self):+ """Dispatch one queued task while the caller holds ``self.lock``."""+ if (+ self.current_tasks < self.max_concurrent_tasks+ and not self.is_queue_empty()+ ):+ task_info = self.dequeue()+ if task_info is None:+ # dequeue() may skip and discard queue entries that no longer+ # pass current validation (see RedisTaskManager.dequeue) and+ # return None once nothing usable is left, even though+ # is_queue_empty() was False a moment earlier.+ return+ func = task_info["func"]+ args = task_info.get("args", ())+ kwargs = task_info.get("kwargs", {})+ # Reserve the freed slot before another add_task can claim it.+ self.current_tasks += 1+ try:+ self.execute_task(func, *args, **kwargs)+ except Exception:+ self.current_tasks -= 1+ self.enqueue(task_info)+ raisedef task_done(self):with self.lock:self.current_tasks -= 1- self.check_queue()+ self._check_queue_locked()def enqueue(self, task: Dict):raise NotImplementedError()diff --git a/test/services/test_task_manager.py b/test/services/test_task_manager.pyindex 15dce2cb..68c7a0ef 100644--- a/test/services/test_task_manager.py+++ b/test/services/test_task_manager.py@@ -1,4 +1,5 @@import json+import threadingimport unittestfrom unittest.mock import MagicMock, patch@@ -84,6 +85,55 @@ class TestInMemoryTaskManager(unittest.TestCase):execute_task.assert_called_once_with(len, [1, 2])self.assertTrue(manager.is_queue_empty())+ def test_new_request_cannot_take_slot_before_waiting_task(self):+ """A completion must reserve its freed slot for the oldest queued task."""+ manager = InMemoryTaskManager(max_concurrent_tasks=1, max_queued_tasks=2)+ manager.current_tasks = 1++ def queued_task():+ pass++ def incoming_task():+ pass++ manager.enqueue({"func": queued_task, "args": (), "kwargs": {}})+ released = threading.Event()+ resume = threading.Event()++ class PausingLock:+ def __init__(self):+ self.lock = threading.Lock()+ self.worker = None+ self.paused = False++ def __enter__(self):+ self.lock.acquire()+ return self++ def __exit__(self, exc_type, exc_value, traceback):+ self.lock.release()+ if threading.current_thread() is self.worker and not self.paused:+ self.paused = True+ released.set()+ resume.wait(timeout=2)++ manager.lock = PausingLock()+ started = []+ with patch.object(manager, "execute_task", side_effect=lambda func: started.append(func)):+ worker = threading.Thread(target=manager.task_done)+ manager.lock.worker = worker+ worker.start()+ try:+ self.assertTrue(released.wait(timeout=2))+ manager.add_task(incoming_task)+ finally:+ resume.set()+ worker.join(timeout=2)++ self.assertFalse(worker.is_alive())+ self.assertEqual(started, [queued_task])+ self.assertEqual(manager.dequeue()["func"], incoming_task)+def test_task_done_requeues_task_when_thread_cannot_start(self):"""出队后若线程启动失败,应回滚名额并把任务放回队列,避免任务丢失。"""manager = InMemoryTaskManager(max_concurrent_tasks=1, max_queued_tasks=1)