From 663c05280d4c18a340fbb98e7adb80c15a8eb5ef Mon Sep 17 00:00:00 2001 From: Brian Helba Date: Fri, 17 Jul 2020 10:59:47 -0400 Subject: [PATCH] Clean up project code entry points --- dandi/__init__.py | 8 ++++++-- dandi/asgi.py | 12 ++++++++++++ dandi/celery.py | 8 ++------ dandi/wsgi.py | 1 + manage.py | 11 ++--------- 5 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 dandi/asgi.py diff --git a/dandi/__init__.py b/dandi/__init__.py index 2f3a71abf..d08b682c2 100644 --- a/dandi/__init__.py +++ b/dandi/__init__.py @@ -1,3 +1,7 @@ -from dandi.celery import app as celery_app +# This project module is imported for us when Django starts. To ensure that Celery app is always +# defined prior to any shared_task definitions (so those tasks will bind to the app), import +# the Celery module here for side effects. +from .celery import app as _celery_app # noqa: F401 -__all__ = ('celery_app',) +# Do not import anything else from this file, to avoid interfering with the startup order of the +# Celery app and Django's settings. diff --git a/dandi/asgi.py b/dandi/asgi.py new file mode 100644 index 000000000..3e54aaec4 --- /dev/null +++ b/dandi/asgi.py @@ -0,0 +1,12 @@ +import os + +import configurations.importer +from django.core.asgi import get_asgi_application + + +os.environ['DJANGO_SETTINGS_MODULE'] = 'dandi.settings' +if not os.environ.get('DJANGO_CONFIGURATION'): + raise ValueError('The environment variable "DJANGO_CONFIGURATION" must be set.') +configurations.importer.install() + +application = get_asgi_application() diff --git a/dandi/celery.py b/dandi/celery.py index 9233a978c..3d2b48cee 100644 --- a/dandi/celery.py +++ b/dandi/celery.py @@ -3,6 +3,7 @@ from celery import Celery import configurations.importer + os.environ['DJANGO_SETTINGS_MODULE'] = 'dandi.settings' if not os.environ.get('DJANGO_CONFIGURATION'): raise ValueError('The environment variable "DJANGO_CONFIGURATION" must be set.') @@ -10,12 +11,7 @@ # Using a string config_source means the worker doesn't have to serialize # the configuration object to child processes. -app = Celery('dandi', config_source='django.conf:settings', namespace='CELERY') +app = Celery(config_source='django.conf:settings', namespace='CELERY') # Load task modules from all registered Django app configs. app.autodiscover_tasks() - - -@app.task(bind=True) -def debug_task(self): - print('Request: {0!r}'.format(self.request)) diff --git a/dandi/wsgi.py b/dandi/wsgi.py index cb13c0909..1ee852b29 100644 --- a/dandi/wsgi.py +++ b/dandi/wsgi.py @@ -3,6 +3,7 @@ import configurations.importer from django.core.wsgi import get_wsgi_application + os.environ['DJANGO_SETTINGS_MODULE'] = 'dandi.settings' if not os.environ.get('DJANGO_CONFIGURATION'): raise ValueError('The environment variable "DJANGO_CONFIGURATION" must be set.') diff --git a/manage.py b/manage.py index f0b73c7a9..dfe6a78e2 100755 --- a/manage.py +++ b/manage.py @@ -3,23 +3,16 @@ import sys import configurations.importer +from django.core.management import execute_from_command_line -def main(): +def main() -> None: os.environ['DJANGO_SETTINGS_MODULE'] = 'dandi.settings' # Production usage runs manage.py for tasks like collectstatic, # so DJANGO_CONFIGURATION should always be explicitly set in production os.environ.setdefault('DJANGO_CONFIGURATION', 'DevelopmentConfiguration') configurations.importer.install(check_options=True) - try: - from django.core.management import execute_from_command_line - except ImportError as exc: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - 'available on your PYTHONPATH environment variable? Did you ' - 'forget to activate a virtual environment?' - ) from exc execute_from_command_line(sys.argv)