Skip to content

Commit 3f55f96

Browse files
committed
BK-2085 Update celery, remove djcelery
1 parent d3cfa17 commit 3f55f96

10 files changed

Lines changed: 92 additions & 33 deletions

File tree

docs/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
4040

41+
'django_celery_results',
4142
'compressor',
42-
'djcelery',
4343

4444
# list of booki apps
4545
'booki.editor',

lib/booktype/apps/convert/tasks.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from uuid import uuid4
2323
from celery import group
24+
from celery.result import allow_join_result
2425

2526
from booktype.convert import loader
2627
from booktype.convert.runner import run_conversion
@@ -93,6 +94,8 @@ def convert(request_data, base_path):
9394
}
9495
"""
9596

97+
# TODO we should use a chain of tasks
98+
9699
assets = AssetCollection(base_path)
97100

98101
assets.add_urls(request_data.assets)
@@ -118,9 +121,13 @@ def convert(request_data, base_path):
118121
)
119122
subtasks.append(subtask)
120123

121-
job = group(subtasks)
124+
job = group(subtasks, disable_sync_subtasks=False)
122125
result = job.apply_async()
123-
result.join(propagate=False)
126+
127+
# TODO we should use chain here
128+
# http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks
129+
with allow_join_result():
130+
result.join(propagate=False)
124131

125132
subtasks_info = {async.task_id: async for async in result.children}
126133
celery.current_task.update_state(state="PROGRESS", meta=subtasks_info)

lib/booktype/skeleton/base_settings.py.original

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import os
33
from unipath import Path
44

5-
import djcelery
6-
djcelery.setup_loader()
7-
85
# DJANGO ADMIN
96
ADMINS = (
107
# ('Your Name', 'your_email@domain.com'),
@@ -264,8 +261,8 @@ INSTALLED_APPS = (
264261
'django.contrib.messages',
265262
'django.contrib.staticfiles',
266263

264+
'django_celery_results',
267265
'compressor',
268-
'djcelery',
269266
'rest_framework',
270267
'rest_framework.authtoken',
271268
'rest_framework_swagger',
@@ -327,8 +324,11 @@ REST_FRAMEWORK = {
327324
}
328325

329326
# CELERY
330-
BROKER_URL = os.environ.get('BOOKTYPE_BROKER_URL', 'amqp://guest:guest@localhost:5672/')
327+
CELERY_BROKER_URL = os.environ.get('BOOKTYPE_BROKER_URL', 'amqp://guest:guest@localhost:5672/')
331328
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
329+
CELERY_TASK_SERIALIZER = 'pickle'
330+
CELERY_RESULT_SERIALIZER = 'pickle'
331+
CELERY_RESULT_BACKEND = 'django-db'
332332

333333
# set of default roles and corresponding permissions
334334
BOOKTYPE_DEFAULT_ROLES = {

lib/booktype/skeleton/prod_settings.py.original

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ REDIS_PASSWORD = os.environ.get('BOOKTYPE_REDIS_PASSWORD')
4141
REDIS_DB = os.environ.get('BOOKTYPE_REDIS_DB', 0)
4242

4343
# CELERY
44-
BROKER_URL = os.environ.get('BOOKTYPE_BROKER_URL', 'amqp://guest:guest@localhost:5672/')
44+
CELERY_BROKER_URL = os.environ.get('BOOKTYPE_BROKER_URL', 'amqp://guest:guest@localhost:5672/')
4545

4646
# DATABASE
4747
DATABASES = {

lib/booktype/skeleton/stage_settings.py.original

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ REDIS_PASSWORD = os.environ.get('BOOKTYPE_REDIS_PASSWORD')
4141
REDIS_DB = os.environ.get('BOOKTYPE_REDIS_DB', 0)
4242

4343
# CELERY
44-
BROKER_URL = os.environ.get('BOOKTYPE_BROKER_URL', 'amqp://guest:guest@localhost:5672/')
44+
CELERY_BROKER_URL = os.environ.get('BOOKTYPE_BROKER_URL', 'amqp://guest:guest@localhost:5672/')
4545

4646
# DATABASE
4747
DATABASES = {

requirements/_base.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Django==1.11
22
django-compressor==2.0
3-
celery>=3.1.18,<3.2
4-
django-celery==3.1.17
3+
celery==4.1.1
4+
django-celery-results==1.0.1
55
Unidecode
66
lxml
77
Pillow==2.9.0

scripts/createbooktype

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import sys
2121
import os
2222
import argparse
2323
import string
24-
import unipath
2524
import random
2625

2726

@@ -193,17 +192,6 @@ def check_redis_available():
193192
logln("[OK]")
194193

195194

196-
def check_south_available():
197-
log("+ Trying to import South module. ")
198-
199-
try:
200-
import south
201-
except ImportError:
202-
raise InstallError()
203-
else:
204-
logln("[OK]")
205-
206-
207195
def check_unidecode_available():
208196
log("+ Trying to import Unidecode module. ")
209197

@@ -588,10 +576,6 @@ sys.path.insert(0, '%(booki_path)s/')
588576
589577
os.environ["DJANGO_SETTINGS_MODULE"] = "%(project_name)s_site.settings.%(profile)s"
590578
591-
# Initialise celery
592-
import djcelery
593-
djcelery.setup_loader()
594-
595579
# This application object is used by any WSGI server configured to use this
596580
# file. This includes Django's development server, if the WSGI_APPLICATION
597581
# setting points here.
@@ -609,6 +593,71 @@ application = get_wsgi_application()
609593
else:
610594
logln("[OK]")
611595

596+
def include_celery(destination):
597+
log("+ Import celery in {}_site __init__ file".format(get_project_name(destination)))
598+
try:
599+
f = open('%s/%s_site/__init__.py' % (destination, get_project_name(destination)), 'wt')
600+
f.write('''
601+
from __future__ import absolute_import, unicode_literals
602+
603+
# TODO we should have ability to change profile mode for celery
604+
# in general we need ENV-variables driven settings, but it will require bigger changes
605+
from .celery import app as celery_app
606+
607+
__all__ = ['celery_app']
608+
609+
''')
610+
f.close()
611+
except OSError:
612+
raise InstallError()
613+
else:
614+
logln("[OK]")
615+
616+
617+
def create_celery(destination):
618+
log("+ Creating celery.py file. ".format(profile))
619+
620+
import booki
621+
booki_path = os.path.abspath(os.path.dirname(booki.__file__) + '/..')
622+
623+
try:
624+
f = open('%s/%s_site/celery.py' % (destination, get_project_name(destination)), 'wt')
625+
f.write('''
626+
from __future__ import absolute_import, unicode_literals
627+
628+
import os
629+
import sys
630+
from unipath import Path
631+
from celery import Celery
632+
633+
BASE_DIR = Path(os.path.abspath(__file__)).ancestor(2)
634+
635+
sys.path.insert(0, BASE_DIR)
636+
sys.path.insert(0, BASE_DIR.child("lib"))
637+
sys.path.insert(0, '{booki_path}')
638+
639+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{project_name}_site.settings.dev')
640+
641+
app = Celery('celery_booktype')
642+
643+
# Using a string here means the worker doesn't have to serialize
644+
# the configuration object to child processes.
645+
# - namespace='CELERY' means all celery-related configuration keys
646+
# should have a `CELERY_` prefix.
647+
app.config_from_object('django.conf:settings', namespace='CELERY')
648+
649+
# Load task modules from all registered Django app configs.
650+
app.autodiscover_tasks()
651+
'''.format(booki_path=booki_path, project_name=get_project_name(destination)))
652+
f.close()
653+
except OSError:
654+
raise InstallError()
655+
else:
656+
logln("[OK]")
657+
658+
659+
660+
612661

613662
def create_supervisor(destination, profile, virtual_env):
614663
log("+ Creating supervisor_{}.conf file.".format(profile))
@@ -902,7 +951,6 @@ if __name__ == '__main__':
902951
check_lxml_available()
903952
check_pil_available()
904953
check_redis_available()
905-
# check_south_available()
906954
check_unidecode_available()
907955

908956
# show info about user id, group id and etc...
@@ -954,10 +1002,14 @@ if __name__ == '__main__':
9541002
# create urls
9551003
create_urls(project_destination)
9561004

957-
# create wsgi file
1005+
# create wsgi files
9581006
for profile in PROFILES:
9591007
create_wsgi(project_destination, profile, args.virtual_env)
9601008

1009+
# create celery files
1010+
include_celery(project_destination)
1011+
create_celery(project_destination)
1012+
9611013
# create supervisor file for wsgi (stage and prod only)
9621014
for profile in PROFILES[1:]:
9631015
create_supervisor(project_destination, profile, args.virtual_env)

tests/func_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
'django.contrib.messages',
102102
'django.contrib.staticfiles',
103103

104-
'djcelery',
104+
'django_celery_results',
105105
'compressor',
106106

107107
# list of booki apps

tests/selenium_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
'django.contrib.staticfiles',
140140

141141
'compressor',
142-
'djcelery',
142+
'django_celery_results',
143143

144144
# list of booki apps
145145
'booki.editor',

tests/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
'django.contrib.messages',
9494
'django.contrib.staticfiles',
9595

96-
'djcelery',
96+
'django_celery_results',
9797

9898
# list of booki apps
9999
'booki.editor',

0 commit comments

Comments
 (0)