Khelendramee commited on
Commit
29c2a81
·
verified ·
1 Parent(s): 70b1b4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -36
app.py CHANGED
@@ -1,46 +1,34 @@
1
- # worker_app.py
2
- from flask import Flask, request, jsonify
3
- import ray
4
 
5
- app = Flask(__name__)
6
 
7
- # ====== Yeh apna FIXED head node address hai ======
8
- HEAD_NODE_ADDRESS = "ray://192.168.1.100:10001"
9
- # ====================================================
10
 
11
- connected = False # Worker connection status
 
12
 
13
- @app.route('/worker', methods=['POST'])
14
- def connect_worker():
15
  global connected
16
-
17
- if connected:
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 jsonify({"message": f"Worker connected successfully to head node at {HEAD_NODE_ADDRESS}."}), 200
24
- except Exception as e:
25
- return jsonify({"error": str(e)}), 500
26
 
27
- @app.route('/noworker', methods=['POST'])
28
- def disconnect_worker():
29
  global connected
30
-
31
- if not connected:
32
- return jsonify({"message": "Worker is already disconnected."}), 200
33
-
34
- try:
35
- ray.shutdown()
36
  connected = False
37
- return jsonify({"message": "Worker disconnected successfully."}), 200
38
- except Exception as e:
39
- return jsonify({"error": str(e)}), 500
40
-
41
- @app.route('/')
42
- def home():
43
- return "Worker Flask App Running."
44
 
45
- if __name__ == '__main__':
46
- app.run(host='0.0.0.0', port=5000)
 
 
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"}