Archived
feat(bot): add async DB and FSM state handling
This commit is contained in:
+12
-2
@@ -1,22 +1,25 @@
|
||||
import asyncio
|
||||
|
||||
from bot.api.asf import get_2fa_token, get_confirmations
|
||||
from bot.states import twofa_tasks
|
||||
from bot.logging_utils import get_logger
|
||||
from bot.state import twofa_tasks
|
||||
from bot.ui.keyboards import twofa_keyboard
|
||||
from bot.api.asf import get_2fa_token, get_confirmations
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
async def stop_twofa_task(message_id: int) -> None:
|
||||
task = twofa_tasks.pop(message_id, None)
|
||||
|
||||
if task:
|
||||
logger.info(
|
||||
"Остановка фоновой задачи обновления 2FA: message_id=%s", message_id
|
||||
)
|
||||
task.cancel()
|
||||
|
||||
try:
|
||||
await task
|
||||
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Ошибка при завершении фоновой задачи обновления 2FA: message_id=%s",
|
||||
@@ -32,6 +35,7 @@ async def auto_update_2fa(message, bot_name: str) -> None:
|
||||
bot_name,
|
||||
message_id,
|
||||
)
|
||||
|
||||
try:
|
||||
while True:
|
||||
code = await get_2fa_token(bot_name)
|
||||
@@ -43,6 +47,7 @@ async def auto_update_2fa(message, bot_name: str) -> None:
|
||||
text,
|
||||
reply_markup=twofa_keyboard(bot_name, bool(confirmations)),
|
||||
)
|
||||
|
||||
last_code = code
|
||||
logger.info(
|
||||
"Сообщение 2FA обновлено: bot=%s message_id=%s confirmations=%s",
|
||||
@@ -50,6 +55,7 @@ async def auto_update_2fa(message, bot_name: str) -> None:
|
||||
message_id,
|
||||
len(confirmations),
|
||||
)
|
||||
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Не удалось обновить сообщение 2FA: bot=%s message_id=%s",
|
||||
@@ -57,13 +63,16 @@ async def auto_update_2fa(message, bot_name: str) -> None:
|
||||
message_id,
|
||||
)
|
||||
break
|
||||
|
||||
await asyncio.sleep(15)
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger.info(
|
||||
"Фоновое обновление 2FA отменено: bot=%s message_id=%s",
|
||||
bot_name,
|
||||
message_id,
|
||||
)
|
||||
|
||||
except Exception as error:
|
||||
logger.exception(
|
||||
"Фоновое обновление 2FA завершилось ошибкой: bot=%s message_id=%s error=%s",
|
||||
@@ -71,6 +80,7 @@ async def auto_update_2fa(message, bot_name: str) -> None:
|
||||
message_id,
|
||||
error,
|
||||
)
|
||||
|
||||
finally:
|
||||
twofa_tasks.pop(message_id, None)
|
||||
logger.info(
|
||||
|
||||
Reference in New Issue
Block a user