Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import ray
|
| 4 |
|
| 5 |
-
app =
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
# ====================================================
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
@app.
|
| 14 |
-
def connect_worker():
|
| 15 |
global connected
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return jsonify({"message": "Already connected to Ray head node."}), 200
|
| 19 |
-
|
| 20 |
-
try:
|
| 21 |
-
ray.init(address=HEAD_NODE_ADDRESS) # Use the fixed head node address
|
| 22 |
connected = True
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
@app.
|
| 28 |
-
def disconnect_worker():
|
| 29 |
global connected
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return jsonify({"message": "Worker is already disconnected."}), 200
|
| 33 |
-
|
| 34 |
-
try:
|
| 35 |
-
ray.shutdown()
|
| 36 |
connected = False
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
return
|
| 40 |
-
|
| 41 |
-
@app.route('/')
|
| 42 |
-
def home():
|
| 43 |
-
return "Worker Flask App Running."
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
| 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" # <-- Yahan apna address daal dena
|
|
|
|
| 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 |
+
return {"message": "Worker connected to Ray Head Node"}
|
| 19 |
+
else:
|
| 20 |
+
return {"message": "Worker already connected"}
|
| 21 |
|
| 22 |
+
@app.post("/noworker")
|
| 23 |
+
async def disconnect_worker():
|
| 24 |
global connected
|
| 25 |
+
if connected:
|
| 26 |
+
os.system("ray stop")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
connected = False
|
| 28 |
+
return {"message": "Worker disconnected from Ray Head Node"}
|
| 29 |
+
else:
|
| 30 |
+
return {"message": "No worker was connected"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
@app.get("/")
|
| 33 |
+
async def root():
|
| 34 |
+
return {"message": "Worker Node Ready"}
|