feat: add durable success-first modelhub agent

This commit is contained in:
CoolBoy
2026-08-15 19:24:26 +08:00
parent 91d1d3d87d
commit 4260793c03
22 changed files with 2420 additions and 86 deletions

23
main.py
View File

@@ -17,6 +17,7 @@ HOST = "0.0.0.0"
PORT = int(os.getenv("PORT", "8080"))
ROOT = Path(__file__).resolve().parent
WORKER_SCRIPT = ROOT / "modelhub_submmit_api" / "poll_runner.py"
READINESS_PATH = ROOT / ".modelhub_state" / "readiness.json"
shutdown_requested = False
worker: subprocess.Popen | None = None
@@ -53,6 +54,7 @@ def _worker_command() -> list[str]:
"--post-cycle-cooldown-seconds",
os.getenv("MODELHUB_AGENT_POST_CYCLE_COOLDOWN_SECONDS", "2"),
"--skip-history-archive",
"--state-sync",
]
daily_target = os.getenv("MODELHUB_AGENT_DAILY_TARGET", "").strip()
@@ -86,6 +88,14 @@ def _config() -> dict[str, object]:
}
def _readiness() -> dict[str, object]:
try:
payload = json.loads(READINESS_PATH.read_text(encoding="utf-8"))
except (FileNotFoundError, OSError, ValueError, TypeError):
return {"ready": False, "reason": "readiness_not_reported"}
return payload if isinstance(payload, dict) else {"ready": False, "reason": "readiness_invalid"}
class Handler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
if self.path == "/health":
@@ -98,6 +108,19 @@ class Handler(BaseHTTPRequestHandler):
self._send_json({"status": "ok", "config": _config()})
return
if self.path == "/ready":
readiness = _readiness()
status = 200 if readiness.get("ready") is True else 503
self._send_json(
{
"status": "ready" if status == 200 else "not_ready",
"readiness": readiness,
"config": _config(),
},
status=status,
)
return
if self.path == "/":
self._send_json({"name": "modelhub-submmit-agent", "status": "running", "config": _config()})
return