Spaces:
Runtime error
Runtime error
File size: 3,664 Bytes
eebf941 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#!/usr/bin/env python3
"""
external_server.py โ ุณูุฑูุฑ ู
ุฑูุฒู ูุชูุฒูุน ุงูู
ูุงู
+ Dashboard ุชูุงุนูู
"""
import logging
import requests
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from peer_discovery import PEERS
from peer_discovery import PORT
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
socketio = SocketIO(app, cors_allowed_origins="*")
connected_peers = {} # {node_id: {"cpu":%, "ram":%, "gpu":%}}
# โโโโโโโโโโโโโโโ ุงุฎุชูุงุฑ ุฃูุถู Peer โโโโโโโโโโโโโโโ
def select_best_peer():
peers_list = list(PEERS)
if not peers_list:
logging.warning("โ ๏ธ ูุง ุชูุฌุฏ ุฃุฌูุฒุฉ ู
ุณุฌูุฉ ุญุงููุงู.")
return None
try:
peer_loads = []
for peer_url in peers_list:
try:
resp = requests.get(f"{peer_url.replace('/run_task','')}/status", timeout=2)
if resp.ok:
data = resp.json()
peer_loads.append((peer_url, data.get("cpu_load", 100)))
except:
continue
if not peer_loads:
return None
peer_loads.sort(key=lambda x: x[1])
return peer_loads[0][0]
except Exception as e:
logging.error(f"โ ุฎุทุฃ ูู ุงุฎุชูุงุฑ ุงูู Peer: {e}")
return None
# โโโโโโโโโโโโโโโ API ุชูุฒูุน ุงูู
ูุงู
โโโโโโโโโโโโโโโ
@app.route("/submit_task", methods=["POST"])
def submit_task():
data = request.get_json()
if not data or "task_id" not in data:
return jsonify({"error": "ูุฌุจ ุชุญุฏูุฏ task_id"}), 400
peer = select_best_peer()
if not peer:
return jsonify({"error": "ูุง ุชูุฌุฏ ุฃุฌูุฒุฉ ู
ุชุงุญุฉ ุญุงููุงู"}), 503
try:
resp = requests.post(peer, json=data, timeout=10)
if resp.ok:
return jsonify({"status": "success", "result": resp.json()})
else:
return jsonify({"error": "ูุดู ุฅุฑุณุงู ุงูู
ูู
ุฉ"}), 500
except Exception as e:
logging.error(f"โ ุฎุทุฃ ูู ุฅุฑุณุงู ุงูู
ูู
ุฉ: {e}")
return jsonify({"error": str(e)}), 500
# โโโโโโโโโโโโโโโ API ุชุญุฏูุซ ุญุงูุฉ ุงูุฃุฌูุฒุฉ โโโโโโโโโโโโโโโ
@app.route("/update_status", methods=["POST"])
def update_status():
data = request.json
node_id = data.get("node_id")
if not node_id:
return jsonify({"error": "node_id ู
ุทููุจ"}), 400
connected_peers[node_id] = {
"cpu": data.get("cpu"),
"ram": data.get("ram"),
"gpu": data.get("gpu")
}
socketio.emit("update_peers", connected_peers, broadcast=True)
return jsonify({"status": "ok"})
# โโโโโโโโโโโโโโโ ุตูุญุฉ Dashboard โโโโโโโโโโโโโโโ
@app.route("/")
def index():
return render_template("dashboard.html")
# โโโโโโโโโโโโโโโ ุฏุฑุฏุดุฉ โโโโโโโโโโโโโโโ
@socketio.on("send_message")
def handle_message(data):
socketio.emit("receive_message", data, broadcast=True)
# โโโโโโโโโโโโโโโ ุชุดุบูู ุงูุณูุฑูุฑ โโโโโโโโโโโโโโโ
if __name__ == "__main__":
logging.info("๐ ุจุฏุก ุงูุณูุฑูุฑ ุงูู
ุฑูุฒู ู
ุน Dashboard ูุฏุฑุฏุดุฉ")
socketio.run(app, host="0.0.0.0", port =5005)
|