From 1e0e883c6eb2b2607dcc67aa876bb8ad8b157ec2 Mon Sep 17 00:00:00 2001 From: Machka Pasla <161734431+machka-pasla@users.noreply.github.com> Date: Mon, 23 Jun 2025 20:12:44 +0300 Subject: [PATCH] Prevent duplicate scheduler startup --- bot/main_bot.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/bot/main_bot.py b/bot/main_bot.py index 10538c0..5995711 100644 --- a/bot/main_bot.py +++ b/bot/main_bot.py @@ -89,18 +89,24 @@ async def on_startup_configured(dispatcher: Dispatcher): async_session_factory: sessionmaker = dispatcher["async_session_factory"] logging.info("STARTUP: on_startup_configured executing...") - scheduler = AsyncIOScheduler(timezone="UTC") - try: - await schedule_subscription_notifications(bot, settings, i18n_instance, - scheduler, panel_service, - async_session_factory) - scheduler.start() - dispatcher["scheduler"] = scheduler - logging.info("STARTUP: APScheduler started.") - except Exception as e: - logging.error(f"STARTUP: Failed to start APScheduler: {e}", - exc_info=True) + existing_scheduler: Optional[AsyncIOScheduler] = dispatcher.get("scheduler") + + if existing_scheduler and existing_scheduler.running: + logging.warning( + "STARTUP: Scheduler already running, skipping initialization.") + else: + scheduler = AsyncIOScheduler(timezone="UTC") + try: + await schedule_subscription_notifications( + bot, settings, i18n_instance, scheduler, panel_service, + async_session_factory) + scheduler.start() + dispatcher["scheduler"] = scheduler + logging.info("STARTUP: APScheduler started.") + except Exception as e: + logging.error( + f"STARTUP: Failed to start APScheduler: {e}", exc_info=True) telegram_webhook_url_to_set = getattr(settings, 'TELEGRAM_WEBHOOK_BASE_URL', None)