Skip to content

Commit

Permalink
Respect celery configuration namespace (#351)
Browse files Browse the repository at this point in the history
When configuring Celery with RabbitMQ in Django, one often uses `app.config_from_object('django.conf:settings', namespace='CELERY')` as per the Celery Quick Start (https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html).
This requires that all configuration keys in the Django settings have to be prefixed with `CELERY_`.

The previous implementation only used `settings.BROKER_URL` (which may be `None`). When passing `None` to Celery (or more specifically: the `Connection` object), it would default to `localhost`.
Expand the `RabbitMQHealthCheck` to accept a `namespace` attribute which can be set to `CELERY` for example.

Co-authored-by: Johann Schmitz <[email protected]>
  • Loading branch information
ercpe and Johann Schmitz committed Mar 29, 2023
1 parent 4c5f866 commit 22883c4
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion health_check/contrib/rabbitmq/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
class RabbitMQHealthCheck(BaseHealthCheckBackend):
"""Health check for RabbitMQ."""

namespace = None

def check_status(self):
"""Check RabbitMQ service by opening and closing a broker channel."""
logger.debug("Checking for a broker_url on django settings...")

broker_url = getattr(settings, "BROKER_URL", None)
broker_url_setting_key = f"{self.namespace}_BROKER_URL" if self.namespace else "BROKER_URL"
broker_url = getattr(settings, broker_url_setting_key, None)

logger.debug("Got %s as the broker_url. Connecting to rabbit...", broker_url)

Expand Down

1 comment on commit 22883c4

@hadpro24
Copy link

Choose a reason for hiding this comment

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

How Can I defined it, I got this error : unavailable: Unable to connect to RabbitMQ: Connection was refused.

In my settings :

CELERY_BROKER_URL = env("CELERY_BROKER_URL")

Celery.py file config :

app = Celery('config')
app.config_from_object('django.conf:settings', namespace='CELERY')

Please sign in to comment.