caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

100 lines
4.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from typing import Dict
import redis
from loguru import logger
from pydantic import ValidationError
from app.controllers.manager.base_manager import TaskManager
from app.models import const
from app.models.schema import VideoParams
from app.services import state as sm
from app.services import task as tm
FUNC_MAP = {
"start": tm.start,
# 'start_test': tm.start_test
}
class RedisTaskManager(TaskManager):
def __init__(
self,
max_concurrent_tasks: int,
redis_url: str,
max_queued_tasks: int = 100,
):
self.redis_client = redis.Redis.from_url(redis_url)
super().__init__(max_concurrent_tasks, max_queued_tasks=max_queued_tasks)
def create_queue(self):
return "task_queue"
def enqueue(self, task: Dict):
task_with_serializable_params = task.copy()
# task.copy() 只复制最外层字典;如果直接改写嵌套 kwargs,会把调用方
# 持有的 VideoParams 同步替换成 dict。后续日志或重试仍可能读取原任务,
# 因此这里单独复制 kwargs,确保序列化过程没有意外副作用。
task_kwargs = task.get("kwargs", {})
task_with_serializable_params["kwargs"] = task_kwargs.copy()
if "params" in task_kwargs and isinstance(task_kwargs["params"], VideoParams):
task_with_serializable_params["kwargs"]["params"] = task_kwargs[
"params"
].model_dump(warnings=False)
# 将函数对象转换为其名称
task_with_serializable_params["func"] = task["func"].__name__
self.redis_client.rpush(self.queue, json.dumps(task_with_serializable_params))
def dequeue(self):
# 循环而非单次弹出:某个任务在入队时可能满足当时的 VideoParams 校验规则,
# 但校验规则本身在两次部署之间收紧了(例如新增 ge=1 约束)。lpop 是破坏性
# 操作,一旦弹出就不能放回原位;如果重建 VideoParams 时才发现校验失败,
# 这条任务已经从队列中永久移除了,不能再假装它还在。与其让异常从这里往上
# 抛、把这条已经丢失的任务的 lock 持有者带崩,不如原地丢弃并继续尝试队列
# 里的下一条,把"拿到一条可用任务或者队列确实空了"这个约定维持住。
while True:
task_json = self.redis_client.lpop(self.queue)
if not task_json:
return None
task_info = json.loads(task_json)
# 将函数名称转换回函数对象
task_info["func"] = FUNC_MAP[task_info["func"]]
if "params" in task_info["kwargs"] and isinstance(
task_info["kwargs"]["params"], dict
):
try:
task_info["kwargs"]["params"] = VideoParams(
**task_info["kwargs"]["params"]
)
except ValidationError as e:
logger.error(
"dropping queued task with params that fail current "
f"VideoParams validation (queued under an older, more "
f"permissive schema, or corrupted): {e}"
)
# 任务状态记录在入队前就已创建,且默认是 processing;如果只是
# 丢弃这条队列项而不动状态记录,API/WebUI 会一直显示任务在
# 运行,永远不会变成失败。用 patch_task 而不是 update_task,
# 这样如果用户已经删除了这个任务,我们不会又把它建回来。
task_id = task_info["kwargs"].get("task_id")
if task_id:
sm.state.patch_task(
task_id,
state=const.TASK_STATE_FAILED,
failed_stage="dequeue",
error=f"discarded stale queued task: {e}",
)
continue
return task_info
def is_queue_empty(self):
return self.redis_client.llen(self.queue) == 0
def queue_size(self):
return self.redis_client.llen(self.queue)