File size: 27,545 Bytes
50a7bf0 |
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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
"""
Queue management service for handling job queues and processing.
This service provides business logic for job queue operations,
including queue management, job processing coordination, and monitoring.
"""
import logging
from datetime import datetime, timedelta
from typing import Optional, Dict, Any, List
from redis.asyncio import Redis
from ..models.job import Job, JobStatus
from ..core.redis import RedisKeyManager, redis_json_get, redis_json_set
logger = logging.getLogger(__name__)
class QueueService:
"""
Service class for job queue management operations.
Handles job queue operations, processing coordination,
and queue monitoring functionality.
"""
def __init__(self, redis_client: Redis):
self.redis_client = redis_client
async def enqueue_job(self, job_id: str, priority: str = "normal") -> bool:
"""
Add a job to the processing queue.
Args:
job_id: Job ID to enqueue
priority: Job priority (affects queue position)
Returns:
True if job was enqueued successfully, False otherwise
"""
try:
# Add job to appropriate queue based on priority
if priority == "urgent":
# Add to front of queue for urgent jobs
await self.redis_client.lpush(RedisKeyManager.JOB_QUEUE, job_id)
elif priority == "high":
# Add near front for high priority jobs
queue_length = await self.redis_client.llen(RedisKeyManager.JOB_QUEUE)
insert_position = min(queue_length // 4, 10) # Insert in first quarter or top 10
if insert_position == 0:
await self.redis_client.lpush(RedisKeyManager.JOB_QUEUE, job_id)
else:
# This is a simplified approach - in production you might use a priority queue
await self.redis_client.rpush(RedisKeyManager.JOB_QUEUE, job_id)
else:
# Normal and low priority jobs go to the end
await self.redis_client.rpush(RedisKeyManager.JOB_QUEUE, job_id)
logger.info(f"Job {job_id} enqueued with priority {priority}")
return True
except Exception as e:
logger.error(f"Failed to enqueue job {job_id}: {e}", exc_info=True)
return False
async def dequeue_job(self) -> Optional[str]:
"""
Remove and return the next job from the queue.
Returns:
Job ID if available, None if queue is empty
"""
try:
# Use blocking pop with timeout to get next job
result = await self.redis_client.blpop(RedisKeyManager.JOB_QUEUE, timeout=1)
if result:
queue_name, job_id = result
logger.info(f"Dequeued job {job_id}")
return job_id
return None
except Exception as e:
logger.error(f"Failed to dequeue job: {e}", exc_info=True)
return None
async def peek_next_job(self) -> Optional[str]:
"""
Get the next job ID without removing it from the queue.
Returns:
Next job ID if available, None if queue is empty
"""
try:
job_id = await self.redis_client.lindex(RedisKeyManager.JOB_QUEUE, 0)
return job_id
except Exception as e:
logger.error(f"Failed to peek next job: {e}", exc_info=True)
return None
async def get_queue_length(self) -> int:
"""
Get current queue length.
Returns:
Number of jobs in queue
"""
try:
return await self.redis_client.llen(RedisKeyManager.JOB_QUEUE)
except Exception as e:
logger.error(f"Failed to get queue length: {e}")
return 0
async def get_queue_position(self, job_id: str) -> Optional[int]:
"""
Get the position of a job in the queue.
Args:
job_id: Job ID to find
Returns:
Queue position (0-based) or None if not found
"""
try:
# Get all jobs in queue
queue_jobs = await self.redis_client.lrange(RedisKeyManager.JOB_QUEUE, 0, -1)
try:
position = queue_jobs.index(job_id)
return position
except ValueError:
return None
except Exception as e:
logger.error(f"Failed to get queue position for job {job_id}: {e}")
return None
async def remove_job_from_queue(self, job_id: str) -> bool:
"""
Remove a specific job from the queue.
Args:
job_id: Job ID to remove
Returns:
True if job was removed, False otherwise
"""
try:
# Remove all occurrences of the job ID from the queue
removed_count = await self.redis_client.lrem(RedisKeyManager.JOB_QUEUE, 0, job_id)
if removed_count > 0:
logger.info(f"Removed job {job_id} from queue")
return True
else:
logger.warning(f"Job {job_id} not found in queue")
return False
except Exception as e:
logger.error(f"Failed to remove job {job_id} from queue: {e}")
return False
async def get_queue_stats(self) -> Dict[str, Any]:
"""
Get comprehensive queue statistics.
Returns:
Dictionary containing queue statistics
"""
try:
queue_length = await self.get_queue_length()
# Get processing statistics from Redis
processing_key = "queue:processing"
processing_jobs = await self.redis_client.scard(processing_key)
# Get completion statistics for today
today = datetime.utcnow().date()
completed_today_key = f"queue:completed:{today}"
failed_today_key = f"queue:failed:{today}"
completed_today = await self.redis_client.get(completed_today_key) or 0
failed_today = await self.redis_client.get(failed_today_key) or 0
# Calculate average processing time (simplified)
avg_processing_time = await self._get_average_processing_time()
return {
"queue_length": queue_length,
"processing_jobs": processing_jobs,
"completed_today": int(completed_today),
"failed_today": int(failed_today),
"average_processing_time_minutes": avg_processing_time,
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
logger.error(f"Failed to get queue stats: {e}", exc_info=True)
return {
"queue_length": 0,
"processing_jobs": 0,
"completed_today": 0,
"failed_today": 0,
"average_processing_time_minutes": 0,
"timestamp": datetime.utcnow().isoformat()
}
async def mark_job_processing(self, job_id: str) -> bool:
"""
Mark a job as currently processing.
Args:
job_id: Job ID to mark as processing
Returns:
True if marked successfully, False otherwise
"""
try:
processing_key = "queue:processing"
await self.redis_client.sadd(processing_key, job_id)
# Set processing timestamp
processing_time_key = f"queue:processing_time:{job_id}"
await self.redis_client.set(
processing_time_key,
datetime.utcnow().isoformat(),
ex=3600 # Expire after 1 hour
)
logger.info(f"Marked job {job_id} as processing")
return True
except Exception as e:
logger.error(f"Failed to mark job {job_id} as processing: {e}")
return False
async def mark_job_completed(self, job_id: str, success: bool = True) -> bool:
"""
Mark a job as completed and update statistics.
Args:
job_id: Job ID to mark as completed
success: Whether job completed successfully
Returns:
True if marked successfully, False otherwise
"""
try:
# Remove from processing set
processing_key = "queue:processing"
await self.redis_client.srem(processing_key, job_id)
# Update completion statistics
today = datetime.utcnow().date()
if success:
completed_key = f"queue:completed:{today}"
await self.redis_client.incr(completed_key)
await self.redis_client.expire(completed_key, 86400 * 7) # Keep for 7 days
else:
failed_key = f"queue:failed:{today}"
await self.redis_client.incr(failed_key)
await self.redis_client.expire(failed_key, 86400 * 7) # Keep for 7 days
# Calculate and store processing time
processing_time_key = f"queue:processing_time:{job_id}"
start_time_str = await self.redis_client.get(processing_time_key)
if start_time_str:
start_time = datetime.fromisoformat(start_time_str)
processing_duration = (datetime.utcnow() - start_time).total_seconds() / 60 # minutes
# Store processing time for statistics
processing_times_key = "queue:processing_times"
await self.redis_client.lpush(processing_times_key, processing_duration)
await self.redis_client.ltrim(processing_times_key, 0, 999) # Keep last 1000 times
# Clean up processing time key
await self.redis_client.delete(processing_time_key)
logger.info(f"Marked job {job_id} as completed (success: {success})")
return True
except Exception as e:
logger.error(f"Failed to mark job {job_id} as completed: {e}")
return False
async def get_estimated_wait_time(self, position: int) -> float:
"""
Calculate estimated wait time for a job at given queue position.
Args:
position: Position in queue (0-based)
Returns:
Estimated wait time in minutes
"""
try:
avg_processing_time = await self._get_average_processing_time()
# Simple calculation: position * average processing time
# In reality, this would be more sophisticated
estimated_minutes = position * avg_processing_time
return estimated_minutes
except Exception as e:
logger.error(f"Failed to calculate estimated wait time: {e}")
return 0.0
async def cleanup_stale_processing_jobs(self, timeout_minutes: int = 60) -> int:
"""
Clean up jobs that have been processing for too long.
Args:
timeout_minutes: Timeout for processing jobs
Returns:
Number of stale jobs cleaned up
"""
try:
processing_key = "queue:processing"
processing_jobs = await self.redis_client.smembers(processing_key)
cleaned_count = 0
cutoff_time = datetime.utcnow() - timedelta(minutes=timeout_minutes)
for job_id in processing_jobs:
processing_time_key = f"queue:processing_time:{job_id}"
start_time_str = await self.redis_client.get(processing_time_key)
if start_time_str:
start_time = datetime.fromisoformat(start_time_str)
if start_time < cutoff_time:
# Job has been processing too long, clean it up
await self.redis_client.srem(processing_key, job_id)
await self.redis_client.delete(processing_time_key)
# Mark job as failed due to timeout
await self.mark_job_completed(job_id, success=False)
cleaned_count += 1
logger.warning(f"Cleaned up stale processing job {job_id}")
else:
# No start time recorded, assume stale
await self.redis_client.srem(processing_key, job_id)
cleaned_count += 1
logger.warning(f"Cleaned up processing job {job_id} with no start time")
if cleaned_count > 0:
logger.info(f"Cleaned up {cleaned_count} stale processing jobs")
return cleaned_count
except Exception as e:
logger.error(f"Failed to cleanup stale processing jobs: {e}")
return 0
async def _get_average_processing_time(self) -> float:
"""
Get average processing time from recent jobs.
Returns:
Average processing time in minutes
"""
try:
processing_times_key = "queue:processing_times"
times = await self.redis_client.lrange(processing_times_key, 0, 99) # Last 100 times
if times:
total_time = sum(float(time) for time in times)
avg_time = total_time / len(times)
return avg_time
else:
# Default estimate if no data available
return 5.0 # 5 minutes default
except Exception as e:
logger.error(f"Failed to get average processing time: {e}")
return 5.0 # Default fallback
async def perform_queue_maintenance(self) -> Dict[str, int]:
"""
Perform comprehensive queue maintenance operations.
Returns:
Dict containing maintenance operation results
"""
try:
results = {
"stale_jobs_cleaned": 0,
"orphaned_jobs_removed": 0,
"expired_keys_cleaned": 0,
"queue_optimized": False
}
# Clean up stale processing jobs
results["stale_jobs_cleaned"] = await self.cleanup_stale_processing_jobs()
# Remove orphaned jobs from queue
results["orphaned_jobs_removed"] = await self._cleanup_orphaned_queue_jobs()
# Clean up expired keys
results["expired_keys_cleaned"] = await self._cleanup_expired_keys()
# Optimize queue structure
results["queue_optimized"] = await self._optimize_queue_structure()
logger.info(f"Queue maintenance completed: {results}")
return results
except Exception as e:
logger.error(f"Failed to perform queue maintenance: {e}")
return {
"stale_jobs_cleaned": 0,
"orphaned_jobs_removed": 0,
"expired_keys_cleaned": 0,
"queue_optimized": False
}
async def _cleanup_orphaned_queue_jobs(self) -> int:
"""
Remove jobs from queue that no longer exist in Redis.
Returns:
Number of orphaned jobs removed
"""
try:
# Get all jobs in queue
queue_jobs = await self.redis_client.lrange(RedisKeyManager.JOB_QUEUE, 0, -1)
orphaned_count = 0
for job_id in queue_jobs:
# Check if job still exists
job_key = RedisKeyManager.job_key(job_id)
exists = await self.redis_client.exists(job_key)
if not exists:
# Remove orphaned job from queue
await self.redis_client.lrem(RedisKeyManager.JOB_QUEUE, 0, job_id)
orphaned_count += 1
logger.warning(f"Removed orphaned job {job_id} from queue")
return orphaned_count
except Exception as e:
logger.error(f"Failed to cleanup orphaned queue jobs: {e}")
return 0
async def _cleanup_expired_keys(self) -> int:
"""
Clean up expired processing time keys and other temporary data.
Returns:
Number of expired keys cleaned
"""
try:
cleaned_count = 0
# Clean up old processing time keys
processing_time_pattern = "queue:processing_time:*"
processing_time_keys = await self.redis_client.keys(processing_time_pattern)
cutoff_time = datetime.utcnow() - timedelta(hours=2)
for key in processing_time_keys:
try:
start_time_str = await self.redis_client.get(key)
if start_time_str:
start_time = datetime.fromisoformat(start_time_str)
if start_time < cutoff_time:
await self.redis_client.delete(key)
cleaned_count += 1
except Exception:
# If we can't parse the time, delete the key
await self.redis_client.delete(key)
cleaned_count += 1
# Clean up old daily statistics (keep last 30 days)
cutoff_date = datetime.utcnow().date() - timedelta(days=30)
# Clean up old completed/failed counters
for days_back in range(31, 365): # Clean up very old data
old_date = datetime.utcnow().date() - timedelta(days=days_back)
old_completed_key = f"queue:completed:{old_date}"
old_failed_key = f"queue:failed:{old_date}"
old_users_key = f"active_users:{old_date}"
deleted = await self.redis_client.delete(old_completed_key, old_failed_key, old_users_key)
cleaned_count += deleted
return cleaned_count
except Exception as e:
logger.error(f"Failed to cleanup expired keys: {e}")
return 0
async def _optimize_queue_structure(self) -> bool:
"""
Optimize queue structure by removing duplicates and reordering if needed.
Returns:
True if optimization was performed, False otherwise
"""
try:
# Get current queue
queue_jobs = await self.redis_client.lrange(RedisKeyManager.JOB_QUEUE, 0, -1)
if not queue_jobs:
return True
# Remove duplicates while preserving order
seen = set()
unique_jobs = []
duplicates_found = False
for job_id in queue_jobs:
if job_id not in seen:
seen.add(job_id)
unique_jobs.append(job_id)
else:
duplicates_found = True
# If duplicates were found, rebuild the queue
if duplicates_found:
# Clear current queue
await self.redis_client.delete(RedisKeyManager.JOB_QUEUE)
# Rebuild with unique jobs
if unique_jobs:
await self.redis_client.rpush(RedisKeyManager.JOB_QUEUE, *unique_jobs)
logger.info(f"Optimized queue: removed {len(queue_jobs) - len(unique_jobs)} duplicates")
return True
return False
except Exception as e:
logger.error(f"Failed to optimize queue structure: {e}")
return False
async def get_queue_health_status(self) -> Dict[str, Any]:
"""
Get comprehensive queue health status.
Returns:
Dict containing queue health information
"""
try:
# Basic queue metrics
queue_length = await self.get_queue_length()
processing_jobs = await self.redis_client.scard("queue:processing")
# Check for stale processing jobs
stale_jobs = await self._count_stale_processing_jobs()
# Check queue growth rate
queue_growth_rate = await self._calculate_queue_growth_rate()
# Determine health status
health_status = "healthy"
issues = []
if queue_length > 100:
health_status = "warning"
issues.append("High queue length")
if stale_jobs > 0:
health_status = "warning"
issues.append(f"{stale_jobs} stale processing jobs")
if queue_growth_rate > 10: # More than 10 jobs per minute
health_status = "warning"
issues.append("High queue growth rate")
if queue_length > 500:
health_status = "critical"
issues.append("Critical queue length")
return {
"status": health_status,
"queue_length": queue_length,
"processing_jobs": processing_jobs,
"stale_processing_jobs": stale_jobs,
"queue_growth_rate_per_minute": queue_growth_rate,
"issues": issues,
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
logger.error(f"Failed to get queue health status: {e}")
return {
"status": "error",
"error": str(e),
"timestamp": datetime.utcnow().isoformat()
}
async def _count_stale_processing_jobs(self, timeout_minutes: int = 60) -> int:
"""
Count jobs that have been processing for too long.
Args:
timeout_minutes: Timeout threshold in minutes
Returns:
Number of stale processing jobs
"""
try:
processing_key = "queue:processing"
processing_jobs = await self.redis_client.smembers(processing_key)
stale_count = 0
cutoff_time = datetime.utcnow() - timedelta(minutes=timeout_minutes)
for job_id in processing_jobs:
processing_time_key = f"queue:processing_time:{job_id}"
start_time_str = await self.redis_client.get(processing_time_key)
if start_time_str:
try:
start_time = datetime.fromisoformat(start_time_str)
if start_time < cutoff_time:
stale_count += 1
except ValueError:
stale_count += 1 # Invalid timestamp counts as stale
else:
stale_count += 1 # No timestamp counts as stale
return stale_count
except Exception as e:
logger.error(f"Failed to count stale processing jobs: {e}")
return 0
async def _calculate_queue_growth_rate(self) -> float:
"""
Calculate queue growth rate in jobs per minute.
Returns:
Queue growth rate
"""
try:
# This is a simplified implementation
# In production, you'd track queue length over time
# Get current queue length
current_length = await self.get_queue_length()
# Get queue length from 5 minutes ago (if tracked)
growth_key = "queue:length_history"
history = await self.redis_client.lrange(growth_key, 0, 4) # Last 5 entries
if len(history) >= 2:
try:
# Calculate average growth over the tracked period
recent_length = float(history[0])
older_length = float(history[-1])
time_diff_minutes = len(history) # Assuming 1 minute intervals
growth_rate = (recent_length - older_length) / time_diff_minutes
return max(0, growth_rate) # Don't return negative growth
except (ValueError, ZeroDivisionError):
pass
# Fallback: assume moderate growth if we can't calculate
return 0.0
except Exception as e:
logger.error(f"Failed to calculate queue growth rate: {e}")
return 0.0
async def track_queue_metrics(self) -> None:
"""
Track queue metrics over time for analysis.
This should be called periodically (e.g., every minute).
"""
try:
current_length = await self.get_queue_length()
processing_count = await self.redis_client.scard("queue:processing")
# Store queue length history
length_history_key = "queue:length_history"
await self.redis_client.lpush(length_history_key, current_length)
await self.redis_client.ltrim(length_history_key, 0, 59) # Keep last hour
# Store processing count history
processing_history_key = "queue:processing_history"
await self.redis_client.lpush(processing_history_key, processing_count)
await self.redis_client.ltrim(processing_history_key, 0, 59) # Keep last hour
# Store timestamp
timestamp_key = "queue:metrics_timestamp"
await self.redis_client.set(timestamp_key, datetime.utcnow().isoformat(), ex=3600)
except Exception as e:
logger.error(f"Failed to track queue metrics: {e}") |