fix: refresh endpoint triggers Kimi collector via SSH to Windows + scp result back
This commit is contained in:
@@ -1239,7 +1239,7 @@ def api_usage():
|
||||
|
||||
@app.route("/api/usage/refresh", methods=["POST"])
|
||||
def api_usage_refresh():
|
||||
"""触发本地采集(异步 subprocess,立即返回)"""
|
||||
"""触发 OCG + Kimi 采集(异步 subprocess,立即返回)"""
|
||||
global _usage_collector_running
|
||||
if _usage_collector_running:
|
||||
return jsonify({"ok": True, "message": "collection already in-flight"})
|
||||
@@ -1248,14 +1248,38 @@ def api_usage_refresh():
|
||||
def _runner():
|
||||
global _usage_collector_running
|
||||
try:
|
||||
log.info("usage: starting local collector subprocess")
|
||||
# 1. OCG 本地采集
|
||||
log.info("usage: starting OCG collector")
|
||||
proc = subprocess.run(
|
||||
[sys.executable, str(_USAGE_COLLECTOR_SCRIPT)],
|
||||
capture_output=True, text=True, timeout=120)
|
||||
log.info(f"usage: collector done (rc={proc.returncode}, "
|
||||
f"stdout={len(proc.stdout)}, stderr={len(proc.stderr)})")
|
||||
log.info(f"usage: OCG done (rc={proc.returncode})")
|
||||
if proc.returncode != 0 and proc.stderr:
|
||||
log.warning(f"usage_collector stderr:\n{proc.stderr[-1000:]}")
|
||||
|
||||
# 2. Kimi 远程采集(SSH 到 Windows)
|
||||
try:
|
||||
log.info("usage: starting Kimi collector via SSH")
|
||||
kimi_cmd = [
|
||||
"ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10",
|
||||
"-o", "BatchMode=yes", "hmo@192.168.1.16",
|
||||
r'"C:\Users\hmo\AppData\Local\Programs\Python\Python310\python.exe" "D:\F\NewI\opencode\daily-workspace\projects\AgentsMeeting\gateway\scripts\usage_collector_kimi.py"'
|
||||
]
|
||||
proc2 = subprocess.run(kimi_cmd, capture_output=True, text=True, timeout=60)
|
||||
log.info(f"usage: Kimi done (rc={proc2.returncode})")
|
||||
if proc2.returncode != 0 and proc2.stderr:
|
||||
log.warning(f"usage_kimi stderr: {proc2.stderr[-300:]}")
|
||||
|
||||
# 3. scp Kimi 结果回 246
|
||||
scp_cmd = [
|
||||
"scp", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10",
|
||||
"hmo@192.168.1.16:D:/F/NewI/opencode/daily-workspace/projects/AgentsMeeting/gateway/temp/usage_stats_kimi.json",
|
||||
str(TEMP_DIR / "usage_stats_kimi.json")
|
||||
]
|
||||
proc3 = subprocess.run(scp_cmd, capture_output=True, timeout=15)
|
||||
log.info(f"usage: Kimi scp done (rc={proc3.returncode})")
|
||||
except Exception as e:
|
||||
log.warning(f"usage: Kimi collection failed: {e}")
|
||||
except Exception as e:
|
||||
log.error(f"usage: collector crash: {e}")
|
||||
finally:
|
||||
@@ -1263,7 +1287,7 @@ def api_usage_refresh():
|
||||
|
||||
t = threading.Thread(target=_runner, name="usage_collector", daemon=True)
|
||||
t.start()
|
||||
return jsonify({"ok": True, "message": "collection triggered (~10s), poll /api/usage shortly"})
|
||||
return jsonify({"ok": True, "message": "collection triggered (~15s), poll /api/usage shortly"})
|
||||
|
||||
|
||||
# ── Key Registry ──────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user