LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
[FEAT] Add configurable RabbitMQ HA mode with quorum queues (#1840)
Rahul Johny09d852c9d725
diff --git a/backend/backend/celery_config.py b/backend/backend/celery_config.pyindex f8833556e..0d9974055 100644--- a/backend/backend/celery_config.py+++ b/backend/backend/celery_config.py@@ -1,6 +1,36 @@+import osfrom urllib.parse import quote_plusfrom django.conf import settings+from kombu import Queue+++def is_rabbitmq_ha_enabled() -> bool:+ """Check if RabbitMQ HA mode with quorum queues is enabled."""+ return os.environ.get("RABBITMQ_HA_ENABLED", "").lower() == "true"+++def make_queue(name: str) -> Queue:+ """Create a kombu Queue with x-queue-type: quorum.++ Always declares the queue as quorum type. Callers are responsible+ for only invoking this helper when HA mode is enabled (i.e. inside+ an ``if is_rabbitmq_ha_enabled():`` guard).++ This is necessary because Celery/kombu sends x-queue-type: classic+ by default, overriding the server-side default_queue_type setting.++ Internal Celery queues (pidbox, reply queues) are not affected+ since they are managed by Celery internals and not listed here.++ Args:+ name: Queue name.++ Returns:+ kombu Queue instance with quorum queue arguments.+ """+ kwargs: dict = {"name": name, "queue_arguments": {"x-queue-type": "quorum"}}+ return Queue(**kwargs)class CeleryConfig:@@ -31,3 +61,27 @@ class CeleryConfig:beat_scheduler = "django_celery_beat.schedulers:DatabaseScheduler"task_acks_late = True+++# When HA is enabled, declare all backend-managed queues as quorum type+# and override QoS semantics for quorum queue compatibility.+# This block runs at import time (same pattern as LLM Whisperer's celeryconfig.py).+if is_rabbitmq_ha_enabled():+ # All queues consumed by the main backend Celery app+ # (worker, worker-logging, worker-metrics)+ _BACKEND_QUEUES = [+ "celery",+ "celery_api_deployments",+ "celery_periodic_logs",+ "celery_log_task_queue",+ "dashboard_metric_events",+ ]+ CeleryConfig.task_queues = [make_queue(q) for q in _BACKEND_QUEUES]++ # Quorum queues do not support global (channel-level) QoS.+ # Celery detects RabbitMQ >= 3.3 and sends basic.qos with global=True,+ # which quorum queues reject with NOT_IMPLEMENTED.+ # Override to per-consumer prefetch.+ from kombu.transport import pyamqp++ pyamqp.Transport.qos_semantics_matches_spec = lambda self, conn: Truediff --git a/workers/shared/infrastructure/config/builder.py b/workers/shared/infrastructure/config/builder.pyindex 5f04df18c..2d9491177 100644--- a/workers/shared/infrastructure/config/builder.py+++ b/workers/shared/infrastructure/config/builder.py@@ -96,6 +96,11 @@ class WorkerBuilder:# Apply configuration to Celery appapp.conf.update(celery_config)+ # Apply RabbitMQ HA quorum-queue declarations (after overrides).+ from .rabbitmq_ha import apply_rabbitmq_ha++ apply_rabbitmq_ha(app, worker_celery_config)+logger.info(f"Built {worker_type} worker with queues: "f"{worker_celery_config.queue_config.all_queues()}"diff --git a/workers/shared/infrastructure/config/rabbitmq_ha.py b/workers/shared/infrastructure/config/rabbitmq_ha.pynew file mode 100644index 000000000..15274977b--- /dev/null+++ b/workers/shared/infrastructure/config/rabbitmq_ha.py@@ -0,0 +1,39 @@+"""RabbitMQ HA (quorum queue) configuration for Celery apps.++Centralises the quorum-queue and QoS fixup so that every WorkerBuilder+variant applies the same logic.+"""++import os+from typing import TYPE_CHECKING++if TYPE_CHECKING:+ from celery import Celery++ from ...models.worker_models import WorkerCeleryConfig+++def apply_rabbitmq_ha(app: "Celery", worker_celery_config: "WorkerCeleryConfig") -> None:+ """Declare quorum queues and patch QoS when RabbitMQ HA is enabled.++ Must be called *after* all config overrides have been merged into+ ``app.conf`` so that HA declarations always take precedence.++ Does nothing when the ``RABBITMQ_HA_ENABLED`` env var is not ``true``.+ """+ if os.environ.get("RABBITMQ_HA_ENABLED", "").lower() != "true":+ return++ from kombu import Queue+ from kombu.transport import pyamqp++ quorum_args = {"x-queue-type": "quorum"}+ app.conf.task_queues = [+ Queue(q, queue_arguments=quorum_args)+ for q in sorted(worker_celery_config.queue_config.all_queues())+ ]++ # Quorum queues don't support global (channel-level) QoS. Celery+ # detects RabbitMQ >= 3.3 and sends basic.qos with global=True,+ # which quorum queues reject with NOT_IMPLEMENTED.+ pyamqp.Transport.qos_semantics_matches_spec = lambda self, conn: Truediff --git a/workers/shared/worker_builder.py b/workers/shared/worker_builder.pyindex f229dec57..b91c715c4 100644--- a/workers/shared/worker_builder.py+++ b/workers/shared/worker_builder.py@@ -72,6 +72,11 @@ class WorkerBuilder:# Apply configuration to Celery appapp.conf.update(celery_config)+ # Apply RabbitMQ HA quorum-queue declarations (after overrides).+ from shared.infrastructure.config.rabbitmq_ha import apply_rabbitmq_ha++ apply_rabbitmq_ha(app, worker_celery_config)+logger.info(f"Built {worker_type} worker with queues: "f"{worker_celery_config.queue_config.all_queues()}"