Explorar o código

Refactor copy trading monitor initialization in MonitoringCoordinator

- Updated the initialization process for the copy trading monitor to import and set it up without auto-starting, addressing previous blocking issues.
- Enhanced error handling to log specific import errors and initialization failures, improving robustness.
- Updated logging messages to reflect the current state of the copy trading monitor during initialization.
Carles Sentis hai 5 días
pai
achega
a0b99ded77
Modificáronse 2 ficheiros con 26 adicións e 20 borrados
  1. 25 19
      src/monitoring/monitoring_coordinator.py
  2. 1 1
      trading_bot.py

+ 25 - 19
src/monitoring/monitoring_coordinator.py

@@ -40,19 +40,23 @@ class MonitoringCoordinator:
         self.risk_manager = RiskManager(hl_client, notification_manager, config)
         self.alarm_manager = AlarmManager()  # AlarmManager only needs alarms_file (defaults to data/price_alarms.json)
         
-        # Initialize copy trading monitor if available
-        if COPY_TRADING_AVAILABLE:
-            try:
-                # DISABLE AGAIN: Still causing blocking issues
-                # self.copy_trading_monitor = CopyTradingMonitor(hl_client, notification_manager)
-                # logger.info("✅ Copy trading monitor initialized (with non-blocking state manager)")
-                self.copy_trading_monitor = None
-                logger.info("🚫 Copy trading monitor disabled - still causing blocking")
-            except Exception as e:
-                logger.error(f"❌ Failed to initialize copy trading monitor: {e}")
-                self.copy_trading_monitor = None
-        else:
+        # Initialize copy trading monitor (Step 2: Initialize but don't start)
+        try:
+            from src.monitoring.copy_trading_monitor import CopyTradingMonitor
+            self.copy_trading_monitor = CopyTradingMonitor(
+                self.hl_client,
+                self.notification_manager
+            )
+            logger.info("✅ Copy trading monitor initialized (Step 2: not auto-starting)")
+            self.copy_trading_available = True
+        except ImportError as e:
+            logger.warning(f"Copy trading monitor not available: {e}")
             self.copy_trading_monitor = None
+            self.copy_trading_available = False
+        except Exception as e:
+            logger.error(f"Error initializing copy trading monitor: {e}")
+            self.copy_trading_monitor = None
+            self.copy_trading_available = False
         
         # Exchange order synchronization (will be initialized with trading stats)
         self.exchange_order_sync = None
@@ -79,13 +83,15 @@ class MonitoringCoordinator:
             await self.risk_manager.start()
             # AlarmManager doesn't have start() method - it's always ready
             
-            # Start copy trading monitor if enabled
-            if self.copy_trading_monitor and hasattr(self.copy_trading_monitor, 'enabled') and self.copy_trading_monitor.enabled:
-                try:
-                    asyncio.create_task(self.copy_trading_monitor.start_monitoring())
-                    logger.info("🔄 Copy trading monitor started (with non-blocking state manager)")
-                except Exception as e:
-                    logger.error(f"❌ Failed to start copy trading monitor: {e}")
+            # Start copy trading monitor if enabled (Step 2: Don't auto-start yet)
+            # if self.copy_trading_monitor and hasattr(self.copy_trading_monitor, 'enabled') and self.copy_trading_monitor.enabled:
+            #     try:
+            #         asyncio.create_task(self.copy_trading_monitor.start_monitoring())
+            #         logger.info("🔄 Copy trading monitor started (with non-blocking state manager)")
+            #     except Exception as e:
+            #         logger.error(f"❌ Failed to start copy trading monitor: {e}")
+            if self.copy_trading_monitor:
+                logger.info("🔄 Copy trading monitor initialized but not auto-started (Step 2 testing)")
             
             # Initialize exchange order sync with trading stats
             self._init_exchange_order_sync()

+ 1 - 1
trading_bot.py

@@ -14,7 +14,7 @@ from datetime import datetime
 from pathlib import Path
 
 # Bot version
-BOT_VERSION = "3.0.325"
+BOT_VERSION = "3.0.326"
 
 # Add src directory to Python path
 sys.path.insert(0, str(Path(__file__).parent / "src"))