diff --git a/arkindex/project/__init__.py b/arkindex/project/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c1a22713fb2a7698d7e665f0da9f8e0380f57423 100644 --- a/arkindex/project/__init__.py +++ b/arkindex/project/__init__.py @@ -0,0 +1,7 @@ +from __future__ import absolute_import, unicode_literals + +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from arkindex.project.celery import app as celery_app + +__all__ = ['celery_app'] diff --git a/arkindex/project/celery.py b/arkindex/project/celery.py new file mode 100644 index 0000000000000000000000000000000000000000..c3e8c6a6e375697aefd69934dc7427d1993833a9 --- /dev/null +++ b/arkindex/project/celery.py @@ -0,0 +1,22 @@ +from __future__ import absolute_import, unicode_literals +import os +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'arkindex.project.settings') + +app = Celery('arkindex') + +# Using a string here means the worker doesn't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object('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/arkindex/project/settings.py b/arkindex/project/settings.py index 2f7eb5ea601851926530c1832628d13df758b1ce..bf3bfb9606340c55236a370775d67058550f3275 100644 --- a/arkindex/project/settings.py +++ b/arkindex/project/settings.py @@ -26,7 +26,8 @@ if not os.path.isdir(LOGS_DIR): SECRET_KEY = 'jf0w^y&ml(caax8f&a1mub)(js9(l5mhbbhosz3gi+m01ex+lo' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +ARKINDEX_ENV = os.environ.get('ARKINDEX_ENV', 'dev') +DEBUG = ARKINDEX_ENV == 'dev' hosts = os.environ.get('ALLOWED_HOSTS') ALLOWED_HOSTS = hosts and hosts.split(',') or [] @@ -256,6 +257,10 @@ LOGGING = { }, } +# Async Workers +CELERY_BROKER_URL = os.environ.get('QUEUE_URL', 'redis://localhost:6379/0') +CELERY_RESULT_BACKEND = 'redis' + # Local settings try: from .local_settings import * # noqa diff --git a/requirements.txt b/requirements.txt index 24721985c9a9367fa9fcdbd0746969ca3736f10f..e71fc5dbc4378a80e88fbcd9692db4ed28e1c4ff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,10 @@ +celery==4.1.1 certifi==2017.7.27.1 chardet==3.0.4 Django==2.0 django-enumfields==0.9.0 -django-webpack-loader==0.5.0 djangorestframework==3.7.1 +django-webpack-loader==0.5.0 elasticsearch==6.2.0 et-xmlfile==1.0.1 idna==2.6 @@ -15,6 +16,7 @@ Pillow==4.3.0 psycopg2==2.7.3.2 python-memcached==1.59 pytz==2017.2 +redis==2.10.6 requests==2.18.4 roman==2.0.0 urllib3==1.22