Pretor/pretor/api/cluster.py

45 lines
1.6 KiB
Python

# Copyright 2026 zhaoxi826
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
import ray
import asyncio
cluster_router = APIRouter(prefix="/api/v1/cluster", tags=["cluster"])
@cluster_router.websocket("/ws/state")
async def update_cluster_state(websocket: WebSocket):
await websocket.accept()
try:
while True:
nodes = ray.nodes()
payload = [
{
"node_id": node.get("NodeID"),
"node_name": node.get("NodeName"),
"alive": node.get("Alive"),
"resources": node.get("Resources", {}),
"remaining": node.get("RemainingResources", {})
}
for node in nodes
]
await websocket.send_json(payload)
await asyncio.sleep(10)
except WebSocketDisconnect:
pass
except RuntimeError as e:
if "closed" not in str(e) and "GeneratorExit" not in str(e):
raise
except Exception:
pass