Skip to content

Commit

Permalink
Merge 4d88ef2 into 75064be
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek committed Feb 23, 2021
2 parents 75064be + 4d88ef2 commit c22344e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 49 deletions.
9 changes: 9 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,12 @@
4.0.0
================
- Drop support for celery 3.0
- Error out if ini is misconfigured
- Add support for pyramid2
- Add support for celery v5
- Drop support for old CELERY config options,
use only the lower case versions.

3.0.0
================

Expand Down
16 changes: 7 additions & 9 deletions pyramid_celery/__init__.py
Expand Up @@ -56,21 +56,19 @@ def configure_logging(*args, **kwargs):
def setup_app(app, root, request, registry, closer, ini_location):
loader = INILoader(celery_app, ini_file=ini_location)
celery_config = loader.read_configuration()

#: TODO: There might be other variables requiring special handling
boolify(
celery_config, 'CELERY_ALWAYS_EAGER', 'CELERY_ENABLE_UTC',
'CELERY_RESULT_PERSISTENT'
celery_config,
'task_always_eager',
'enable_utc',
'result_persistent',
)

if asbool(celery_config.get('USE_CELERYCONFIG', False)) is True:
if asbool(celery_config.get('use_celeryconfig', False)) is True:
config_path = 'celeryconfig'
celery_app.config_from_object(config_path)
else:
if celery_version.major < 4:
hijack_key = 'CELERYD_HIJACK_ROOT_LOGGER'
else:
hijack_key = 'worker_hijack_root_logger'
hijack_key = 'worker_hijack_root_logger'

