Skip to content

Conversation

@vaishcodescape
Copy link

@vaishcodescape vaishcodescape commented Jan 29, 2026

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

  • I have read the contributing guidelines.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if applicable).
  • 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.

…queue and enhance connection handling; add default queue name and priority mapping
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 29, 2026

📝 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

  • Rabbit MQ Integration #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.

@vaishcodescape
Copy link
Author

vaishcodescape commented Jan 29, 2026

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/app/core/orchestration/queue_manager.py (1)

145-165: Critical: Swallowed exceptions cause failed messages to be acknowledged.

When a handler raises an exception, _process_item catches it (line 164), logs it, and returns normally. Back in _worker, execution continues to message.ack() (line 134), so the failed message is acknowledged and lost instead of being nacked.

The exception must be re-raised so the worker's exception handler can properly nack the message.

Proposed fix
     async def _process_item(self, item: Dict[str, Any], worker_name: str) -> None:
         """Process a queue item by message type."""
-        try:
-            message_data = item["data"]
-            message_type = message_data.get("type", "unknown")
+        message_data = item["data"]
+        message_type = message_data.get("type", "unknown")
 
-            handler = self.handlers.get(message_type)
+        handler = self.handlers.get(message_type)
 
-            if handler:
-                logger.debug(
-                    f"Worker {worker_name} processing {item['id']} (type: {message_type})"
-                )
-                if asyncio.iscoroutinefunction(handler):
-                    await handler(message_data)
-                else:
-                    handler(message_data)
+        if handler:
+            logger.debug(
+                f"Worker {worker_name} processing {item['id']} (type: {message_type})"
+            )
+            if asyncio.iscoroutinefunction(handler):
+                await handler(message_data)
             else:
-                logger.warning(f"No handler found for message type: {message_type}")
-
-        except Exception as e:
-            logger.error(f"Error processing item {item.get('id', 'unknown')}: {str(e)}")
+                handler(message_data)
+        else:
+            logger.warning(f"No handler found for message type: {message_type}")

By removing the try/except wrapper, any handler exception will propagate to _worker, which will then log it and call message.nack(requeue=False).

🤖 Fix all issues with AI agents
In `@backend/app/core/orchestration/queue_manager.py`:
- Around line 87-109: The enqueue method can be called when the manager is not
connected (self.channel is None), so add a guard at the start of enqueue (in
function enqueue) that checks connection state (e.g., self.channel is not None
and not closed or a self.started flag set by start/stop) and if not connected
raise a clear exception (ConnectionError or RuntimeError) and/or log an error
before returning; ensure this check is applied before calling
self.channel.default_exchange.publish and reference self.queue_name in the error
message for context.
🧹 Nitpick comments (2)
backend/app/core/orchestration/queue_manager.py (2)

57-59: Use logging.exception to capture stack trace.

When logging errors in exception handlers, logging.exception automatically includes the stack trace, which aids debugging.

Proposed fix
         except Exception as e:
-            logger.error(f"Failed to connect to RabbitMQ: {e}")
+            logger.exception(f"Failed to connect to RabbitMQ: {e}")
             raise

135-143: Use logging.exception for error handlers to capture stack traces.

Per static analysis hints, logging.exception automatically includes the traceback, which is helpful for debugging processing and worker errors.

Proposed fix
                     except asyncio.CancelledError:
                         raise
                     except Exception as e:
-                        logger.error(f"Error processing message: {e}")
+                        logger.exception(f"Error processing message: {e}")
                         await message.nack(requeue=False)
         except asyncio.CancelledError:
             logger.info(f"Worker {worker_name} cancelled")
         except Exception as e:
-            logger.error(f"Worker {worker_name} error: {e}")
+            logger.exception(f"Worker {worker_name} error: {e}")

Comment on lines +87 to 109
async def enqueue(
self,
message: Dict[str, Any],
priority: QueuePriority = QueuePriority.MEDIUM,
delay: float = 0,
) -> None:
"""Add a message to the single priority queue."""
if delay > 0:
await asyncio.sleep(delay)

queue_item = {
"id": message.get("id", f"msg_{datetime.now().timestamp()}"),
"priority": priority,
"data": message
"priority": priority.value,
"data": message,
}
json_message = json.dumps(queue_item).encode()
json_body = json.dumps(queue_item).encode()
numeric_priority = PRIORITY_MAP[priority]

await self.channel.default_exchange.publish(
aio_pika.Message(body=json_message),
routing_key=self.queues[priority]
aio_pika.Message(body=json_body, priority=numeric_priority),
routing_key=self.queue_name,
)
logger.info(f"Enqueued message {queue_item['id']} with priority {priority}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Guard against publishing when not connected.

If enqueue() is called before start() or after stop(), self.channel will be None, causing an AttributeError on self.channel.default_exchange.publish(). Add a connection check to provide a clear error.

Proposed fix
     async def enqueue(
         self,
         message: Dict[str, Any],
         priority: QueuePriority = QueuePriority.MEDIUM,
         delay: float = 0,
     ) -> None:
         """Add a message to the single priority queue."""
+        if not self.channel:
+            raise RuntimeError("Cannot enqueue: not connected. Call start() first.")
+
         if delay > 0:
             await asyncio.sleep(delay)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async def enqueue(
self,
message: Dict[str, Any],
priority: QueuePriority = QueuePriority.MEDIUM,
delay: float = 0,
) -> None:
"""Add a message to the single priority queue."""
if delay > 0:
await asyncio.sleep(delay)
queue_item = {
"id": message.get("id", f"msg_{datetime.now().timestamp()}"),
"priority": priority,
"data": message
"priority": priority.value,
"data": message,
}
json_message = json.dumps(queue_item).encode()
json_body = json.dumps(queue_item).encode()
numeric_priority = PRIORITY_MAP[priority]
await self.channel.default_exchange.publish(
aio_pika.Message(body=json_message),
routing_key=self.queues[priority]
aio_pika.Message(body=json_body, priority=numeric_priority),
routing_key=self.queue_name,
)
logger.info(f"Enqueued message {queue_item['id']} with priority {priority}")
async def enqueue(
self,
message: Dict[str, Any],
priority: QueuePriority = QueuePriority.MEDIUM,
delay: float = 0,
) -> None:
"""Add a message to the single priority queue."""
if not self.channel:
raise RuntimeError("Cannot enqueue: not connected. Call start() first.")
if delay > 0:
await asyncio.sleep(delay)
queue_item = {
"id": message.get("id", f"msg_{datetime.now().timestamp()}"),
"priority": priority.value,
"data": message,
}
json_body = json.dumps(queue_item).encode()
numeric_priority = PRIORITY_MAP[priority]
await self.channel.default_exchange.publish(
aio_pika.Message(body=json_body, priority=numeric_priority),
routing_key=self.queue_name,
)
logger.info(f"Enqueued message {queue_item['id']} with priority {priority}")
🤖 Prompt for AI Agents
In `@backend/app/core/orchestration/queue_manager.py` around lines 87 - 109, The
enqueue method can be called when the manager is not connected (self.channel is
None), so add a guard at the start of enqueue (in function enqueue) that checks
connection state (e.g., self.channel is not None and not closed or a
self.started flag set by start/stop) and if not connected raise a clear
exception (ConnectionError or RuntimeError) and/or log an error before
returning; ensure this check is applied before calling
self.channel.default_exchange.publish and reference self.queue_name in the error
message for context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENHANCEMENT:Queue Worker Inefficient Polling

1 participant