Spaces:
Running on Zero
Running on Zero
Commit ·
4566c49
1
Parent(s): 8572d5c
fix library taps
Browse files- app/bot/handlers/errors.py +37 -0
- app/bot/handlers/library.py +16 -5
- app/bot/loader.py +2 -0
- app/db/session.py +3 -1
app/bot/handlers/errors.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import html
|
| 2 |
+
import logging
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
from aiogram import Bot
|
| 6 |
+
from aiogram.types import ErrorEvent
|
| 7 |
+
|
| 8 |
+
from app.config import get_settings
|
| 9 |
+
|
| 10 |
+
log = logging.getLogger(__name__)
|
| 11 |
+
_last_notified: dict[str, float] = {}
|
| 12 |
+
NOTICE = "⚠️ ችግር ተፈጥሯል፣ እባክዎ እንደገና ይሞክሩ።\nSomething went wrong, please try again."
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
async def on_error(event: ErrorEvent, bot: Bot) -> bool:
|
| 16 |
+
exc = event.exception
|
| 17 |
+
log.error("Unhandled error while processing an update", exc_info=exc)
|
| 18 |
+
update = event.update
|
| 19 |
+
try: # never leave the user staring at a silent button
|
| 20 |
+
if update.callback_query:
|
| 21 |
+
await update.callback_query.answer(NOTICE[:190], show_alert=True)
|
| 22 |
+
elif update.message:
|
| 23 |
+
await update.message.answer(NOTICE)
|
| 24 |
+
except Exception:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
key = f"{type(exc).__name__}:{str(exc)[:80]}"
|
| 28 |
+
now = time.monotonic()
|
| 29 |
+
if now - _last_notified.get(key, 0) > 300: # at most one admin alert per distinct error / 5 min
|
| 30 |
+
_last_notified[key] = now
|
| 31 |
+
text = f"⚠️ <b>{html.escape(type(exc).__name__)}</b>\n<code>{html.escape(str(exc)[:1500])}</code>"
|
| 32 |
+
for admin_id in get_settings().admin_id_set:
|
| 33 |
+
try:
|
| 34 |
+
await bot.send_message(admin_id, text)
|
| 35 |
+
except Exception:
|
| 36 |
+
pass
|
| 37 |
+
return True
|
app/bot/handlers/library.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from typing import Union
|
| 2 |
|
| 3 |
from aiogram import F, Router
|
|
@@ -13,16 +15,25 @@ from app.db.models import User
|
|
| 13 |
from app.db.repos import library as repo
|
| 14 |
from app.services import library as lib
|
| 15 |
|
|
|
|
| 16 |
router = Router()
|
| 17 |
|
| 18 |
|
| 19 |
async def _show(target: Union[Message, CallbackQuery], text: str, markup) -> None:
|
| 20 |
-
"""Edit the current message when navigating, send a new one when opening from the menu.
|
|
|
|
| 21 |
if isinstance(target, CallbackQuery):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
await target.answer(text, reply_markup=markup)
|
| 28 |
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import logging
|
| 3 |
from typing import Union
|
| 4 |
|
| 5 |
from aiogram import F, Router
|
|
|
|
| 15 |
from app.db.repos import library as repo
|
| 16 |
from app.services import library as lib
|
| 17 |
|
| 18 |
+
log = logging.getLogger(__name__)
|
| 19 |
router = Router()
|
| 20 |
|
| 21 |
|
| 22 |
async def _show(target: Union[Message, CallbackQuery], text: str, markup) -> None:
|
| 23 |
+
"""Edit the current message when navigating, send a new one when opening from the menu.
|
| 24 |
+
If editing fails for any reason, send a fresh message instead of staying silent."""
|
| 25 |
if isinstance(target, CallbackQuery):
|
| 26 |
+
if not target.message:
|
| 27 |
+
await target.answer()
|
| 28 |
+
return
|
| 29 |
+
_, edited = await asyncio.gather(
|
| 30 |
+
target.answer(),
|
| 31 |
+
target.message.edit_text(text, reply_markup=markup),
|
| 32 |
+
return_exceptions=True,
|
| 33 |
+
)
|
| 34 |
+
if isinstance(edited, Exception) and "not modified" not in str(edited):
|
| 35 |
+
log.warning("edit_text failed (%r); sending a new message instead", edited)
|
| 36 |
+
await target.message.answer(text, reply_markup=markup)
|
| 37 |
else:
|
| 38 |
await target.answer(text, reply_markup=markup)
|
| 39 |
|
app/bot/loader.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from aiogram import Dispatcher
|
| 2 |
|
| 3 |
from app.bot.handlers import build_router
|
|
|
|
| 4 |
from app.bot.middlewares.context import ContextMiddleware
|
| 5 |
from app.bot.middlewares.gate import GateMiddleware
|
| 6 |
from app.bot.middlewares.throttle import ThrottleMiddleware
|
|
@@ -13,4 +14,5 @@ def build_dispatcher() -> Dispatcher:
|
|
| 13 |
observer.middleware(ContextMiddleware()) # must come before the gate
|
| 14 |
observer.middleware(GateMiddleware())
|
| 15 |
dp.include_router(build_router())
|
|
|
|
| 16 |
return dp
|
|
|
|
| 1 |
from aiogram import Dispatcher
|
| 2 |
|
| 3 |
from app.bot.handlers import build_router
|
| 4 |
+
from app.bot.handlers.errors import on_error
|
| 5 |
from app.bot.middlewares.context import ContextMiddleware
|
| 6 |
from app.bot.middlewares.gate import GateMiddleware
|
| 7 |
from app.bot.middlewares.throttle import ThrottleMiddleware
|
|
|
|
| 14 |
observer.middleware(ContextMiddleware()) # must come before the gate
|
| 15 |
observer.middleware(GateMiddleware())
|
| 16 |
dp.include_router(build_router())
|
| 17 |
+
dp.errors.register(on_error)
|
| 18 |
return dp
|
app/db/session.py
CHANGED
|
@@ -22,7 +22,9 @@ def _build_engine():
|
|
| 22 |
# - drop ?sslmode=..., force asyncpg, require SSL
|
| 23 |
# - statement_cache_size=0 (pgbouncer transaction mode has no session prepared statements)
|
| 24 |
# - small client-side pool + pre_ping so stale connections never reach a handler
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
connect_args = {"ssl": "require", "statement_cache_size": 0}
|
| 27 |
kwargs.update(pool_size=5, max_overflow=5, pool_pre_ping=True, pool_recycle=1800)
|
| 28 |
return create_async_engine(url, connect_args=connect_args, **kwargs)
|
|
|
|
| 22 |
# - drop ?sslmode=..., force asyncpg, require SSL
|
| 23 |
# - statement_cache_size=0 (pgbouncer transaction mode has no session prepared statements)
|
| 24 |
# - small client-side pool + pre_ping so stale connections never reach a handler
|
| 25 |
+
# - prepared_statement_cache_size=0: SQLAlchemy keeps its OWN prepared-statement cache, which is
|
| 26 |
+
# unsafe when the pooler hands each transaction a different backend connection
|
| 27 |
+
url = url.set(drivername="postgresql+asyncpg", query={"prepared_statement_cache_size": "0"})
|
| 28 |
connect_args = {"ssl": "require", "statement_cache_size": 0}
|
| 29 |
kwargs.update(pool_size=5, max_overflow=5, pool_pre_ping=True, pool_recycle=1800)
|
| 30 |
return create_async_engine(url, connect_args=connect_args, **kwargs)
|