cmvr-visual/backend/app/services/orchestration/resource_policy.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

82 lines
2.7 KiB
Python

"""Admission is evaluated at phase boundaries. A closing window never cancels work."""
from datetime import datetime, timezone, timedelta
from app.core.runtime_policy import read_runtime_policy
SHANGHAI = timezone(timedelta(hours=8))
def execution_policy(now=None, *, settings=None):
policy = read_runtime_policy(settings)
now = now or datetime.now(timezone.utc)
if now.tzinfo is None:
now = now.replace(tzinfo=timezone.utc)
local = now.astimezone(SHANGHAI)
opened = local.hour >= 21 or local.hour < 8
next_at = local.replace(hour=21, minute=0, second=0, microsecond=0)
if local.hour >= 21:
next_at += timedelta(days=1)
enabled = policy["batch_window_enabled"]
return {
"timezone": "Asia/Shanghai",
"start": "21:00",
"end": "08:00",
"batch_window_enabled": enabled,
"configuration_error": policy["configuration_error"],
"batch_open": not policy["configuration_error"] and (not enabled or opened),
"next_batch_at": next_at.astimezone(timezone.utc).isoformat()
if enabled and (not policy["configuration_error"])
else None,
"close_behavior": "finish_current",
}
def admits(job, now=None, *, settings=None):
if job.cancel_requested or job.desired_state != "run":
return False
if set(job.hold_reasons or []) - {"window"}:
return False
if job.execution_mode != "batch":
return True
policy = read_runtime_policy(settings)
if policy["configuration_error"]:
return False
return not policy["batch_window_enabled"] or execution_policy(now)["batch_open"]
def resource_plan(task_type, input_kind=None):
if task_type == "audio.transcribe":
return (
[("extract_audio", "cpu_media"), ("transcribe", "new_gpu")]
if input_kind == "video"
else [("transcribe", "new_gpu")]
)
if task_type == "media.structure":
return (
[("sample_frames", "cpu_media"), ("describe_frames", "qwen_http")]
if input_kind == "video"
else [("describe_frames", "qwen_http")]
)
if task_type == "text.script.generate":
return [("script", "qwen_http")]
if task_type == "audio.assemble":
return [("assemble", "cpu_media")]
if task_type == "math.solve":
return [("solve", "qwen_http")]
return [
(
"execute",
"new_gpu"
if task_type
in {
"image.generate",
"image.reference.generate",
"video.generate",
"defect.generate",
"video.people.analyze",
"video.replace",
}
else "cpu_media",
)
]