Уведомления. Правки
This commit is contained in:
+15
-13
@@ -1,15 +1,14 @@
|
||||
from datetime import datetime, timedelta
|
||||
import asyncpg
|
||||
import asyncio
|
||||
from aiogram import Bot, Router
|
||||
from aiogram.fsm.state import State, StatesGroup
|
||||
from aiogram import Bot, Router, types
|
||||
import logging
|
||||
from config import DATABASE_URL, ADMIN_USERNAME, ADMIN_PASSWORD, SERVERS
|
||||
from database import get_balance, update_key_expiry, delete_key, update_balance
|
||||
from client import extend_client_key, delete_client
|
||||
from auth import login_with_credentials
|
||||
from handlers.texts import KEY_EXPIRY_10H, KEY_EXPIRY_24H, KEY_RENEWED, KEY_RENEWAL_FAILED, KEY_DELETED, KEY_DELETION_FAILED
|
||||
from aiogram import Router, types
|
||||
from aiogram.fsm.state import StatesGroup, State
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -25,16 +24,16 @@ async def notify_expiring_keys(bot: Bot):
|
||||
conn = await asyncpg.connect(DATABASE_URL)
|
||||
logger.info("Подключение к базе данных успешно.")
|
||||
|
||||
current_time = datetime.utcnow().timestamp() * 1000
|
||||
threshold_time_10h = (datetime.utcnow() + timedelta(hours=10)).timestamp() * 1000
|
||||
threshold_time_24h = (datetime.utcnow() + timedelta(days=1)).timestamp() * 1000
|
||||
current_time = (datetime.utcnow() + timedelta(hours=3)).timestamp() * 1000
|
||||
threshold_time_10h = (datetime.utcnow() + timedelta(hours=10 + 3)).timestamp() * 1000
|
||||
threshold_time_24h = (datetime.utcnow() + timedelta(days=1 + 3)).timestamp() * 1000
|
||||
|
||||
logger.info("Начало обработки уведомлений.")
|
||||
|
||||
await notify_10h_keys(bot, conn, current_time, threshold_time_10h)
|
||||
await asyncio.sleep(1) # Задержка между уведомлениями за 10 часов и 24 часа
|
||||
await asyncio.sleep(1)
|
||||
await notify_24h_keys(bot, conn, current_time, threshold_time_24h)
|
||||
await asyncio.sleep(1) # Задержка перед обработкой истекших ключей
|
||||
await asyncio.sleep(1)
|
||||
await handle_expired_keys(bot, conn, current_time)
|
||||
|
||||
except Exception as e:
|
||||
@@ -126,7 +125,7 @@ async def notify_24h_keys(bot: Bot, conn: asyncpg.Connection, current_time: floa
|
||||
async def handle_expired_keys(bot: Bot, conn: asyncpg.Connection, current_time: float):
|
||||
logger.info("Проверка истекших ключей...")
|
||||
|
||||
current_time = int(current_time)
|
||||
current_time = (datetime.utcnow() + timedelta(hours=3)).timestamp() * 1000
|
||||
expiring_keys = await conn.fetch('''
|
||||
SELECT tg_id, client_id, expiry_time, server_id, email FROM keys
|
||||
WHERE expiry_time <= $1
|
||||
@@ -152,6 +151,9 @@ async def handle_expired_keys(bot: Bot, conn: asyncpg.Connection, current_time:
|
||||
await update_key_expiry(client_id, new_expiry_time)
|
||||
logger.info(f"Ключ для клиента {tg_id} продлен до {datetime.utcfromtimestamp(new_expiry_time / 1000).strftime('%Y-%m-%d %H:%M:%S')}.")
|
||||
|
||||
await conn.execute('UPDATE keys SET notified = FALSE, notified_24h = FALSE WHERE client_id = $1', client_id)
|
||||
logger.info(f"Поле notified и notified_24h для клиента {client_id} сброшены.")
|
||||
|
||||
session = await login_with_credentials(server_id, ADMIN_USERNAME, ADMIN_PASSWORD)
|
||||
success = await extend_client_key(session, server_id, tg_id, client_id, email, new_expiry_time)
|
||||
if success:
|
||||
@@ -175,14 +177,14 @@ async def handle_expired_keys(bot: Bot, conn: asyncpg.Connection, current_time:
|
||||
if success:
|
||||
try:
|
||||
await bot.send_message(tg_id, KEY_DELETED, reply_markup=keyboard)
|
||||
logger.info(f"Ключ для пользователя {tg_id} удален.")
|
||||
logger.info(f"Ключ для пользователя {tg_id} успешно удален.")
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при отправке уведомления об удалении ключа пользователю {tg_id}: {e}")
|
||||
logger.error(f"Ошибка при отправке уведомления о удалении ключа пользователю {tg_id}: {e}")
|
||||
else:
|
||||
try:
|
||||
await bot.send_message(tg_id, KEY_DELETION_FAILED, reply_markup=keyboard)
|
||||
logger.error(f"Не удалось удалить ключ для пользователя {tg_id}.")
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при отправке уведомления о неудачном удалении ключа пользователю {tg_id}: {e}")
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
await asyncio.sleep(1) # Задержка между уведомлениями
|
||||
|
||||
Reference in New Issue
Block a user