Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,39 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
# Head node address
|
| 7 |
-
RAY_HEAD_ADDRESS = "ray://<HEAD-NODE-IP>:10001" # <--
|
| 8 |
|
| 9 |
# Worker connect status
|
| 10 |
connected = False
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@app.post("/worker")
|
| 13 |
async def connect_worker():
|
| 14 |
global connected
|
| 15 |
if not connected:
|
| 16 |
os.system(f"ray start --address='{RAY_HEAD_ADDRESS}'")
|
| 17 |
connected = True
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@app.post("/noworker")
|
| 23 |
async def disconnect_worker():
|
|
@@ -25,10 +41,16 @@ async def disconnect_worker():
|
|
| 25 |
if connected:
|
| 26 |
os.system("ray stop")
|
| 27 |
connected = False
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@app.get("/")
|
| 33 |
async def root():
|
| 34 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import os
|
| 3 |
+
import subprocess
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
# Head node address
|
| 8 |
+
RAY_HEAD_ADDRESS = "ray://<HEAD-NODE-IP>:10001" # <-- Apna correct head node address yahan daalo
|
| 9 |
|
| 10 |
# Worker connect status
|
| 11 |
connected = False
|
| 12 |
|
| 13 |
+
def get_ray_status():
|
| 14 |
+
"""Check if ray is running or stopped."""
|
| 15 |
+
try:
|
| 16 |
+
output = subprocess.check_output(["ray", "status"], stderr=subprocess.STDOUT)
|
| 17 |
+
if b"cluster" in output or b"Connected" in output:
|
| 18 |
+
return "running"
|
| 19 |
+
else:
|
| 20 |
+
return "unknown"
|
| 21 |
+
except subprocess.CalledProcessError:
|
| 22 |
+
return "stopped"
|
| 23 |
+
except FileNotFoundError:
|
| 24 |
+
return "ray-not-installed"
|
| 25 |
+
|
| 26 |
@app.post("/worker")
|
| 27 |
async def connect_worker():
|
| 28 |
global connected
|
| 29 |
if not connected:
|
| 30 |
os.system(f"ray start --address='{RAY_HEAD_ADDRESS}'")
|
| 31 |
connected = True
|
| 32 |
+
return {
|
| 33 |
+
"message": "Worker connection attempt finished",
|
| 34 |
+
"connection": 1 if connected else 0,
|
| 35 |
+
"ray_status": get_ray_status()
|
| 36 |
+
}
|
| 37 |
|
| 38 |
@app.post("/noworker")
|
| 39 |
async def disconnect_worker():
|
|
|
|
| 41 |
if connected:
|
| 42 |
os.system("ray stop")
|
| 43 |
connected = False
|
| 44 |
+
return {
|
| 45 |
+
"message": "Worker disconnection attempt finished",
|
| 46 |
+
"connection": 1 if connected else 0,
|
| 47 |
+
"ray_status": get_ray_status()
|
| 48 |
+
}
|
| 49 |
|
| 50 |
@app.get("/")
|
| 51 |
async def root():
|
| 52 |
+
return {
|
| 53 |
+
"message": "Worker Node Ready",
|
| 54 |
+
"connection": 1 if connected else 0,
|
| 55 |
+
"ray_status": get_ray_status()
|
| 56 |
+
}
|