Skip to content

Commit

Permalink
Clean up project code entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba committed Jul 17, 2020
1 parent c3e40b6 commit 663c052
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
8 changes: 6 additions & 2 deletions dandi/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions dandi/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 2 additions & 6 deletions dandi/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
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.')
configurations.importer.install()

# 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))
1 change: 1 addition & 0 deletions dandi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
11 changes: 2 additions & 9 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 663c052

Please sign in to comment.