From 97ea3c89a5126e2782b12738a4a776b5b507bf0c Mon Sep 17 00:00:00 2001 From: tugofpeas Date: Sat, 8 Aug 2026 20:20:51 -0500 Subject: [PATCH] Postpone SECRET_KEY evaluation to the qcluster command execution (#332) * Suppress ImproperlyConfigured within Conf, allowing the exception to be raised specifically during the 'qcluster' command instead. This ensures build scripts and workflows can freely execute other Django commands (e.g., 'python manage.py collectstatic') without failing due to a missing/empty secret key. * Closes #279 Co-authored-by: Tug of Peas <19413623+tugofpeas@users.noreply.github.com> --- django_q/conf.py | 8 +++++++- django_q/management/commands/qcluster.py | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django_q/conf.py b/django_q/conf.py index e240294..cff4aea 100644 --- a/django_q/conf.py +++ b/django_q/conf.py @@ -7,6 +7,7 @@ from multiprocessing import cpu_count from warnings import warn from django.conf import settings +from django.core.exceptions import ImproperlyConfigured from django.utils.translation import gettext_lazy as _ from django_q.queues import Queue @@ -211,7 +212,12 @@ class Conf: # Use the secret key for package signing # Django itself should raise an error if it's not configured - SECRET_KEY = settings.SECRET_KEY + # but suppress the exception early to allow other parts of the app to still run. + # For example any "python manage.py ..." command still runs even if SECRET_KEY is not set. + try: + SECRET_KEY = settings.SECRET_KEY + except ImproperlyConfigured: + SECRET_KEY = None # The redis stats key Q_STAT = f"django_q:{PREFIX}:cluster" diff --git a/django_q/management/commands/qcluster.py b/django_q/management/commands/qcluster.py index bfedcca..abb6649 100644 --- a/django_q/management/commands/qcluster.py +++ b/django_q/management/commands/qcluster.py @@ -1,5 +1,6 @@ import os +from django.conf import settings from django.core.management.base import BaseCommand from django.utils.translation import gettext as _ @@ -28,6 +29,8 @@ class Command(BaseCommand): ) def handle(self, *args, **options): + # Ensure that the cluster starts only if the SECRET_KEY is set, as it is required for signing. + settings.SECRET_KEY # Set alternative cluster_name before creating the cluster (cluster_name is broker's queue_name, too) cluster_name = options.get("cluster_name") if cluster_name: