Forráskód Böngészése

Refactor logging and shutdown handling in trading bot - Updated logging calls to use the module logger for consistency and improved clarity. Adjusted shutdown logic to check for the application's running state using the private attribute, enhancing error handling during bot termination.

Carles Sentis 5 napja
szülő
commit
d012cfe6be
2 módosított fájl, 7 hozzáadás és 8 törlés
  1. 0 1
      src/bot/core.py
  2. 7 7
      trading_bot.py

+ 0 - 1
src/bot/core.py

@@ -303,7 +303,6 @@ For support or issues, check the logs or contact the administrator.
             try:
                 await self.market_monitor.stop()
                 if self.application:
-                    await self.application.stop()
                     await self.application.shutdown()
             except Exception as e:
                 logger.error(f"Error during shutdown: {e}") 

+ 7 - 7
trading_bot.py

@@ -162,8 +162,8 @@ class BotManager:
         except Exception as e:
             self.logger.error(f"❌ Bot error in BotManager.run_bot: {e}", exc_info=True)
             # Ensure cleanup is attempted
-            if self.bot and self.bot.application and self.bot.application.is_running:
-                 logger.info("Attempting to stop application due to error in run_bot...")
+            if self.bot and self.bot.application and self.bot.application._running:
+                 self.logger.info("Attempting to stop application due to error in run_bot...")
                  await self.bot.application.stop()
             raise # Re-raise the exception to be caught by main()
     
@@ -200,18 +200,18 @@ def main():
         success = asyncio.run(bot_manager.start())
         
     except KeyboardInterrupt:
-        logger.info("👋 Bot stopped by user (Ctrl+C in main).")
+        logging.getLogger(__name__).info("👋 Bot stopped by user (Ctrl+C in main).")
         print("\n👋 Bot stopped by user.")
     except Exception as e:
-        logger.critical(f"💥 Unexpected critical error in main: {e}", exc_info=True)
+        logging.getLogger(__name__).critical(f"💥 Unexpected critical error in main: {e}", exc_info=True)
         print(f"\n💥 Unexpected critical error: {e}")
     finally:
-        logger.info("BotManager main function finished.")
+        logging.getLogger(__name__).info("BotManager main function finished.")
         print("📊 Your trading statistics should have been saved by the bot's internal shutdown.")
 
         if bot_manager and bot_manager.bot and bot_manager.bot.application:
-             if bot_manager.bot.application.is_running:
-                 logger.warning("Application was still marked as running in main finally block. Attempting forced synchronous stop. This may not work if loop is closed.")
+             if hasattr(bot_manager.bot.application, '_running') and bot_manager.bot.application._running:
+                 logging.getLogger(__name__).warning("Application was still marked as running in main finally block. Attempting forced synchronous stop. This may not work if loop is closed.")
         
         if not success:
             print("\n❌ Bot session ended with errors.")