layerindex/tasks.py: fix loading the settings in the Celery worker
PATCH 1/1 layerindex/tasks.py: fix loading the settings in the Celery worker
Since "Update Django settings import to use the correct form" (dc63275), the worker fails to start unless DJANGO_SETTINGS_MODULE is
set: importing django.conf never raises ImportError, so utils.setup_django() is no longer called, and reading the broker setting fails. Set DJANGO_SETTINGS_MODULE as setup_django() does.
AI-Generated: Claude Opus 5.5
Signed-off-by Tim Orling <[email protected]>
---
Found while adding RabbitMQ and a Celery worker to a test instance: the worker exited with ImproperlyConfigured on startup. The Docker setups don't set DJANGO_SETTINGS_MODULE, so a deployment of current master has no working worker (no Update Layer button, no email).
layerindex/tasks.py | 12 +++++------- tests/test_celery_app.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 tests/test_celery_app.py
layerindex/tasks.py +5 -7
@@ -16,13 +16,11 @@ import subprocess import shlex from datetime import datetime -try: - from django.conf import settings -except ImportError: - # not in a full django env, so settings is inaccessible. - # setup django to access settings. - utils.setup_django() - from django.conf import settings +# The Celery worker imports this module outside Django; point it at the +# settings, as utils.setup_django() does. (Importing django.conf always +# succeeds; the settings are only loaded when first used.) +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") +from django.conf import settings tasks = Celery('layerindex', broker=settings.RABBIT_BROKER,
tests/test_celery_app.py new file +19 -0
@@ -0,0 +1,19 @@ +# layerindex-web - test that the Celery worker can load the task module +# +# Licensed under the MIT license, see COPYING.MIT for details +# +# SPDX-License-Identifier: MIT + +import os +import subprocess +import sys + + +def test_tasks_module_loads_without_django_settings_module(): + # As the worker does: celery --workdir=<source> --app layerindex.tasks + env = {k: v for k, v in os.environ.items() if k != 'DJANGO_SETTINGS_MODULE'} + srcdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + result = subprocess.run([sys.executable, '-c', 'import layerindex.tasks as t; print(t.tasks.conf.broker_url)'], + cwd=srcdir, env=env, capture_output=True, text=True) + assert result.returncode == 0, result.stderr + assert result.stdout.startswith('amqp://')
base-commit: 08e58cdece30019a6f9e5d14af16ebe20afa6177
Signature · 1 line
2.56.0