Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -167,39 +167,48 @@ def calculate_duration_str(timestamp_str):
|
|
| 167 |
# ==============================================================================
|
| 168 |
async def auto_pilot_loop():
|
| 169 |
"""
|
| 170 |
-
Background task that
|
|
|
|
| 171 |
"""
|
| 172 |
-
logger.info("🤖 [Auto-Pilot] Daemon started.")
|
| 173 |
while True:
|
| 174 |
try:
|
| 175 |
-
await asyncio.sleep(
|
| 176 |
|
| 177 |
-
# Safety check: System must be ready
|
| 178 |
if not sys_state.ready: continue
|
| 179 |
|
| 180 |
-
#
|
| 181 |
if hub_manager and int(time.time()) % 60 == 0:
|
| 182 |
is_ready, msg = await hub_manager.check_training_readiness()
|
| 183 |
sys_state.training_status_msg = msg
|
| 184 |
|
| 185 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
if sys_state.auto_pilot and not sys_state.cycle_running and not sys_state.training_running:
|
| 187 |
-
#
|
| 188 |
-
if
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
logger.info("🤖 [Auto-Pilot] Triggering scan...")
|
| 197 |
-
asyncio.create_task(run_unified_cycle())
|
| 198 |
-
await asyncio.sleep(5)
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
logger.error(f"⚠️ [Auto-Pilot Error] {e}")
|
| 202 |
-
await asyncio.sleep(30)
|
| 203 |
|
| 204 |
# ==============================================================================
|
| 205 |
# 🚀 Lifespan (Server Startup/Shutdown & Injection)
|
|
|
|
| 167 |
# ==============================================================================
|
| 168 |
async def auto_pilot_loop():
|
| 169 |
"""
|
| 170 |
+
Background task that manages scanning AND monitoring.
|
| 171 |
+
Fixes the 'Blind Spot' where Guardians were not checked if a trade was open.
|
| 172 |
"""
|
| 173 |
+
logger.info("🤖 [Auto-Pilot] Daemon started (Monitoring Mode Enabled).")
|
| 174 |
while True:
|
| 175 |
try:
|
| 176 |
+
await asyncio.sleep(5) # Check every 5 seconds (Faster response)
|
| 177 |
|
|
|
|
| 178 |
if not sys_state.ready: continue
|
| 179 |
|
| 180 |
+
# 1. حالة التدريب
|
| 181 |
if hub_manager and int(time.time()) % 60 == 0:
|
| 182 |
is_ready, msg = await hub_manager.check_training_readiness()
|
| 183 |
sys_state.training_status_msg = msg
|
| 184 |
|
| 185 |
+
# 2. فحص حالة الصفقات المفتوحة (Watchdog Check)
|
| 186 |
+
# هذا هو الجزء الذي كان مفقوداً! نتأكد من أن الحراس يعملون.
|
| 187 |
+
if trade_manager and len(trade_manager.open_positions) > 0:
|
| 188 |
+
# التأكد من عمل الحلقات الخلفية
|
| 189 |
+
wd_status = await trade_manager.ensure_active_guardians()
|
| 190 |
+
|
| 191 |
+
# تحديث واجهة المستخدم برسائل الحارس
|
| 192 |
+
# إذا كانت الرسالة "No active trades" نتجاهلها، غير ذلك نعرضها
|
| 193 |
+
if "No active" not in wd_status:
|
| 194 |
+
sys_state.last_cycle_logs = trade_manager.latest_guardian_log
|
| 195 |
+
continue # ننتقل للدورة التالية ولا نقوم بمسح جديد
|
| 196 |
+
|
| 197 |
+
# 3. Trigger Cycle (فقط إذا لم تكن هناك صفقات)
|
| 198 |
if sys_state.auto_pilot and not sys_state.cycle_running and not sys_state.training_running:
|
| 199 |
+
# Check time since last cycle
|
| 200 |
+
if sys_state.last_cycle_time:
|
| 201 |
+
elapsed = (datetime.now() - sys_state.last_cycle_time).total_seconds()
|
| 202 |
+
if elapsed < sys_state.scan_interval:
|
| 203 |
+
continue
|
| 204 |
+
|
| 205 |
+
logger.info("🤖 [Auto-Pilot] Triggering scan...")
|
| 206 |
+
asyncio.create_task(run_unified_cycle())
|
| 207 |
+
await asyncio.sleep(5)
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
logger.error(f"⚠️ [Auto-Pilot Error] {e}")
|
| 211 |
+
await asyncio.sleep(30)
|
| 212 |
|
| 213 |
# ==============================================================================
|
| 214 |
# 🚀 Lifespan (Server Startup/Shutdown & Injection)
|