Skip to content

Commit 9477ac3

Browse files
chore: remove unnecessarily code
1 parent 03e6904 commit 9477ac3

File tree

1 file changed

+3
-42
lines changed

1 file changed

+3
-42
lines changed

app/telegram/__init__.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import asyncio
2-
import signal
32
from asyncio import Lock
4-
from threading import Thread
53

64
from aiogram import Bot, Dispatcher
75
from aiogram.client.default import DefaultBotProperties
@@ -35,15 +33,6 @@ def get_dispatcher():
3533
return _dp
3634

3735

38-
def _signal_handler():
39-
"""Handle SIGINT/SIGTERM signals during polling"""
40-
logger.info("Signal received, shutting down telegram bot...")
41-
if _polling_task and not _polling_task.done():
42-
# Force stop dispatcher and cancel task
43-
_dp._running = False
44-
_polling_task.cancel()
45-
46-
4736
async def startup_telegram_bot():
4837
restart = False
4938
global _bot
@@ -72,15 +61,6 @@ async def startup_telegram_bot():
7261

7362
try:
7463
if settings.method == RunMethod.LONGPOLLING:
75-
# Set up signal handlers for graceful shutdown
76-
for sig in (signal.SIGTERM, signal.SIGINT):
77-
try:
78-
signal.signal(sig, lambda s, f: _signal_handler())
79-
except ValueError:
80-
# Signal only works in main thread
81-
pass
82-
83-
# Create polling task that responds to signals
8464
_polling_task = asyncio.create_task(_dp.start_polling(_bot, handle_signals=False))
8565
else:
8666
# register webhook
@@ -117,26 +97,12 @@ async def shutdown_telegram_bot():
11797
if _polling_task is not None and not _polling_task.done():
11898
logger.info("stopping long polling")
11999
# Force stop the dispatcher first
120-
_dp._running = False
100+
await _dp.stop_polling()
121101
# Cancel the polling task
122102
_polling_task.cancel()
123-
try:
124-
# Give it limited time to respond to cancellation
125-
await asyncio.wait_for(_polling_task, timeout=3.0)
126-
except asyncio.TimeoutError:
127-
logger.warning("Polling task did not respond to cancellation, forcing shutdown")
128-
except asyncio.CancelledError:
129-
logger.info("Telegram bot polling task cancelled successfully")
130-
except Exception as e:
131-
logger.warning(f"Error during polling task cancellation: {e}")
132-
finally:
133-
_polling_task = None
103+
_polling_task = None
134104
else:
135-
try:
136-
await asyncio.wait_for(_bot.get_webhook_info(), timeout=5.0)
137-
await _bot.delete_webhook(drop_pending_updates=True)
138-
except asyncio.TimeoutError:
139-
logger.warning("Webhook cleanup timed out")
105+
await _bot.delete_webhook(drop_pending_updates=True)
140106
except (
141107
TelegramNetworkError,
142108
TelegramRetryAfter,
@@ -145,11 +111,6 @@ async def shutdown_telegram_bot():
145111
) as err:
146112
if hasattr(err, "message"):
147113
logger.error(err.message)
148-
# elif isinstance(err, TelegramUnauthorizedError):
149-
# try:
150-
# asyncio.create_task(_dp.stop_polling())
151-
# except Exception:
152-
# pass
153114
else:
154115
logger.error(err)
155116

0 commit comments

Comments
 (0)