refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 21:19:50 +02:00
parent d18f8fcb8e
commit 3a90a48109

39
app.py
View File

@@ -583,45 +583,6 @@ def ws_task(ws):
except Exception: except Exception:
pass pass
# ---------------- WebSocket: live tail zadań ----------------
@sock.route("/ws/task")
def ws_task(ws):
# (bez zmian jak w mojej poprzedniej wersji)
q = ws.environ.get("QUERY_STRING", "")
params = {}
for part in q.split("&"):
if not part: continue
k, _, v = part.partition("=")
params[k] = v
upid = params.get("upid", "").strip()
node = params.get("node", "").strip()
if not upid or not node:
ws.send(json.dumps({"type":"error","error":"upid and node are required"}))
return
start = 0
try:
while True:
st = get_json(["pvesh", "get", f"/nodes/{node}/tasks/{upid}/status"]) or {}
ws.send(json.dumps({"type":"status","status":st}))
lines = get_json(["pvesh", "get", f"/nodes/{node}/tasks/{upid}/log", "-start", str(start)]) or []
if isinstance(lines, list) and lines:
for ln in lines:
txt = (ln.get("t") if isinstance(ln, dict) else None)
if txt:
ws.send(json.dumps({"type":"log","line":txt}))
try:
start = max((int(x.get("n", start)) for x in lines if isinstance(x, dict)), default=start) + 1
except Exception:
pass
if isinstance(st, dict) and (str(st.get("status","")).lower() == "stopped" or st.get("exitstatus")):
ok = (str(st.get("exitstatus","")).upper() == "OK")
ws.send(json.dumps({"type":"done","ok":ok,"exitstatus":st.get("exitstatus")}))
break
time.sleep(1.2)
except Exception:
try: ws.close()
except Exception: pass
# ---------------- WebSocket: broadcast observe per sid ---------------- # ---------------- WebSocket: broadcast observe per sid ----------------
@sock.route("/ws/observe") @sock.route("/ws/observe")
def ws_observe(ws): def ws_observe(ws):