Devr.AI icon indicating copy to clipboard operation
Devr.AI copied to clipboard

refactor: update AsyncQueueManager to use a single RabbitMQ priority queue and enhance connection handling; add default queue name and priority mapping

Open vaishcodescape opened this issue 10 months ago β€’ 2 comments

Pull Request

Closes #251

πŸ“ Description

Refactors AsyncQueueManager to fix inefficient queue polling and make it production-ready. Replaces three separate queues (high_task_queue, medium_task_queue, low_task_queue) with a single RabbitMQ priority queue and push-based consumption. Workers no longer poll with queue.get() and a fixed sleep; priority is enforced by the broker via x-max-priority and per-message priority, and workers use queue.iterator() so the broker pushes messages instead of the app polling.

πŸ”§ Changes Made

  • Single priority queue: Replaced three queues with one queue (task_queue) declared with x-max-priority: 10. All messages go to this queue with a numeric priority (HIGH=10, MEDIUM=5, LOW=1).
  • Push-based workers: Workers no longer loop over queues and call queue.get(). Each worker uses async with queue.iterator(): async for message in queue_iter so the broker pushes messages; no polling or asyncio.sleep(0.1).
  • Prefetch (QoS): Set channel.set_qos(prefetch_count=1) so the broker doesn’t over-deliver; each consumer has at most one unacked message.
  • Publishing: enqueue() publishes to the single queue with aio_pika.Message(..., priority=numeric_priority) and the same routing_key. Public API unchanged: enqueue(message, priority=QueuePriority.MEDIUM, delay=0).
  • Shutdown: stop() clears worker_tasks after gather and closes channel/connection. Workers break out of the iterator when cancelled or when self.running is False.

πŸ“· Screenshots or Visual Changes (if applicable)

N/A

🀝 Collaboration

N/A

βœ… Checklist

  • [x] I have read the contributing guidelines.
  • [x] I have added tests that prove my fix is effective or that my feature works.
  • [x] I have added necessary documentation (if applicable).
  • [x] Any dependent changes have been merged and published in downstream modules.

Summary by CodeRabbit

  • Refactor
    • Unified the message queue architecture into a single priority-enabled queue system, replacing multiple per-priority queues for improved efficiency.
    • Enhanced queue processing with better type safety, more reliable message routing, and optimized worker management.
    • Improved resource utilization through centralized queue management with priority-based task assignment and allocation.

✏️ Tip: You can customize this high-level summary in your review settings.

vaishcodescape avatar Jan 29 '26 08:01 vaishcodescape

πŸ“ Walkthrough

Walkthrough

Refactored AsyncQueueManager to consolidate per-priority RabbitMQ queues into a single priority-enabled queue, replacing inefficient polling with push-based async iterator consumption, adding explicit type hints and improved logging throughout.

Changes

Cohort / File(s) Summary
Queue Architecture Modernization
backend/app/core/orchestration/queue_manager.py
Introduced single shared RabbitMQ queue with numeric priorities (DEFAULT_QUEUE_NAME, MAX_PRIORITY, PRIORITY_MAP constants). Refactored enqueue() to use single queue with x-max-priority header and numeric priority values. Rewrote _worker() from polling loop with asyncio.sleep(0.1) to push-based async iterator consumption, eliminating inefficient CPU spinning. Added _process_item() method for centralized message handling. Updated method signatures with explicit return types (None) and expanded type hints. Enhanced logging for queue operations and worker lifecycle.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #88: Modifies the same AsyncQueueManager file with RabbitMQ integration and lifecycle management changes, likely a predecessor or complementary refactoring.

Suggested reviewers

  • smokeyScraper

Poem

🐰✨ One queue to rule them all, we say, No more polling every single day! Async waits where messages call, Like carrots gathered in a sprawling hallβ€” Efficient hops, no CPU waste, Devr.AI workers move at pace! πŸš€

πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title accurately summarizes the main change: replacing multiple queues with a single RabbitMQ priority queue and enhancing connection handling.
Linked Issues check βœ… Passed The PR implementation meets all core requirements from #251: eliminates polling with sleep(0.1), uses push-based async iterator consumption, maintains priority handling via numeric priorities, and implements QoS configuration.
Out of Scope Changes check βœ… Passed All changes are directly related to the single priority queue refactoring and worker consumption model improvement. No unrelated modifications detected outside PR #257 objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • [ ] πŸ“ Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jan 29 '26 08:01 coderabbitai[bot]

@smokeyScraper This should solve the multiple Queue issue and utilise resources more properly

vaishcodescape avatar Jan 29 '26 08:01 vaishcodescape