File size: 15,293 Bytes
138f7cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
"""
Module: infrastructure.queue.priority_queue
Description: Priority queue system for task management
Author: Anderson H. Silva
Date: 2025-01-25
License: Proprietary - All rights reserved
"""
import asyncio
import heapq
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Callable, TypeVar, Generic
from enum import IntEnum
from dataclasses import dataclass, field
from uuid import uuid4
import json
from pydantic import BaseModel, Field
from src.core import get_logger
logger = get_logger(__name__)
T = TypeVar('T')
class TaskPriority(IntEnum):
"""Task priority levels."""
CRITICAL = 1 # Highest priority
HIGH = 2
NORMAL = 3
LOW = 4
BACKGROUND = 5 # Lowest priority
class TaskStatus(str):
"""Task status constants."""
PENDING = "pending"
PROCESSING = "processing"
COMPLETED = "completed"
FAILED = "failed"
CANCELLED = "cancelled"
RETRY = "retry"
@dataclass(order=True)
class PriorityTask:
"""Priority task with comparison support for heapq."""
priority: int
timestamp: float = field(compare=False)
task_id: str = field(compare=False)
task_type: str = field(compare=False)
payload: Dict[str, Any] = field(compare=False)
retry_count: int = field(default=0, compare=False)
max_retries: int = field(default=3, compare=False)
timeout: int = field(default=300, compare=False) # 5 minutes default
callback: Optional[str] = field(default=None, compare=False)
metadata: Dict[str, Any] = field(default_factory=dict, compare=False)
class TaskResult(BaseModel):
"""Task execution result."""
task_id: str
status: str
result: Optional[Any] = None
error: Optional[str] = None
started_at: datetime
completed_at: datetime
duration_seconds: float
retry_count: int = 0
class QueueStats(BaseModel):
"""Queue statistics."""
pending_tasks: int
processing_tasks: int
completed_tasks: int
failed_tasks: int
total_processed: int
average_processing_time: float
tasks_by_priority: Dict[str, int]
tasks_by_type: Dict[str, int]
class PriorityQueueService:
"""Priority queue service for managing tasks."""
def __init__(self, max_workers: int = 5):
"""Initialize priority queue service."""
self.max_workers = max_workers
self._queue: List[PriorityTask] = []
self._processing: Dict[str, PriorityTask] = {}
self._completed: Dict[str, TaskResult] = {}
self._failed: Dict[str, TaskResult] = {}
self._workers: List[asyncio.Task] = []
self._handlers: Dict[str, Callable] = {}
self._running = False
self._total_processed = 0
self._total_processing_time = 0.0
self._lock = asyncio.Lock()
logger.info(
"priority_queue_initialized",
max_workers=max_workers
)
async def start(self):
"""Start queue workers."""
if self._running:
return
self._running = True
# Start worker tasks
for i in range(self.max_workers):
worker = asyncio.create_task(self._worker(f"worker-{i}"))
self._workers.append(worker)
logger.info(
"priority_queue_started",
workers=len(self._workers)
)
async def stop(self):
"""Stop queue workers."""
self._running = False
# Cancel all workers
for worker in self._workers:
worker.cancel()
# Wait for workers to finish
await asyncio.gather(*self._workers, return_exceptions=True)
self._workers.clear()
logger.info("priority_queue_stopped")
def register_handler(self, task_type: str, handler: Callable):
"""Register a task handler."""
self._handlers[task_type] = handler
logger.info(
"task_handler_registered",
task_type=task_type,
handler=handler.__name__
)
async def enqueue(
self,
task_type: str,
payload: Dict[str, Any],
priority: TaskPriority = TaskPriority.NORMAL,
timeout: int = 300,
max_retries: int = 3,
callback: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None
) -> str:
"""
Enqueue a task with priority.
Args:
task_type: Type of task to execute
payload: Task payload data
priority: Task priority level
timeout: Task timeout in seconds
max_retries: Maximum retry attempts
callback: Optional callback URL
metadata: Optional task metadata
Returns:
Task ID
"""
task_id = str(uuid4())
task = PriorityTask(
priority=priority.value,
timestamp=datetime.now().timestamp(),
task_id=task_id,
task_type=task_type,
payload=payload,
timeout=timeout,
max_retries=max_retries,
callback=callback,
metadata=metadata or {}
)
async with self._lock:
heapq.heappush(self._queue, task)
logger.info(
"task_enqueued",
task_id=task_id,
task_type=task_type,
priority=priority.name,
queue_size=len(self._queue)
)
return task_id
async def dequeue(self) -> Optional[PriorityTask]:
"""Dequeue highest priority task."""
async with self._lock:
if self._queue:
task = heapq.heappop(self._queue)
self._processing[task.task_id] = task
return task
return None
async def get_task_status(self, task_id: str) -> Optional[str]:
"""Get task status."""
# Check if processing
if task_id in self._processing:
return TaskStatus.PROCESSING
# Check if completed
if task_id in self._completed:
return TaskStatus.COMPLETED
# Check if failed
if task_id in self._failed:
return TaskStatus.FAILED
# Check if in queue
async with self._lock:
for task in self._queue:
if task.task_id == task_id:
return TaskStatus.PENDING
return None
async def get_task_result(self, task_id: str) -> Optional[TaskResult]:
"""Get task result if completed or failed."""
if task_id in self._completed:
return self._completed[task_id]
elif task_id in self._failed:
return self._failed[task_id]
return None
async def cancel_task(self, task_id: str) -> bool:
"""Cancel a pending task."""
async with self._lock:
# Remove from queue if pending
self._queue = [t for t in self._queue if t.task_id != task_id]
heapq.heapify(self._queue)
# Cannot cancel if already processing
if task_id in self._processing:
return False
return True
async def get_stats(self) -> QueueStats:
"""Get queue statistics."""
tasks_by_priority = {}
tasks_by_type = {}
# Count pending tasks
async with self._lock:
for task in self._queue:
priority_name = TaskPriority(task.priority).name
tasks_by_priority[priority_name] = tasks_by_priority.get(priority_name, 0) + 1
tasks_by_type[task.task_type] = tasks_by_type.get(task.task_type, 0) + 1
avg_time = (
self._total_processing_time / self._total_processed
if self._total_processed > 0
else 0.0
)
return QueueStats(
pending_tasks=len(self._queue),
processing_tasks=len(self._processing),
completed_tasks=len(self._completed),
failed_tasks=len(self._failed),
total_processed=self._total_processed,
average_processing_time=avg_time,
tasks_by_priority=tasks_by_priority,
tasks_by_type=tasks_by_type
)
async def _worker(self, worker_id: str):
"""Worker coroutine to process tasks."""
logger.info(f"Worker {worker_id} started")
while self._running:
try:
# Get next task
task = await self.dequeue()
if not task:
# No tasks, wait a bit
await asyncio.sleep(0.1)
continue
# Process task
await self._process_task(task, worker_id)
except asyncio.CancelledError:
break
except Exception as e:
logger.error(
f"Worker {worker_id} error",
error=str(e),
exc_info=True
)
await asyncio.sleep(1)
logger.info(f"Worker {worker_id} stopped")
async def _process_task(self, task: PriorityTask, worker_id: str):
"""Process a single task."""
start_time = datetime.now()
logger.info(
"task_processing_started",
worker_id=worker_id,
task_id=task.task_id,
task_type=task.task_type
)
try:
# Get handler
handler = self._handlers.get(task.task_type)
if not handler:
raise ValueError(f"No handler registered for task type: {task.task_type}")
# Execute with timeout
result = await asyncio.wait_for(
handler(task.payload, task.metadata),
timeout=task.timeout
)
# Task completed successfully
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
task_result = TaskResult(
task_id=task.task_id,
status=TaskStatus.COMPLETED,
result=result,
started_at=start_time,
completed_at=end_time,
duration_seconds=duration,
retry_count=task.retry_count
)
self._completed[task.task_id] = task_result
self._processing.pop(task.task_id, None)
self._total_processed += 1
self._total_processing_time += duration
logger.info(
"task_completed",
worker_id=worker_id,
task_id=task.task_id,
duration=duration
)
# Execute callback if provided
if task.callback:
await self._execute_callback(task, task_result)
except asyncio.TimeoutError:
await self._handle_task_failure(
task, worker_id, "Task timeout", start_time
)
except Exception as e:
await self._handle_task_failure(
task, worker_id, str(e), start_time
)
async def _handle_task_failure(
self,
task: PriorityTask,
worker_id: str,
error: str,
start_time: datetime
):
"""Handle task failure with retry logic."""
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
task.retry_count += 1
if task.retry_count <= task.max_retries:
# Retry with exponential backoff
backoff = min(2 ** task.retry_count, 60) # Max 60 seconds
await asyncio.sleep(backoff)
# Re-enqueue with same priority
async with self._lock:
heapq.heappush(self._queue, task)
self._processing.pop(task.task_id, None)
logger.warning(
"task_retry",
worker_id=worker_id,
task_id=task.task_id,
retry_count=task.retry_count,
error=error
)
else:
# Max retries exceeded, mark as failed
task_result = TaskResult(
task_id=task.task_id,
status=TaskStatus.FAILED,
error=error,
started_at=start_time,
completed_at=end_time,
duration_seconds=duration,
retry_count=task.retry_count
)
self._failed[task.task_id] = task_result
self._processing.pop(task.task_id, None)
logger.error(
"task_failed",
worker_id=worker_id,
task_id=task.task_id,
error=error,
retry_count=task.retry_count
)
# Execute callback with failure
if task.callback:
await self._execute_callback(task, task_result)
async def _execute_callback(self, task: PriorityTask, result: TaskResult):
"""Execute task callback."""
try:
import httpx
async with httpx.AsyncClient() as client:
await client.post(
task.callback,
json={
"task_id": task.task_id,
"task_type": task.task_type,
"status": result.status,
"result": result.result,
"error": result.error,
"duration_seconds": result.duration_seconds
},
timeout=30.0
)
logger.info(
"callback_executed",
task_id=task.task_id,
callback=task.callback
)
except Exception as e:
logger.error(
"callback_failed",
task_id=task.task_id,
callback=task.callback,
error=str(e)
)
def clear_completed(self, older_than_minutes: int = 60):
"""Clear old completed tasks."""
cutoff_time = datetime.now() - timedelta(minutes=older_than_minutes)
# Clear old completed tasks
self._completed = {
task_id: result
for task_id, result in self._completed.items()
if result.completed_at > cutoff_time
}
# Clear old failed tasks
self._failed = {
task_id: result
for task_id, result in self._failed.items()
if result.completed_at > cutoff_time
}
logger.info(
"old_tasks_cleared",
remaining_completed=len(self._completed),
remaining_failed=len(self._failed)
)
# Global priority queue instance
priority_queue = PriorityQueueService() |