osamabyc86 commited on
Commit
c69a031
·
verified ·
1 Parent(s): d1abaf9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +82 -1
main.py CHANGED
@@ -17,7 +17,25 @@ from typing import Any
17
 
18
  from flask import Flask, request, jsonify
19
  from flask_cors import CORS
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # أو مباشرة:
23
  # from peer_discovery import PORT as CPU_PORT
@@ -180,7 +198,70 @@ def auto_monitor(auto_executor):
180
  except Exception as e:
181
  logging.error(f"خطأ في المراقبة التلقائية: {e}")
182
  time.sleep(5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  # ─────────────── القائمة التفاعلية CLI ───────────────
185
  def menu(executor: DistributedExecutor):
186
  tasks = {
@@ -366,4 +447,4 @@ if __name__ == "__main__":
366
  except ImportError:
367
  logging.info("🛈 your_control غير متوفّر – تشغيل افتراضي")
368
 
369
- main()
 
17
 
18
  from flask import Flask, request, jsonify
19
  from flask_cors import CORS
20
+ # ... (بقية الاستيرادات)
21
 
22
+ def main():
23
+ # 1. تحميل وتشغيل peer_discovery.py أولاً
24
+ peer_module = load_and_run_peer_discovery()
25
+
26
+ if peer_module is None:
27
+ print("⚠️ سيستمر التشغيل بدون peer_discovery.py")
28
+ else:
29
+ # يمكنك الوصول إلى متغيرات ووظائف peer_discovery.py هنا
30
+ if hasattr(peer_module, 'CENTRAL_REGISTRY_SERVERS'):
31
+ print("السيرفرات المركزية:", peer_module.CENTRAL_REGISTRY_SERVERS)
32
+
33
+ # 2. متابعة تنفيذ باقي الوظائف
34
+ print("🚀 بدء تشغيل التطبيق الرئيسي...")
35
+ # ... باقي الكود
36
+
37
+ if __name__ == "__main__":
38
+ main()
39
 
40
  # أو مباشرة:
41
  # from peer_discovery import PORT as CPU_PORT
 
198
  except Exception as e:
199
  logging.error(f"خطأ في المراقبة التلقائية: {e}")
200
  time.sleep(5)
201
+ import importlib.util
202
+ import sys
203
+ from pathlib import Path
204
+ import time
205
+ import requests
206
+
207
+ def load_and_connect_to_central_server(max_attempts=10, retry_delay=5):
208
+ """
209
+ دالة محسنة لتحميل peer_discovery.py والمحاولة للاتصال بالسيرفر المركزي
210
+ حتى تنجح أو تصل إلى الحد الأقصى للمحاولات
211
+
212
+ Args:
213
+ max_attempts (int): الحد الأقصى لعدد المحاولات (0 للمحاولة إلى ما لا نهاية)
214
+ retry_delay (int): الوقت بين المحاولات بالثواني
215
+ """
216
+ # 1. تحميل ملف peer_discovery.py أولاً
217
+ try:
218
+ peer_discovery_path = Path(__file__).parent / "peer_discovery.py"
219
+
220
+ if not peer_discovery_path.exists():
221
+ raise FileNotFoundError(f"ملف peer_discovery.py غير موجود في {peer_discovery_path.parent}")
222
+
223
+ spec = importlib.util.spec_from_file_location("peer_discovery_module", peer_discovery_path)
224
+ peer_module = importlib.util.module_from_spec(spec)
225
+ spec.loader.exec_module(peer_module)
226
+
227
+ print("✅ تم تحميل peer_discovery.py بنجاح")
228
+ except Exception as e:
229
+ print(f"❌ خطأ في تحميل peer_discovery.py: {str(e)}")
230
+ return None
231
 
232
+ # 2. المحاولة للاتصال بالسيرفر المركزي
233
+ attempt = 0
234
+ while True:
235
+ attempt += 1
236
+ try:
237
+ if not hasattr(peer_module, 'CENTRAL_REGISTRY_SERVERS'):
238
+ raise AttributeError("لا يوجد تعريف للسيرفرات المركزية في peer_discovery.py")
239
+
240
+ servers = peer_module.CENTRAL_REGISTRY_SERVERS
241
+ if not servers:
242
+ raise ValueError("قائمة السيرفرات المركزية فارغة")
243
+
244
+ # اختيار سيرفر عشوائي للمحاولة
245
+ selected_server = random.choice(servers)
246
+
247
+ print(f"🔌 محاولة الاتصال بالسيرفر المركزي ({attempt}): {selected_server}")
248
+
249
+ # مثال على طلب اتصال (يمكن تعديله حسب حاجتك)
250
+ response = requests.get(f"{selected_server}/ping", timeout=5)
251
+ response.raise_for_status()
252
+
253
+ print(f"✅ تم الاتصال بنجاح بالسيرفر: {selected_server}")
254
+ return peer_module
255
+
256
+ except Exception as e:
257
+ print(f"❌ فشل الاتصال: {str(e)}")
258
+
259
+ if max_attempts > 0 and attempt >= max_attempts:
260
+ print(f"⚠️ تم الوصول للحد الأقصى للمحاولات ({max_attempts})")
261
+ return None
262
+
263
+ print(f"↻ إعادة المحاولة بعد {retry_delay} ثواني...")
264
+ time.sleep(retry_delay)
265
  # ─────────────── القائمة التفاعلية CLI ───────────────
266
  def menu(executor: DistributedExecutor):
267
  tasks = {
 
447
  except ImportError:
448
  logging.info("🛈 your_control غير متوفّر – تشغيل افتراضي")
449
 
450
+ main()