# TODO: Couldn't find a way with celery to do this
hijack_logger = asbool(
Expand Down Expand Up @@ -112,7 +110,7 @@ def on_preload_parsed(options, **kwargs):
env = bootstrap(ini_location, options=options)
else:
env = bootstrap(ini_location)
except:
except: # noqa
import traceback
traceback.print_exc()
exit(-1)
Expand Down
31 changes: 14 additions & 17 deletions pyramid_celery/loaders.py
Expand Up @@ -3,7 +3,7 @@
import celery.loaders.base
import celery.schedules
from celery import VERSION as celery_version
from pyramid.compat import configparser
from six.moves import configparser
from pyramid.exceptions import ConfigurationError

from functools import partial
Expand Down Expand Up @@ -99,21 +99,25 @@ def read_configuration(self, fail_silently=True):
config_dict = {}

for key, value in self.parser.items('celery'):
if celery_version.major < 4:
key = key.upper()
config_dict[key] = value

if celery_version.major < 4:
list_settings = ['CELERY_IMPORTS', 'CELERY_ACCEPT_CONTENT']
else:
list_settings = ['imports', 'accept_content']
if celery_version.major > 6:
# TODO: Check for invalid settings
# that won't be accepted by celery.
pass

list_settings = [
'imports',
'include',
'accept_content',
]

for setting in list_settings:
if setting in config_dict:
split_setting = config_dict[setting].split()
config_dict[setting] = split_setting

tuple_list_settings = ['ADMINS']
tuple_list_settings = ['admins']

for setting in tuple_list_settings:
if setting in config_dict:
Expand All @@ -133,16 +137,9 @@ def read_configuration(self, fail_silently=True):
route_config[name] = get_route_config(self.parser, section)

if beat_config:
if celery_version.major < 4:
config_dict['CELERYBEAT_SCHEDULE'] = beat_config
else:
config_dict['beat_schedule'] = beat_config

config_dict['beat_schedule'] = beat_config

if route_config:
if celery_version.major < 4:
config_dict['CELERY_ROUTES'] = route_config
else:
config_dict['task_routes'] = route_config
config_dict['task_routes'] = route_config

return config_dict
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -2,3 +2,4 @@ pyramid
celery
pytest
mock
six
16 changes: 9 additions & 7 deletions tests/configs/dev.ini
@@ -1,14 +1,16 @@
[celery]
BROKER_URL = redis://localhost:1337/0
CELERY_IMPORTS = myapp.tasks
broker_url = redis://localhost:1337/0
celery_imports = myapp.tasks
otherapp.tasks
CELERY_RESULT_BACKEND = sqlite:///celery_results.db
CELERY_RESULT_SERIALIZER = json
CELERY_ACCEPT_CONTENT =
result_backend = sqlite:///celery_results.db
result_serializer = json
accept_content =
json
xml
CELERY_TIMEZONE = America/Los_Angeles
ADMINS =
yaml

timezone = America/Los_Angeles
admins =
john,john@initrode.example
exceptions,exceptions@majortech.example

Expand Down
6 changes: 3 additions & 3 deletions tests/configs/imports.ini
@@ -1,4 +1,4 @@
[celery]
BROKER_URL = redis://localhost:1337/0
CELERY_IMPORTS = myapp.tasks
otherapp.tasks
broker_url = redis://localhost:1337/0
imports = myapp.tasks
otherapp.tasks
2 changes: 1 addition & 1 deletion tests/configs/routing.ini
Expand Up @@ -2,7 +2,7 @@
use = egg:pyramid_celery

[celery]
BROKER_URL = redis://localhost:1337/0
broker_url = redis://localhost:1337/0

[celeryroute:otherapp.tasks.Task3]
queue = slow_tasks
Expand Down
14 changes: 7 additions & 7 deletions tests/test_celery.py
Expand Up @@ -13,8 +13,8 @@ def test_includeme_custom_config():
config.registry.settings = {}
includeme(config)
config.configure_celery('tests/configs/dev.ini')
assert celery_app.conf['BROKER_URL'] == 'redis://localhost:1337/0'
assert celery_app.conf['CELERY_TIMEZONE'] == 'America/Los_Angeles'
assert celery_app.conf['broker_url'] == 'redis://localhost:1337/0'
assert celery_app.conf['timezone'] == 'America/Los_Angeles'


@pytest.mark.unit
Expand Down Expand Up @@ -44,7 +44,7 @@ def test_includeme_use_celeryconfig():
includeme(config)
config.configure_celery('tests/configs/useceleryconfig.ini')

assert celery_app.conf['BROKER_URL'] == 'redis://localhost:1337/0'
assert celery_app.conf['broker_url'] == 'redis://localhost:1337/0'


@pytest.mark.unit
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_celery_imports():
includeme(config)
config.configure_celery('tests/configs/imports.ini')

assert celery_app.conf['CELERY_IMPORTS'] == [
assert celery_app.conf['imports'] == [
'myapp.tasks',
'otherapp.tasks'
]
Expand Down Expand Up @@ -136,8 +136,8 @@ def test_celery_accept_content():

includeme(config)
config.configure_celery('tests/configs/dev.ini')

assert celery_app.conf['CELERY_ACCEPT_CONTENT'] == [
assert celery_app.conf['accept_content'] == [
'json',
'xml'
'xml',
'yaml',
]
10 changes: 5 additions & 5 deletions tests/test_loaders.py
Expand Up @@ -14,10 +14,10 @@ def test_basic_ini():
ini_path = os.path.join(here, 'tests/configs/dev.ini')
loader = INILoader(celery_app, ini_file=ini_path)
result = loader.read_configuration()
schedule = result['CELERYBEAT_SCHEDULE']
schedule = result['beat_schedule']

assert result['BROKER_URL'] == 'redis://localhost:1337/0'
assert result['ADMINS'] == [
assert result['broker_url'] == 'redis://localhost:1337/0'
assert result['admins'] == [
('john', 'john@initrode.example'),
('exceptions', 'exceptions@majortech.example')]
assert schedule['task1']['task'] == 'myapp.tasks.Task1'
Expand Down Expand Up @@ -73,8 +73,8 @@ def test_celery_routing():
ini_path = os.path.join(here, 'tests/configs/routing.ini')
loader = INILoader(celery_app, ini_file=ini_path)
result = loader.read_configuration()
routes = result['CELERY_ROUTES']
routes = result['task_routes']

assert result['BROKER_URL'] == 'redis://localhost:1337/0'
assert result['broker_url'] == 'redis://localhost:1337/0'
assert routes['myapp.tasks.Task1']['queue'] == 'fast_tasks'
assert routes['otherapp.tasks.Task3']['queue'] == 'slow_tasks'

0 comments on commit c22344e

Please sign in to comment.