Spaces:
Running
Running
File size: 10,795 Bytes
042d8bf 84c65b6 042d8bf | 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
main.py โ ูุธุงู
ุชูุฒูุน ุงูู
ูุงู
ุงูุฐูู
"""
import os
import sys
import time
import threading
import subprocess
import logging
import argparse
import socket
import random
import requests
import importlib.util
from pathlib import Path
from typing import Any
from flask import Flask, request, jsonify
from flask_cors import CORS
from peer_discovery import CENTRAL_REGISTRY_SERVERS
from peer_discovery import PORT
import external_server
# โโโโโโโโโโโโโโโ ุฅุนุฏุงุฏุงุช ุงูู
ุณุงุฑุงุช โโโโโโโโโโโโโโโ
FILE = Path(__file__).resolve()
BASE_DIR = FILE.parent
sys.path.insert(0, str(BASE_DIR))
def main():
print("๐ง CoreFlow ูุนู
ู ุงูุขู...")
# ุดุบูู ุงูุฎุงุฏู
ุฃู ุฃู ู
ูุทู ุฃุณุงุณู ููุง
# โโโโโโโโโโโโโโโ ุฅุนุฏุงุฏ ุงูุณุฌูุงุช โโโโโโโโโโโโโโโ
os.makedirs("logs", exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler("logs/main.log", mode="a", encoding="utf-8")
]
)
# โโโโโโโโโโโโโโโ ุชุญู
ูู ู
ุชุบูุฑุงุช ุงูุจูุฆุฉ โโโโโโโโโโโโโโโ
try:
from dotenv import load_dotenv
load_dotenv()
logging.info("ุชู
ุชุญู
ูู ู
ุชุบูุฑุงุช ุงูุจูุฆุฉ ู
ู .env")
except ImportError:
logging.warning("python-dotenv ุบูุฑ ู
ุซุจููุชุ ุชูุฎุทูู .env")
# โโโโโโโโโโโโโโโ ุซูุงุจุช ุงูุชููุฆุฉ โโโโโโโโโโโโโโโ
CPU_PORT = int(os.getenv("CPU_PORT", "5297"))
SHARED_SECRET = os.getenv("SHARED_SECRET", "my_shared_secret_123")
PYTHON_EXE = sys.executable
# โโโโโโโโโโโโโโโ ุฎูุงุฑุงุช ุณุทุฑ ุงูุฃูุงู
ุฑ โโโโโโโโโโโโโโโ
parser = argparse.ArgumentParser(description="ูุธุงู
ุชูุฒูุน ุงูู
ูุงู
ุงูุฐูู")
parser.add_argument(
"--stats-interval", "-s",
type=int,
default=0,
help="ุซูุงูู ุจูู ูู ุทุจุงุนุฉ ูุฅุญุตุงุฆูุฉ ุงูุฃูุฑุงู (0 = ู
ุฑุฉ ูุงุญุฏุฉ ููุท)"
)
parser.add_argument(
"--no-cli",
action="store_true",
help="ุชุนุทูู ุงููุงุฆู
ุฉ ุงูุชูุงุนููุฉ ุญุชู ุนูุฏ ูุฌูุฏ TTY"
)
args = parser.parse_args()
# โโโโโโโโโโโโโโโ ู
ุชุบูุฑุงุช ุงููุธุงู
โโโโโโโโโโโโโโโ
PEERS = set() # ู
ุฌู
ูุนุฉ ุนูุงููู ุงูุฃูุฑุงู ูุณูุงุณู ูุตูุฉ
PEERS_INFO = {} # ูุงู
ูุณ ูุญูุธ ู
ุนููู
ุงุช ุงูุฃูุฑุงู ุงููุงู
ูุฉ
current_server_index = 0
# โโโโโโโโโโโโโโโ ุฏูุงู ุงูุชุดุงู ุงูุฃูุฑุงู โโโโโโโโโโโโโโโ
def register_service_lan():
"""ุชุณุฌูู ุงูุฎุฏู
ุฉ ุนูู ุงูุดุจูุฉ ุงูู
ุญููุฉ"""
while True:
try:
logging.info("ุฌุงุฑู ุชุณุฌูู ุงูุฎุฏู
ุฉ ุนูู ุงูุดุจูุฉ ุงูู
ุญููุฉ...")
time.sleep(10)
except Exception as e:
logging.error(f"ุฎุทุฃ ูู ุชุณุฌูู ุงูุฎุฏู
ุฉ: {e}")
def discover_lan_loop():
"""ุงูุชุดุงู ุงูุฃูุฑุงู ุนูู ุงูุดุจูุฉ ุงูู
ุญููุฉ"""
while True:
try:
logging.info("ุฌุงุฑู ู
ุณุญ ุงูุดุจูุฉ ุงูู
ุญููุฉ...")
time.sleep(15)
except Exception as e:
logging.error(f"ุฎุทุฃ ูู ุงูุชุดุงู ุงูุฃูุฑุงู: {e}")
def fetch_central_loop():
"""ุฌูุจ ุชุญุฏูุซุงุช ู
ู ุงูุณูุฑูุฑ ุงูู
ุฑูุฒู"""
while True:
try:
logging.info("ุฌุงุฑู ุชุญุฏูุซ ูุงุฆู
ุฉ ุงูุฃูุฑุงู...")
time.sleep(30)
except Exception as e:
logging.error(f"ุฎุทุฃ ูู ุฌูุจ ุงูุชุญุฏูุซุงุช: {e}")
# โโโโโโโโโโโโโโโ ุฏูุงู ู
ุณุงุนุฏุฉ โโโโโโโโโโโโโโโ
def get_local_ip():
"""ุงูุญุตูู ุนูู ุนููุงู IP ุงูู
ุญูู"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
def add_peer(peer_data):
"""ุฅุถุงูุฉ ูุฑูู ุฌุฏูุฏ ุฅูู ุงููุธุงู
"""
peer_url = f"http://{peer_data['ip']}:{peer_data['port']}/run"
if peer_url not in PEERS:
PEERS.add(peer_url)
PEERS_INFO[peer_url] = peer_data
logging.info(f"ุชู
ุช ุฅุถุงูุฉ ูุฑูู ุฌุฏูุฏ: {peer_url}")
return peer_url
def benchmark(fn, *args):
"""ููุงุณ ุฒู
ู ุชูููุฐ ุงูุฏุงูุฉ"""
t0 = time.time()
res = fn(*args)
return time.time() - t0, res
def load_and_run_peer_discovery():
"""ุชุญู
ูู ูุชุดุบูู ู
ูู peer_discovery.py"""
try:
peer_discovery_path = Path(__file__).parent / "peer_discovery.py"
if not peer_discovery_path.exists():
raise FileNotFoundError("ู
ูู peer_discovery.py ุบูุฑ ู
ูุฌูุฏ")
spec = importlib.util.spec_from_file_location("peer_discovery_module", peer_discovery_path)
peer_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(peer_module)
logging.info("ุชู
ุชุญู
ูู peer_discovery.py ุจูุฌุงุญ")
return peer_module
except Exception as e:
logging.error(f"ุฎุทุฃ ูู ุชุญู
ูู peer_discovery.py: {str(e)}")
return None
# โโโโโโโโโโโโโโโ ุฏูุงู ุงูู
ูุงู
โโโโโโโโโโโโโโโ
def example_task(x: int) -> int:
"""ุฏุงูุฉ ู
ุซุงู ุจุฏููุฉ ุฅุฐุง ูู
ุชูู ู
ูุฌูุฏุฉ ูู your_tasks.py"""
return x * x
def matrix_multiply(size: int) -> list:
"""ุถุฑุจ ุงูู
ุตูููุงุช (ุจุฏูู ู
ุคูุช)"""
return [[i*j for j in range(size)] for i in range(size)]
def prime_calculation(limit: int) -> list:
"""ุญุณุงุจ ุงูุฃุนุฏุงุฏ ุงูุฃูููุฉ (ุจุฏูู ู
ุคูุช)"""
primes = []
for num in range(2, limit):
if all(num % i != 0 for i in range(2, int(num**0.5) + 1)):
primes.append(num)
return primes
def data_processing(size: int) -> dict:
"""ู
ุนุงูุฌุฉ ุงูุจูุงูุงุช (ุจุฏูู ู
ุคูุช)"""
return {i: i**2 for i in range(size)}
# โโโโโโโโโโโโโโโ ุฎุงุฏู
Flask โโโโโโโโโโโโโโโ
flask_app = Flask(__name__)
CORS(flask_app, resources={r"/*": {"origins": "*"}})
@flask_app.route("/run_task", methods=["POST"])
def run_task():
try:
data = request.get_json() if request.is_json else request.form
task_id = data.get("task_id")
if not task_id:
return jsonify(error="ูุฌุจ ุชุญุฏูุฏ task_id"), 400
if task_id == "1":
result = matrix_multiply(500)
elif task_id == "2":
result = prime_calculation(100_000)
elif task_id == "3":
result = data_processing(10_000)
else:
return jsonify(error="ู
ุนุฑู ุงูู
ูู
ุฉ ุบูุฑ ุตุญูุญ"), 400
return jsonify(result=result)
except Exception as e:
logging.error(f"ุฎุทุฃ ูู ู
ุนุงูุฌุฉ ุงูู
ูู
ุฉ: {str(e)}", exc_info=True)
return jsonify(error="ุญุฏุซ ุฎุทุฃ ุฏุงุฎูู ูู ุงูุฎุงุฏู
"), 500
def 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 connect_until_success():
global CPU_PORT, current_server_index
peer_module = load_and_run_peer_discovery()
if peer_module is None:
logging.warning("ุณูุณุชู
ุฑ ุงูุชุดุบูู ุจุฏูู peer_discovery.py")
return None, []
CENTRAL_REGISTRY_SERVERS = getattr(peer_module, 'CENTRAL_REGISTRY_SERVERS', [])
if not CENTRAL_REGISTRY_SERVERS:
logging.error("ูุงุฆู
ุฉ ุงูุณูุฑูุฑุงุช ุงูู
ุฑูุฒูุฉ ูุงุฑุบุฉ")
return None, []
while True:
for port in [CPU_PORT, 5298, 5299]:
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()
CPU_PORT = port
current_server_index = idx
logging.info(f"ุชู
ุงูุงุชุตุงู ุจุงูุณูุฑูุฑ: {server} ุนูู ุงูู
ููุฐ {CPU_PORT}")
# ู
ุนุงูุฌุฉ ูุงุฆู
ุฉ ุงูุฃูุฑุงู ุงูู
ุณุชูู
ุฉ
peers_list = resp.json()
peer_urls = []
for p in peers_list:
peer_url = add_peer(p)
peer_urls.append(peer_url)
return server, peer_urls
except Exception as e:
logging.warning(f"ูุดู ุงูุงุชุตุงู ุจู {server}: {str(e)}")
time.sleep(5)
def main():
"""ุงูุฏุงูุฉ ุงูุฑุฆูุณูุฉ ูุชุดุบูู ุงููุธุงู
"""
# ุชุดุบูู ุงูุฎุฏู
ุงุช ุงูุฃุณุงุณูุฉ
try:
subprocess.Popen([PYTHON_EXE, "peer_server.py", "--port", str(CPU_PORT)])
subprocess.Popen([PYTHON_EXE, "load_balancer.py"])
logging.info("ุชู
ุชุดุบูู ุงูุฎุฏู
ุงุช ุงูุฎููููุฉ")
except Exception as exc:
logging.error(f"ุฎุทุฃ ุจุชุดุบูู ุงูุฎุฏู
ุงุช ุงูุฎูููุฉ: {exc}")
# ุงูุงุชุตุงู ุจุงูุณูุฑูุฑ ุงูู
ุฑูุฒู
server, initial_peers = connect_until_success()
# ุชุดุบูู ุฎุงุฏู
Flask
threading.Thread(target=start_flask_server, daemon=True).start()
# ุงูุจูุงุก ูู ุญููุฉ ุฑุฆูุณูุฉ
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
logging.info("ุชู
ุฅููุงุก ุงูุจุฑูุงู
ุฌ.")
if __name__ == "__main__":
# ุฅุถุงูุฉ ุงููุฑูู ุงูู
ุญูู
add_peer({"ip": "127.0.0.1", "port": CPU_PORT})
# ุชุดุบูู ุฎุฏู
ุงุช ุงูุชุดุงู ุงูุฃูุฑุงู
threading.Thread(target=register_service_lan, daemon=True).start()
threading.Thread(target=discover_lan_loop, daemon=True).start()
threading.Thread(target=fetch_central_loop, daemon=True).start()
# ุจุฏุก ุงููุธุงู
ุงูุฑุฆูุณู
main()
|