Spaces:
Running
Running
Upload 2 files
Browse files- main.py +54 -16
- peer_discovery.py +27 -1
main.py
CHANGED
|
@@ -25,20 +25,8 @@ def start_external_server():
|
|
| 25 |
subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "external_server.py")])
|
| 26 |
except Exception as e:
|
| 27 |
logging.error(f"โ ุฎุทุฃ ูู ุชุดุบูู external_server.py: {e}")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def get_free_port(start_port=7520, max_port=7600):
|
| 31 |
-
port = start_port
|
| 32 |
-
while port <= max_port:
|
| 33 |
-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 34 |
-
try:
|
| 35 |
-
s.bind(("0.0.0.0", port))
|
| 36 |
-
return port # ูุฐุง ุงูุจูุฑุช ู
ุชุงุญ
|
| 37 |
-
except OSError:
|
| 38 |
-
port += 1
|
| 39 |
-
raise RuntimeError("๐ซ ูุง ููุฌุฏ ุจูุฑุช ู
ุชุงุญ ูู ุงููุทุงู ุงูู
ุญุฏุฏ.")
|
| 40 |
-
|
| 41 |
-
# โ
|
| 42 |
FILE = Path(__file__).resolve()
|
| 43 |
BASE_DIR = FILE.parent
|
| 44 |
PROJECT_ROOT = BASE_DIR.parent
|
|
@@ -83,7 +71,7 @@ except ImportError as e:
|
|
| 83 |
sys.exit(1)
|
| 84 |
|
| 85 |
# โโโโโโโโโโโโโโโ ุซุงุจุชุงุช ุงูุชููุฆุฉ โโโโโโโโโโโโโโโ
|
| 86 |
-
CPU_PORT = 7520
|
| 87 |
SHARED_SECRET = os.getenv("SHARED_SECRET", "my_shared_secret_123")
|
| 88 |
PYTHON_EXE = sys.executable
|
| 89 |
|
|
@@ -133,7 +121,7 @@ def run_task():
|
|
| 133 |
def start_flask_server():
|
| 134 |
ip_public = os.getenv("PUBLIC_IP", "127.0.0.1")
|
| 135 |
logging.info(f"๐ Flask ู
ุชููุฑ ุนูู: http://{ip_public}:{CPU_PORT}/run_task")
|
| 136 |
-
flask_app.run(host="0.0.0.0", port=
|
| 137 |
|
| 138 |
# โโโโโโโโโโโโโโโ ุฎุฏู
ุงุช ุฎูููุฉ ู
ุญููุฉ โโโโโโโโโโโโโโโ
|
| 139 |
def start_services():
|
|
@@ -234,6 +222,56 @@ def start_ram_manager(
|
|
| 234 |
# ุชุดุบููู ูู ุฎูุท ู
ููุตู ุญุชู ูุง ูุญุฌุจ main loop
|
| 235 |
threading.Thread(target=ram_manager.main, daemon=True).start()
|
| 236 |
print(f"[MAIN] ram_manager ุดุบููุงู ุนูู ุงูุจูุฑุช {port}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
# โโโโโโโโโโโโโโโ ุงูุฏุงูุฉ ุงูุฑุฆูุณูุฉ โโโโโโโโโโโโโโโ
|
| 239 |
def main():
|
|
|
|
| 25 |
subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "external_server.py")])
|
| 26 |
except Exception as e:
|
| 27 |
logging.error(f"โ ุฎุทุฃ ูู ุชุดุบูู external_server.py: {e}")
|
| 28 |
+
|
| 29 |
+
# โโโโโโโโโโโโโโโ ุถุจุท ุงูู
ุณุงุฑุงุช โโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
FILE = Path(__file__).resolve()
|
| 31 |
BASE_DIR = FILE.parent
|
| 32 |
PROJECT_ROOT = BASE_DIR.parent
|
|
|
|
| 71 |
sys.exit(1)
|
| 72 |
|
| 73 |
# โโโโโโโโโโโโโโโ ุซุงุจุชุงุช ุงูุชููุฆุฉ โโโโโโโโโโโโโโโ
|
| 74 |
+
CPU_PORT = int(os.getenv("CPU_PORT", "7520"))
|
| 75 |
SHARED_SECRET = os.getenv("SHARED_SECRET", "my_shared_secret_123")
|
| 76 |
PYTHON_EXE = sys.executable
|
| 77 |
|
|
|
|
| 121 |
def start_flask_server():
|
| 122 |
ip_public = os.getenv("PUBLIC_IP", "127.0.0.1")
|
| 123 |
logging.info(f"๐ Flask ู
ุชููุฑ ุนูู: http://{ip_public}:{CPU_PORT}/run_task")
|
| 124 |
+
flask_app.run(host="0.0.0.0", port=CPU_PORT, debug=False)
|
| 125 |
|
| 126 |
# โโโโโโโโโโโโโโโ ุฎุฏู
ุงุช ุฎูููุฉ ู
ุญููุฉ โโโโโโโโโโโโโโโ
|
| 127 |
def start_services():
|
|
|
|
| 222 |
# ุชุดุบููู ูู ุฎูุท ู
ููุตู ุญุชู ูุง ูุญุฌุจ main loop
|
| 223 |
threading.Thread(target=ram_manager.main, daemon=True).start()
|
| 224 |
print(f"[MAIN] ram_manager ุดุบููุงู ุนูู ุงูุจูุฑุช {port}")
|
| 225 |
+
# --- ุฃุถูู ุงูุฏุงูุฉ ุงูุฌุฏูุฏุฉ ูู ุฃู ู
ูุงู ูุจู main() -----------------
|
| 226 |
+
def connect_until_success():
|
| 227 |
+
"""
|
| 228 |
+
ูุฏูุฑ ุนูู ูู CENTRAL_REGISTRY_SERVERS ููู ู
ููุฐ ูู RPORTS
|
| 229 |
+
ุญุชู ููุฌุญ ุงูุชุณุฌููุ ุซู
ููุนูุฏ ุงูุณูุฑูุฑ ูุงููุงุฆู
ุฉ ุงูุฃูููุฉ ููุฃูุฑุงู.
|
| 230 |
+
"""
|
| 231 |
+
global PORT, current_server_index
|
| 232 |
+
while True:
|
| 233 |
+
for port in RPORTS: # ุฌุฑูุจ ูู ุงูู
ูุงูุฐ
|
| 234 |
+
for idx, server in enumerate(CENTRAL_REGISTRY_SERVERS):
|
| 235 |
+
info = {
|
| 236 |
+
"node_id": os.getenv("NODE_ID", socket.gethostname()),
|
| 237 |
+
"ip": get_local_ip(),
|
| 238 |
+
"port": port
|
| 239 |
+
}
|
| 240 |
+
try:
|
| 241 |
+
resp = requests.post(f"{server}/register",
|
| 242 |
+
json=info, timeout=5)
|
| 243 |
+
resp.raise_for_status() # ูุฌุงุญ
|
| 244 |
+
PORT = port # ุซุจูุช ุงูู
ููุฐ ุงูููุงุฆู
|
| 245 |
+
current_server_index = idx
|
| 246 |
+
print(f"โ
Connected: {server} on port {PORT}")
|
| 247 |
+
return server, resp.json() # peers_list
|
| 248 |
+
except Exception:
|
| 249 |
+
pass
|
| 250 |
+
time.sleep(5) # ุฃุนุฏ ุงูู
ุญุงููุฉ ุจุนุฏ 5 ุซูุงูู
|
| 251 |
+
# ----------------------------------------------------------------
|
| 252 |
+
|
| 253 |
+
def main():
|
| 254 |
+
logging.basicConfig(level=logging.INFO)
|
| 255 |
+
print("๐ Peer Discovery System starting...")
|
| 256 |
+
|
| 257 |
+
# ุฎููุท ุงูุชุดุงู/ุชุณุฌูู LAN
|
| 258 |
+
threading.Thread(target=register_service_lan, daemon=True).start()
|
| 259 |
+
threading.Thread(target=discover_lan_loop, daemon=True).start()
|
| 260 |
+
|
| 261 |
+
# โฌ๏ธ ุจุฏูู register_with_central() ุจูุฐุง ุงูู
ูุทุน
|
| 262 |
+
server, peers = connect_until_success() # ูุง ูุฎุฑุฌ ุฅูุง ุนูุฏ ุงููุฌุงุญ
|
| 263 |
+
for p in peers: # ุฃุถู ุงูุฃูุฑุงู ุงูุฃูููููู
|
| 264 |
+
peer_url = f"http://{p['ip']}:{p['port']}/run"
|
| 265 |
+
PEERS.add(peer_url)
|
| 266 |
+
|
| 267 |
+
# ุงุณุชู
ุฑู ูู ู
ุฒุงู
ูุฉ ุงูุณููุฑูุฑ ุงูู
ุฑูุฒู
|
| 268 |
+
threading.Thread(target=fetch_central_loop, daemon=True).start()
|
| 269 |
+
|
| 270 |
+
try:
|
| 271 |
+
while True:
|
| 272 |
+
time.sleep(60)
|
| 273 |
+
except KeyboardInterrupt:
|
| 274 |
+
print("๐ Exiting...")
|
| 275 |
|
| 276 |
# โโโโโโโโโโโโโโโ ุงูุฏุงูุฉ ุงูุฑุฆูุณูุฉ โโโโโโโโโโโโโโโ
|
| 277 |
def main():
|
peer_discovery.py
CHANGED
|
@@ -7,7 +7,7 @@ import logging
|
|
| 7 |
import requests
|
| 8 |
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
|
| 9 |
import random
|
| 10 |
-
rport = {"7520", "7384" ,"9021" ,"6998" ,"5810" ,"9274" ,"8645" ,"7329" ,"7734" ,"8456" ,"6173"
|
| 11 |
# ๐ ุฅุนุฏุงุฏ ุงูู peer discovery ุนุจุฑ LAN ูInternet
|
| 12 |
SERVICE = "_tasknode._tcp.local."
|
| 13 |
PORT = int(os.getenv("CPU_PORT", random.choice(list(rport))))
|
|
@@ -135,6 +135,32 @@ def fetch_central_loop():
|
|
| 135 |
except Exception as e:
|
| 136 |
print(f"โ ๏ธ Fetch central peers failed on {server}: {e}")
|
| 137 |
time.sleep(300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# ๐ Main
|
| 140 |
def main():
|
|
|
|
| 7 |
import requests
|
| 8 |
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser
|
| 9 |
import random
|
| 10 |
+
rport = {"7520", "7384" ,"9021" ,"6998" ,"5810" ,"9274" ,"8645" ,"7329" ,"7734" ,"8456" ,"6173","7860" }
|
| 11 |
# ๐ ุฅุนุฏุงุฏ ุงูู peer discovery ุนุจุฑ LAN ูInternet
|
| 12 |
SERVICE = "_tasknode._tcp.local."
|
| 13 |
PORT = int(os.getenv("CPU_PORT", random.choice(list(rport))))
|
|
|
|
| 135 |
except Exception as e:
|
| 136 |
print(f"โ ๏ธ Fetch central peers failed on {server}: {e}")
|
| 137 |
time.sleep(300)
|
| 138 |
+
def connect_until_success():
|
| 139 |
+
"""
|
| 140 |
+
ูุญุงูู ุงูุงุชุตุงู ุจูู ุณูุฑูุฑ ูู CENTRAL_REGISTRY_SERVERS
|
| 141 |
+
ุนูู ูู ู
ููุฐ ูู RPORTS ุจุงูุชุชุงุจุน (ู
ุน ุชุฃุฎูุฑ ุจุณูุท).
|
| 142 |
+
ูุง ูุฎุฑุฌ ุฅูุง ุจุนุฏ ูุฌุงุญ ุงูุชุณุฌููุ ูููุนูุฏ ุงูุณูุฑูุฑ ุงูู
ุฎุชุงุฑ ููุงุฆู
ุฉ ุงูุฃูุฑุงู.
|
| 143 |
+
"""
|
| 144 |
+
global PORT, current_server_index
|
| 145 |
+
while True:
|
| 146 |
+
for port in RPORTS: # ุฌุฑูุจ ูู ุงูู
ูุงูุฐ
|
| 147 |
+
for idx, server in enumerate(CENTRAL_REGISTRY_SERVERS):
|
| 148 |
+
info = {
|
| 149 |
+
"node_id": os.getenv("NODE_ID", socket.gethostname()),
|
| 150 |
+
"ip": get_local_ip(),
|
| 151 |
+
"port": port
|
| 152 |
+
}
|
| 153 |
+
try:
|
| 154 |
+
resp = requests.post(f"{server}/register",
|
| 155 |
+
json=info, timeout=5)
|
| 156 |
+
resp.raise_for_status() # ูุฌุงุญ
|
| 157 |
+
PORT = port # ุซุจูุช ุงูู
ููุฐ ุงููุงุฌุญ
|
| 158 |
+
current_server_index = idx # ุญุฏูุซ ุงูู
ุคุดูุฑ
|
| 159 |
+
print(f"โ
Connected: {server} on port {PORT}")
|
| 160 |
+
return server, resp.json() # peers_list
|
| 161 |
+
except Exception as e:
|
| 162 |
+
logging.info("โ %s:%s -> %s", server, port, e)
|
| 163 |
+
time.sleep(5) # ุงูุชุธุฑ ููููุงู ุซู
ุฃูุนูุฏ ุงููุฑูุฉ
|
| 164 |
|
| 165 |
# ๐ Main
|
| 166 |
def main():
|