Khelendramee commited on
Commit
c92cbb8
·
verified ·
1 Parent(s): 0a87585

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)