EmailBot/scheduler.py

57 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# scheduler.py
import asyncio
import logging
from database import get_target_email
from email_sender import send_batch_to_email
from queue_manager import file_queue
from timezone_utils import get_current_time, format_time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def batch_sender(bot):
"""
Фоновая задача: каждую минуту отправляет накопленные файлы
"""
while True:
try:
await asyncio.sleep(60)
queue_size = await file_queue.get_queue_size()
if queue_size == 0:
logger.info("Очередь пуста, пропускаем отправку")
continue
logger.info(f"Начинаем отправку {queue_size} файлов...")
target_email = await get_target_email()
if not target_email:
logger.warning("Целевая почта не настроена! Пропускаем отправку.")
continue
files = await file_queue.get_all_files()
if files:
# Отправляем пачку
await send_batch_to_email(target_email, files)
# Используем время Екатеринбурга для логов
current_time = format_time()
logger.info(f"✅ Отправлено {len(files)} файлов в {current_time}")
try:
from config import ADMIN_ID
await bot.send_message(
ADMIN_ID,
f"📧 Отправлен отчёт с {len(files)} файлами\n"
f"Время: {current_time}"
)
except Exception as e:
logger.error(f"Не удалось уведомить админа: {e}")
except Exception as e:
logger.error(f"Ошибка в batch_sender: {e}")
await asyncio.sleep(10)