Browse Source

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 2 weeks ago
parent
commit
d012cfe6be
2 changed files with 7 additions and 8 deletions
  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:
             try:
                 await self.market_monitor.stop()
                 await self.market_monitor.stop()
                 if self.application:
                 if self.application:
-                    await self.application.stop()
                     await self.application.shutdown()
                     await self.application.shutdown()
             except Exception as e:
             except Exception as e:
                 logger.error(f"Error during shutdown: {e}") 
                 logger.error(f"Error during shutdown: {e}") 

+ 7 - 7
trading_bot.py

@@ -162,8 +162,8 @@ class BotManager:
         except Exception as e:
         except Exception as e:
             self.logger.error(f"❌ Bot error in BotManager.run_bot: {e}", exc_info=True)
             self.logger.error(f"❌ Bot error in BotManager.run_bot: {e}", exc_info=True)
             # Ensure cleanup is attempted
             # Ensure cleanup is attempted
-            if self.bot and self.bot.application and self.bot.application.is_running:
+            if self.bot and self.bot.application and self.bot.application._running:
-                 logger.info("Attempting to stop application due to error in run_bot...")
+                 self.logger.info("Attempting to stop application due to error in run_bot...")
                  await self.bot.application.stop()
                  await self.bot.application.stop()
             raise # Re-raise the exception to be caught by main()
             raise # Re-raise the exception to be caught by main()
     
     
@@ -200,18 +200,18 @@ def main():
         success = asyncio.run(bot_manager.start())
         success = asyncio.run(bot_manager.start())
         
         
     except KeyboardInterrupt:
     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.")
         print("\n👋 Bot stopped by user.")
     except Exception as e:
     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}")
         print(f"\n💥 Unexpected critical error: {e}")
     finally:
     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.")
         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 and bot_manager.bot and bot_manager.bot.application:
-             if bot_manager.bot.application.is_running:
+             if hasattr(bot_manager.bot.application, '_running') and bot_manager.bot.application._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.")
+                 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:
         if not success:
             print("\n❌ Bot session ended with errors.")
             print("\n❌ Bot session ended with errors.")