Skip to content

Commit

Permalink
Add support for multiple celery queues
Browse files Browse the repository at this point in the history
Close #163
  • Loading branch information
Marcin Gorny authored and codingjoe committed Jan 16, 2018
1 parent 106feb8 commit 8a86a1b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ The following people have contributed to django-health-check:
- Johannes Hoppe
- Aleksi Häkli
- Mark Speedy
- Marcin Górny
8 changes: 7 additions & 1 deletion health_check/contrib/celery/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from celery import current_app
from django.apps import AppConfig

from health_check.plugins import plugin_dir
Expand All @@ -8,4 +9,9 @@ class HealthCheckConfig(AppConfig):

def ready(self):
from .backends import CeleryHealthCheck
plugin_dir.register(CeleryHealthCheck)

for queue in current_app.amqp.queues:
celery_class_name = 'CeleryHealthCheck' + queue.title()

celery_class = type(celery_class_name, (CeleryHealthCheck,), {'queue': queue})
plugin_dir.register(celery_class)
3 changes: 2 additions & 1 deletion health_check/contrib/celery/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def check_status(self):
try:
result = add.apply_async(
args=[4, 4],
expires=timeout
expires=timeout,
queue=self.queue
)
result.get(timeout=timeout)
if result.result != 8:
Expand Down
13 changes: 11 additions & 2 deletions tests/test_autodiscover.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from celery import current_app
from django.conf import settings

from health_check.contrib.celery.backends import CeleryHealthCheck
from health_check.plugins import plugin_dir


class TestAutoDiscover:
def test_autodiscover(self):
health_check_plugins = list(filter(
lambda x: 'health_check.' in x,
lambda x: 'health_check.' in x and 'celery' not in x,
settings.INSTALLED_APPS
))

assert len(plugin_dir._registry) == len(health_check_plugins)
non_celery_plugins = [x for x in plugin_dir._registry if not issubclass(x[0], CeleryHealthCheck)]

# The number of installed apps excluding celery should equal to all plugins except celery
assert len(non_celery_plugins) == len(health_check_plugins)

def test_discover_celery_queues(self):
celery_plugins = [x for x in plugin_dir._registry if issubclass(x[0], CeleryHealthCheck)]
assert len(celery_plugins) == len(current_app.amqp.queues)
3 changes: 3 additions & 0 deletions tests/testapp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .celery import app

__all__ = ['app']
4 changes: 4 additions & 0 deletions tests/testapp/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from celery import Celery

app = Celery('testapp')
app.config_from_object('django.conf:settings', namespace='CELERY')
5 changes: 5 additions & 0 deletions tests/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@
SECRET_KEY = uuid.uuid4().hex

USE_L10N = True

CELERY_TASK_QUEUES = {
'default': {},
'queue2': {}
}

0 comments on commit 8a86a1b

Please sign in to comment.