Files
tugofpeasandTug of Peas 97ea3c89a5 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>
2026-08-09 03:20:51 +02:00

43 lines
1.4 KiB
Python

import os
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.translation import gettext as _
from django_q.cluster import Cluster
class Command(BaseCommand):
# Translators: help text for qcluster management command
help = _("Starts a Django Q Cluster.")
def add_arguments(self, parser):
parser.add_argument(
"--run-once",
action="store_true",
dest="run_once",
default=False,
help="Run once and then stop.",
)
parser.add_argument(
"-n",
"--name",
dest="cluster_name",
default=None,
help="Set alternative cluster name instead of the name in Q_CLUSTER settings (for multi-queue setup). "
"On Linux you should set name through `Q_CLUSTER_NAME=cluster_name python manage.py qcluster` instead.",
)
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:
os.environ["Q_CLUSTER_NAME"] = cluster_name
q = Cluster()
q.start()
if options.get("run_once", False):
q.stop()