"""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 policy = read_runtime_policy(settings) if policy["configuration_error"]: return False return ( not policy["batch_window_enabled"] or execution_policy(now, settings=settings)["batch_open"] ) def resource_plan(task_type, input_kind=None): from app.domain.tasks import CPU_TYPES, GPU_TYPES, DIALOGUE_GPU_TYPES if task_type not in GPU_TYPES | CPU_TYPES: raise ValueError("Unknown task resource policy") if task_type in DIALOGUE_GPU_TYPES: return [("execute", "new_gpu")] if task_type == "audio.dialogue.extract": return [("extract", "cpu_media")] if task_type == "video.dialogue.select": return [("select", "cpu_media")] if task_type == "text.dialogue.plan": return [("script", "qwen_http")] 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 in {"video.continuity.inspect", "video.dialogue.review"}: return [("sample_frames", "cpu_media"), ("assess", "qwen_http")] if task_type in {"audio.normalize", "audio.mix"}: return [("audio_normalize" if task_type == "audio.normalize" else "audio_mix", "cpu_media")] if task_type in {"text.script.generate", "text.story.plan"}: 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 GPU_TYPES else "cpu_media")]