|
@@ -350,8 +350,6 @@ For support or issues, check the logs or contact the administrator.
|
|
|
|
|
|
self.setup_handlers()
|
|
|
|
|
|
- keep_running_future = asyncio.Future()
|
|
|
-
|
|
|
try:
|
|
|
logger.info("🚀 Initializing bot application (v20.x style)...")
|
|
|
await self.application.initialize()
|
|
@@ -371,37 +369,18 @@ For support or issues, check the logs or contact the administrator.
|
|
|
logger.info("▶️ Starting PTB application's internal tasks (update processing, job queue).")
|
|
|
await self.application.start()
|
|
|
|
|
|
- if self.application.updater:
|
|
|
- logger.info(f"▶️ Activating PTB updater to fetch updates (drop_pending_updates={Config.TELEGRAM_DROP_PENDING_UPDATES}).")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- await self.application.updater.start_polling(drop_pending_updates=Config.TELEGRAM_DROP_PENDING_UPDATES)
|
|
|
- else:
|
|
|
- logger.error("❌ Critical: Application updater is not initialized. Bot cannot receive Telegram updates.")
|
|
|
-
|
|
|
-
|
|
|
- if not keep_running_future.done():
|
|
|
- keep_running_future.set_exception(RuntimeError("Updater not available"))
|
|
|
-
|
|
|
-
|
|
|
- logger.info("✅ Bot is initialized and updater is polling. Awaiting stop signal via keep_running_future or Ctrl+C.")
|
|
|
- await keep_running_future
|
|
|
-
|
|
|
+ logger.info(f"▶️ Starting polling (drop_pending_updates={Config.TELEGRAM_DROP_PENDING_UPDATES})...")
|
|
|
+ await self.application.run_polling(drop_pending_updates=Config.TELEGRAM_DROP_PENDING_UPDATES)
|
|
|
+
|
|
|
+ logger.info("✅ Bot is initialized and polling. Awaiting stop signal or Ctrl+C.")
|
|
|
+
|
|
|
+
|
|
|
except (KeyboardInterrupt, SystemExit) as e:
|
|
|
logger.info(f"🛑 Bot run interrupted by {type(e).__name__}. Initiating shutdown (v20.x style)...")
|
|
|
- if not keep_running_future.done():
|
|
|
- keep_running_future.set_exception(e if isinstance(e, SystemExit) else KeyboardInterrupt())
|
|
|
except asyncio.CancelledError:
|
|
|
logger.info("🛑 Bot run task cancelled. Initiating shutdown (v20.x style)...")
|
|
|
- if not keep_running_future.done():
|
|
|
- keep_running_future.cancel()
|
|
|
except Exception as e:
|
|
|
logger.error(f"❌ Unhandled error in bot run loop (v20.x style): {e}", exc_info=True)
|
|
|
- if not keep_running_future.done():
|
|
|
- keep_running_future.set_exception(e)
|
|
|
finally:
|
|
|
logger.info("🔌 Starting graceful shutdown sequence in TelegramTradingBot.run (v20.x style)...")
|
|
|
try:
|
|
@@ -410,18 +389,11 @@ For support or issues, check the logs or contact the administrator.
|
|
|
logger.info("Market monitor stopped.")
|
|
|
|
|
|
if self.application:
|
|
|
-
|
|
|
- if self.application.updater and self.application.updater.running:
|
|
|
- logger.info("Stopping PTB updater polling...")
|
|
|
- await self.application.updater.stop()
|
|
|
- logger.info("PTB updater polling stopped.")
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
if self.application.running:
|
|
|
logger.info("Stopping PTB application components (handlers, job queue)...")
|
|
|
await self.application.stop()
|
|
|
logger.info("PTB application components stopped.")
|
|
|
-
|
|
|
|
|
|
logger.info("Shutting down PTB application (bot, persistence, etc.)...")
|
|
|
await self.application.shutdown()
|