From 13ce23c6fb6382cd96818e224ca3ffa2bfd7ecc4 Mon Sep 17 00:00:00 2001 From: Pedro Peixoto Date: Sun, 28 Feb 2021 17:41:29 -0300 Subject: [PATCH] Support custom celeryconfig modules --- README.rst | 3 +++ pyramid_celery/__init__.py | 2 +- setup.py | 1 - tests/configs/custom_useceleryconfig.ini | 3 +++ tests/custom_celeryconfig/__init__.py | 0 tests/custom_celeryconfig/config.py | 1 + tests/test_celery.py | 16 ++++++++++++++++ 7 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/configs/custom_useceleryconfig.ini create mode 100644 tests/custom_celeryconfig/__init__.py create mode 100644 tests/custom_celeryconfig/config.py diff --git a/README.rst b/README.rst index a48b5b8..5cc78b3 100644 --- a/README.rst +++ b/README.rst @@ -64,6 +64,9 @@ If you want to use the standard **celeryconfig** python file you can set the [celery] use_celeryconfig = True + celery_config_module = my.custom.celeryconfig + +You may also suppress the **celery_config_module** to use the default `"celeryconfig"` You can get more information for celeryconfig.py here: diff --git a/pyramid_celery/__init__.py b/pyramid_celery/__init__.py index bdc6ee3..583cca9 100644 --- a/pyramid_celery/__init__.py +++ b/pyramid_celery/__init__.py @@ -70,7 +70,7 @@ def setup_app(app, root, request, registry, closer, ini_location): ) if asbool(celery_config.get('use_celeryconfig', False)) is True: - config_path = 'celeryconfig' + config_path = celery_config.get('celery_config_module', 'celeryconfig') celery_app.config_from_object(config_path) else: hijack_key = 'worker_hijack_root_logger' diff --git a/setup.py b/setup.py index 38dc01a..e519d2c 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ import os -import sys from setuptools import setup, find_packages here = os.path.abspath(os.path.dirname(__file__)) diff --git a/tests/configs/custom_useceleryconfig.ini b/tests/configs/custom_useceleryconfig.ini new file mode 100644 index 0000000..3b50d30 --- /dev/null +++ b/tests/configs/custom_useceleryconfig.ini @@ -0,0 +1,3 @@ +[celery] +use_celeryconfig = true +celery_config_module = custom_celeryconfig.config diff --git a/tests/custom_celeryconfig/__init__.py b/tests/custom_celeryconfig/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/custom_celeryconfig/config.py b/tests/custom_celeryconfig/config.py new file mode 100644 index 0000000..942f10e --- /dev/null +++ b/tests/custom_celeryconfig/config.py @@ -0,0 +1 @@ +BROKER_URL = "redis://localhost:1337/1" diff --git a/tests/test_celery.py b/tests/test_celery.py index 954da2e..3938601 100644 --- a/tests/test_celery.py +++ b/tests/test_celery.py @@ -52,6 +52,22 @@ def test_includeme_use_celeryconfig(): assert celery_app.conf['broker_url'] == 'redis://localhost:1337/0' +@pytest.mark.unit +def test_includeme_use_custom_celeryconfig(): + from pyramid_celery import includeme + from pyramid_celery import celery_app + from pyramid import testing + from pyramid.registry import Registry + config = testing.setUp() + config.registry = Registry() + config.registry.settings = {} + + includeme(config) + config.configure_celery('tests/configs/custom_useceleryconfig.ini') + + assert celery_app.conf['broker_url'] == 'redis://localhost:1337/1' + + @pytest.mark.unit def test_preload_no_ini(): from pyramid_celery import on_preload_parsed