refactor: update AsyncQueueManager to use a single RabbitMQ priority queue and enhance connection handling; add default queue name and priority mapping
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 withx-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 usesasync with queue.iterator(): async for message in queue_iterso the broker pushes messages; no polling orasyncio.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 withaio_pika.Message(..., priority=numeric_priority)and the samerouting_key. Public API unchanged:enqueue(message, priority=QueuePriority.MEDIUM, delay=0). - Shutdown:
stop()clearsworker_tasksaftergatherand closes channel/connection. Workers break out of the iterator when cancelled or whenself.runningis 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.
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. π― 3 (Moderate) | β±οΈ ~20 minutes π°β¨ 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! π βοΈ Tip: You can configure your own custom pre-merge checks in the settings.π Walkthrough
Walkthrough
Changes
Cohort / File(s)
Summary
Queue Architecture Modernization
backend/app/core/orchestration/queue_manager.pyIntroduced 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
Possibly related PRs
Suggested reviewers
Poem
π₯ 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.β¨ Finishing touches
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
@smokeyScraper This should solve the multiple Queue issue and utilise resources more properly