"""Admission is evaluated at phase boundaries. A closing window never cancels work.""" from datetime import datetime,timezone,timedelta from .runtime_policy import read_runtime_policy # fixed UTC+8 has no DST; avoids depending on OS IANA data on Windows. 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=="math.solve":return [("solve","qwen_http")] return [("execute","new_gpu" if task_type in {"image.generate","video.generate","defect.generate"} else "cpu_media")]