Skip to content

Commit

Permalink
[Doppins] Upgrade dependency celery to ==4.0.0 (#336)
Browse files Browse the repository at this point in the history
* Upgrade dependency celery to ==4.0.0

* Prepare project for celery 4.0.0
  • Loading branch information
eirsyl authored and warlo committed Nov 15, 2016
1 parent 254f05c commit c69f3c0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 56 deletions.
6 changes: 3 additions & 3 deletions lego/apps/search/signal_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .tasks import InstanceRemovalTask, InstanceUpdateTask
from .tasks import instance_removal, instance_update


class BaseSignalHandler:
Expand All @@ -16,7 +16,7 @@ class AsyncSignalHandler(BaseSignalHandler):
"""

def on_save(self, instance):
InstanceUpdateTask.update_instance(instance)
instance_update.delay(instance)

def on_delete(self, instance):
InstanceRemovalTask.remove_instance(instance)
instance_removal.delay(instance)
56 changes: 24 additions & 32 deletions lego/apps/search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,27 @@
from .registry import get_model_index


class InstanceUpdateTask(celery_app.Task):

@classmethod
def update_instance(cls, instance):
task = cls()
task.delay(instance)

def run(self, instance):
"""
Update a instance in the index. This function always retrieves the instance from the
database, this makes sure delayed tasks injects the newest update into the index.
"""
model_index = get_model_index(instance._meta.model)
if model_index:
instance.refresh_from_db()
model_index.update_instance(instance)


class InstanceRemovalTask(celery_app.Task):

@classmethod
def remove_instance(cls, instance):
task = cls()
task.delay(instance)

def run(self, instance):
"""
Remove a instance from the index. This is done by knowing the type and id of the object.
"""
model_index = get_model_index(instance._meta.model)
if model_index:
model_index.remove_instance(instance)
@celery_app.task(serializer='pickle')
def instance_update(instance):
"""
Update a instance in the index. This function always retrieves the instance from the
database, this makes sure delayed tasks injects the newest update into the index.
TODO: Change to json task serializer.
"""
model_index = get_model_index(instance._meta.model)
if model_index:
instance.refresh_from_db()
model_index.update_instance(instance)


@celery_app.task(serializer='pickle')
def instance_removal(instance):
"""
Remove a instance from the index. This is done by knowing the type and id of the object.
TODO: Change to json task serializer.
"""
model_index = get_model_index(instance._meta.model)
if model_index:
model_index.remove_instance(instance)
28 changes: 12 additions & 16 deletions lego/settings/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os

import celery # noqa
from django.conf import settings # noqa

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lego.settings')

Expand All @@ -25,23 +24,20 @@ def on_configure(self):

app = celery.Celery('lego')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

schedule = {}

app.conf.update(
CELERYBEAT_SCHEDULE=schedule,
CELERY_RESULT_BACKEND=None,
CELERY_TRACK_STARTED=True,
CELERY_SEND_EVENTS=True,
CELERY_TASK_SERIALIZER='pickle',
CELERY_ENABLE_UTC=True,
CELERY_DISABLE_RATE_LIMITS=True,
CELERY_IGNORE_RESULT=True,
CELERY_ACKS_LATE=False,
CELERY_PREFETCH_MULTIPLIER=2,
CELERYD_HIJACK_ROOT_LOGGER=False,
CELERY_REDIRECT_STDOUTS=False,
CELERY_ACCEPT_CONTENT=['pickle', 'json']
beat_schedule=schedule,
result_backend=None,
task_track_started=True,
task_serializer='pickle',
worker_disable_rate_limits=True,
task_ignore_result=True,
task_acks_late=False,
worker_hijack_root_logger=False,
worker_redirect_stdouts=False,
accept_content=['pickle', 'json']
)
2 changes: 1 addition & 1 deletion lego/settings/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'] += ['rest_framework.renderers.BrowsableAPIRenderer']

BROKER_URL = 'redis://127.0.0.1'
CELERY_BROKER_URL = 'redis://127.0.0.1'

STREAM_REDIS_CONFIG = {
'default': {
Expand Down
2 changes: 1 addition & 1 deletion lego/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
] + MIDDLEWARE_CLASSES

# Celery
BROKER_URL = env('CELERY_BROKER_URL')
CELERY_BROKER_URL = env('CELERY_BROKER_URL')

# Stream Framework
STREAM_REDIS_CONFIG = {
Expand Down
6 changes: 5 additions & 1 deletion lego/settings/secure.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_SSL_REDIRECT = True
X_FRAME_OPTIONS = 'DENY'

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
CSRF_COOKIE_SECURE = True

CSRF_COOKIE_HTTPONLY = True
CSRF_HEADER_NAME = 'HTTP_X_CSRFTOKEN'

SECURE_HSTS_SECONDS = 3600
SECURE_HSTS_INCLUDE_SUBDOMAINS = False
2 changes: 1 addition & 1 deletion lego/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
{'host': 'localhost'},
]

CELERY_ALWAYS_EAGER = True
CELERY_TASK_ALWAYS_EAGER = True
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ stream-framework==1.4.0
git+https://github.com/django-statsd/django-statsd.git#67c8077
certifi==2016.9.26
elasticsearch==2.4.0
celery==3.1.24
asgi_redis==0.14.1
channels==0.17.2
celery==4.0.0

djangorestframework==3.5.3
djangorestframework-jwt==1.8.0
Expand Down

0 comments on commit c69f3c0

Please sign in to comment.