EmailBot/queue_manager.py

73 lines
2.6 KiB
Python
Raw 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.

# queue_manager.py - обновляем timestamp
import asyncio
from datetime import datetime
from typing import List, Dict
from dataclasses import dataclass
import logging
from timezone_utils import get_current_time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class QueuedFile:
"""Класс для хранения любого файла в очереди"""
file_bytes: bytes
filename: str
user_info: str
user_id: int
file_type: str
timestamp: datetime # Теперь будет в часовом поясе Екатеринбурга
class FileQueue:
"""Управление очередью файлов"""
def __init__(self):
self.queue: List[QueuedFile] = []
self.lock = asyncio.Lock()
async def add_file(self, file_bytes: bytes, filename: str, user_info: str, user_id: int, file_type: str):
"""Добавить файл в очередь с временем Екатеринбурга"""
async with self.lock:
self.queue.append(QueuedFile(
file_bytes=file_bytes,
filename=filename,
user_info=user_info,
user_id=user_id,
file_type=file_type,
timestamp=get_current_time() # Время Екатеринбурга
))
logger.info(f"Файл '{filename}' добавлен в очередь. Всего в очереди: {len(self.queue)}")
async def get_all_files(self) -> List[QueuedFile]:
"""Получить все файлы и очистить очередь"""
async with self.lock:
files = self.queue.copy()
self.queue.clear()
return files
async def get_queue_size(self) -> int:
"""Получить размер очереди"""
async with self.lock:
return len(self.queue)
async def peek_queue(self, limit: int = 10) -> List[dict]:
"""Посмотреть первые N файлов в очереди (не забирая их)"""
async with self.lock:
from timezone_utils import format_time
result = []
for file in self.queue[:limit]:
result.append({
'filename': file.filename,
'user_info': file.user_info,
'size_kb': len(file.file_bytes) / 1024,
'timestamp': format_time(file.timestamp)
})
return result
# Глобальный экземпляр очереди
file_queue = FileQueue()
image_queue = file_queue # Для обратной совместимости