From 91ec0a22a154387906d3564ec5ecb1012d12d8b2 Mon Sep 17 00:00:00 2001
From: Bastien Abadie <bastien@nextcairn.com>
Date: Mon, 4 Jun 2018 12:19:15 +0200
Subject: [PATCH] Initial Celery integration

---
 arkindex/project/__init__.py |  7 +++++++
 arkindex/project/celery.py   | 22 ++++++++++++++++++++++
 arkindex/project/settings.py |  7 ++++++-
 requirements.txt             |  4 +++-
 4 files changed, 38 insertions(+), 2 deletions(-)
 create mode 100644 arkindex/project/celery.py

diff --git a/arkindex/project/__init__.py b/arkindex/project/__init__.py
index e69de29bb2..c1a22713fb 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 0000000000..c3e8c6a6e3
--- /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 2f7eb5ea60..bf3bfb9606 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 24721985c9..e71fc5dbc4 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
-- 
GitLab