"""
队列服务
提供任务队列管理功能
"""

import structlog
from typing import Dict, Any
from datetime import datetime

logger = structlog.get_logger()

class QueueService:
    """队列服务类"""
    
    def __init__(self):
        self.active_workers = 0
        self.queue_size = 0
        self.total_processed = 0
        self.total_failed = 0
        self._stats = {
            "active_workers": 0,
            "queue_size": 0,
            "total_processed": 0,
            "total_failed": 0
        }
    
    async def get_stats(self) -> Dict[str, Any]:
        """获取队列统计信息"""
        return self._stats.copy()
    
    async def increment_processed(self):
        """增加已处理任务计数"""
        self.total_processed += 1
        self._stats["total_processed"] = self.total_processed
    
    async def increment_failed(self):
        """增加失败任务计数"""
        self.total_failed += 1
        self._stats["total_failed"] = self.total_failed
    
    async def set_active_workers(self, count: int):
        """设置活跃工作线程数"""
        self.active_workers = count
        self._stats["active_workers"] = count
    
    async def set_queue_size(self, size: int):
        """设置队列大小"""
        self.queue_size = size
        self._stats["queue_size"] = size
    
    async def add_to_queue(self):
        """添加任务到队列"""
        self.queue_size += 1
        self._stats["queue_size"] = self.queue_size
    
    async def remove_from_queue(self):
        """从队列移除任务"""
        if self.queue_size > 0:
            self.queue_size -= 1
            self._stats["queue_size"] = self.queue_size

# 全局服务实例
_queue_service = None

def get_queue_service() -> QueueService:
    """获取队列服务实例"""
    global _queue_service
    if _queue_service is None:
        _queue_service = QueueService()
    return _queue_service 