Spaces:
Running
Running
Upload 2 files
Browse filesFiles changed (2)
- main.py+54-16
- peer_discovery.py+27-1
24 unmodified lines252627282930313233343536373839404142434438 unmodified lines8384858687888943 unmodified lines13313413513613713813994 unmodified lines23423523623723823924 unmodified linessubprocess.Popen([sys.executable, os.path.join(os.getcwd(), "external_server.py")])except Exception as e:logging.error(f"โ ุฎุทุฃ ูู ุชุดุบูู external_server.py: {e}")import socketdef get_free_port(start_port=7520, max_port=7600):port = start_portwhile port <= max_port:with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:try:s.bind(("0.0.0.0", port))return port # ูุฐุง ุงูุจูุฑุช ู ุชุงุญexcept OSError:port += 1raise RuntimeError("๐ซ ูุง ููุฌุฏ ุจูุฑุช ู ุชุงุญ ูู ุงููุทุงู ุงูู ุญุฏุฏ.")# โFILE = Path(__file__).resolve()BASE_DIR = FILE.parentPROJECT_ROOT = BASE_DIR.parent38 unmodified linessys.exit(1)# โโโโโโโโโโโโโโโ ุซุงุจุชุงุช ุงูุชููุฆุฉ โโโโโโโโโโโโโโโCPU_PORT = 7520SHARED_SECRET = os.getenv("SHARED_SECRET", "my_shared_secret_123")PYTHON_EXE = sys.executable43 unmodified linesdef start_flask_server():ip_public = os.getenv("PUBLIC_IP", "127.0.0.1")logging.info(f"๐ Flask ู ุชููุฑ ุนูู: http://{ip_public}:{CPU_PORT}/run_task")flask_app.run(host="0.0.0.0", port=7520, debug=False)# โโโโโโโโโโโโโโโ ุฎุฏู ุงุช ุฎูููุฉ ู ุญููุฉ โโโโโโโโโโโโโโโdef start_services():94 unmodified lines# ุชุดุบููู ูู ุฎูุท ู ููุตู ุญุชู ูุง ูุญุฌุจ main loopthreading.Thread(target=ram_manager.main, daemon=True).start()print(f"[MAIN] ram_manager ุดุบููุงู ุนูู ุงูุจูุฑุช {port}")# โโโโโโโโโโโโโโโ ุงูุฏุงูุฉ ุงูุฑุฆูุณูุฉ โโโโโโโโโโโโโโโdef main():24 unmodified lines252627282930313238 unmodified lines7172737475767743 unmodified lines12112212312412512612794 unmodified lines22222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627724 unmodified linessubprocess.Popen([sys.executable, os.path.join(os.getcwd(), "external_server.py")])except Exception as e:logging.error(f"โ ุฎุทุฃ ูู ุชุดุบูู external_server.py: {e}")# โโโโโโโโโโโโโโโ ุถุจุท ุงูู ุณุงุฑุงุช โโโโโโโโโโโโโโโFILE = Path(__file__).resolve()BASE_DIR = FILE.parentPROJECT_ROOT = BASE_DIR.parent38 unmodified linessys.exit(1)# โโโโโโโโโโโโโโโ ุซุงุจุชุงุช ุงูุชููุฆุฉ โโโโโโโโโโโโโโโCPU_PORT = int(os.getenv("CPU_PORT", "7520"))SHARED_SECRET = os.getenv("SHARED_SECRET", "my_shared_secret_123")PYTHON_EXE = sys.executable43 unmodified linesdef start_flask_server():ip_public = os.getenv("PUBLIC_IP", "127.0.0.1")logging.info(f"๐ Flask ู ุชููุฑ ุนูู: http://{ip_public}:{CPU_PORT}/run_task")flask_app.run(host="0.0.0.0", port=CPU_PORT, debug=False)# โโโโโโโโโโโโโโโ ุฎุฏู ุงุช ุฎูููุฉ ู ุญููุฉ โโโโโโโโโโโโโโโdef start_services():94 unmodified lines# ุชุดุบููู ูู ุฎูุท ู ููุตู ุญุชู ูุง ูุญุฌุจ main loopthreading.Thread(target=ram_manager.main, daemon=True).start()print(f"[MAIN] ram_manager ุดุบููุงู ุนูู ุงูุจูุฑุช {port}")# --- ุฃุถูู ุงูุฏุงูุฉ ุงูุฌุฏูุฏุฉ ูู ุฃู ู ูุงู ูุจู main() -----------------def connect_until_success():"""ูุฏูุฑ ุนูู ูู CENTRAL_REGISTRY_SERVERS ููู ู ููุฐ ูู RPORTSุญุชู ููุฌุญ ุงูุชุณุฌููุ ุซู ููุนูุฏ ุงูุณูุฑูุฑ ูุงููุงุฆู ุฉ ุงูุฃูููุฉ ููุฃูุฑุงู."""global PORT, current_server_indexwhile True:for port in RPORTS: # ุฌุฑูุจ ูู ุงูู ูุงูุฐfor idx, server in enumerate(CENTRAL_REGISTRY_SERVERS):info = {"node_id": os.getenv("NODE_ID", socket.gethostname()),"ip": get_local_ip(),"port": port}try:resp = requests.post(f"{server}/register",json=info, timeout=5)resp.raise_for_status() # ูุฌุงุญPORT = port # ุซุจูุช ุงูู ููุฐ ุงูููุงุฆูcurrent_server_index = idxprint(f"โ Connected: {server} on port {PORT}")return server, resp.json() # peers_listexcept Exception:passtime.sleep(5) # ุฃุนุฏ ุงูู ุญุงููุฉ ุจุนุฏ 5 ุซูุงูู# ----------------------------------------------------------------def main():logging.basicConfig(level=logging.INFO)print("๐ Peer Discovery System starting...")# ุฎููุท ุงูุชุดุงู/ุชุณุฌูู LANthreading.Thread(target=register_service_lan, daemon=True).start()threading.Thread(target=discover_lan_loop, daemon=True).start()# โฌ๏ธ ุจุฏูู register_with_central() ุจูุฐุง ุงูู ูุทุนserver, peers = connect_until_success() # ูุง ูุฎุฑุฌ ุฅูุง ุนูุฏ ุงููุฌุงุญfor p in peers: # ุฃุถู ุงูุฃูุฑุงู ุงูุฃููููููpeer_url = f"http://{p['ip']}:{p['port']}/run"PEERS.add(peer_url)# ุงุณุชู ุฑู ูู ู ุฒุงู ูุฉ ุงูุณููุฑูุฑ ุงูู ุฑูุฒูthreading.Thread(target=fetch_central_loop, daemon=True).start()try:while True:time.sleep(60)except KeyboardInterrupt:print("๐ Exiting...")# โโโโโโโโโโโโโโโ ุงูุฏุงูุฉ ุงูุฑุฆูุณูุฉ โโโโโโโโโโโโโโโdef main():
6 unmodified lines78910111213121 unmodified lines1351361371381391406 unmodified linesimport requestsfrom zeroconf import Zeroconf, ServiceInfo, ServiceBrowserimport randomrport = {"7520", "7384" ,"9021" ,"6998" ,"5810" ,"9274" ,"8645" ,"7329" ,"7734" ,"8456" ,"6173" ,"7000" , "8000" }# ๐ ุฅุนุฏุงุฏ ุงูู peer discovery ุนุจุฑ LAN ูInternetSERVICE = "_tasknode._tcp.local."PORT = int(os.getenv("CPU_PORT", random.choice(list(rport))))121 unmodified linesexcept Exception as e:print(f"โ ๏ธ Fetch central peers failed on {server}: {e}")time.sleep(300)# ๐ Maindef main():6 unmodified lines78910111213121 unmodified lines1351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651666 unmodified linesimport requestsfrom zeroconf import Zeroconf, ServiceInfo, ServiceBrowserimport randomrport = {"7520", "7384" ,"9021" ,"6998" ,"5810" ,"9274" ,"8645" ,"7329" ,"7734" ,"8456" ,"6173","7860" }# ๐ ุฅุนุฏุงุฏ ุงูู peer discovery ุนุจุฑ LAN ูInternetSERVICE = "_tasknode._tcp.local."PORT = int(os.getenv("CPU_PORT", random.choice(list(rport))))121 unmodified linesexcept Exception as e:print(f"โ ๏ธ Fetch central peers failed on {server}: {e}")time.sleep(300)def connect_until_success():"""ูุญุงูู ุงูุงุชุตุงู ุจูู ุณูุฑูุฑ ูู CENTRAL_REGISTRY_SERVERSุนูู ูู ู ููุฐ ูู RPORTS ุจุงูุชุชุงุจุน (ู ุน ุชุฃุฎูุฑ ุจุณูุท).ูุง ูุฎุฑุฌ ุฅูุง ุจุนุฏ ูุฌุงุญ ุงูุชุณุฌููุ ูููุนูุฏ ุงูุณูุฑูุฑ ุงูู ุฎุชุงุฑ ููุงุฆู ุฉ ุงูุฃูุฑุงู."""global PORT, current_server_indexwhile True:for port in RPORTS: # ุฌุฑูุจ ูู ุงูู ูุงูุฐfor idx, server in enumerate(CENTRAL_REGISTRY_SERVERS):info = {"node_id": os.getenv("NODE_ID", socket.gethostname()),"ip": get_local_ip(),"port": port}try:resp = requests.post(f"{server}/register",json=info, timeout=5)resp.raise_for_status() # ูุฌุงุญPORT = port # ุซุจูุช ุงูู ููุฐ ุงููุงุฌุญcurrent_server_index = idx # ุญุฏูุซ ุงูู ุคุดูุฑprint(f"โ Connected: {server} on port {PORT}")return server, resp.json() # peers_listexcept Exception as e:logging.info("โ %s:%s -> %s", server, port, e)time.sleep(5) # ุงูุชุธุฑ ููููุงู ุซู ุฃูุนูุฏ ุงููุฑูุฉ# ๐ Maindef main():
main.py+54-16
peer_discovery.py+27-1