Files
modelhub-submit-agent/main.py
2026-07-21 18:45:22 +08:00

75 lines
2.0 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.

"""
ModelHub 提交智能体(代理模式)
用途:获取 strategyId本地脚本直接调用 ModelHub API
"""
import json
import os
import signal
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
HOST = "0.0.0.0"
PORT = 8080
shutdown_requested = False
def _config() -> dict:
strategy_id = os.getenv("STRATEGY_ID", "")
return {
"strategy_id": strategy_id,
"status": "running",
}
class Handler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
if self.path == "/health":
self._send_json({"status": "ok"})
return
if self.path == "/":
self._send_json({
"name": "modelhub-submit-agent",
"config": _config(),
})
return
self._send_json({"error": "not found"}, status=404)
def log_message(self, fmt: str, *args: object) -> None:
print(f"{self.address_string()} - {fmt % args}", flush=True)
def _send_json(self, body: dict, status: int = 200) -> None:
payload = json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def _handle_signal(signum: int, _frame: object) -> None:
global shutdown_requested
shutdown_requested = True
print(f"received signal {signum}, shutting down", flush=True)
def main() -> None:
signal.signal(signal.SIGTERM, _handle_signal)
signal.signal(signal.SIGINT, _handle_signal)
server = ThreadingHTTPServer((HOST, PORT), Handler)
server.timeout = 1
strategy_id = os.getenv("STRATEGY_ID", "NOT_SET")
print(f"modelhub-submit-agent listening on {HOST}:{PORT}", flush=True)
print(f"STRATEGY_ID: {strategy_id}", flush=True)
while not shutdown_requested:
server.handle_request()
server.server_close()
if __name__ == "__main__":
main()