Skip to content

Commit

Permalink
Added support for --ini-var argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek committed Dec 28, 2014
1 parent fb69dd4 commit b946494
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ An example ini configuration looks like this:
type = integer
schedule = 30
Running the worker
=============================
To run the worker we just use the standard celery command with an additional
argument:

.. code-block:: bash
celery worker -A pyramid_celery.celery_app --ini development.ini
If you've defined variables in your .ini like %(database_username)s you can use
the *--ini-var* argument, which is a comma separated list of key value pairs:

.. code-block:: bash
celery worker -A pyramid_celery.celery_app --ini development.ini --ini-var=database_username=sontek,database_password=OhYeah!
The reason it is a csv instead of using *--ini-var* multiple times is because of
a bug in celery itself. When they fix the bug we will re-work the API. Ticket
is here:

https://github.com/celery/celery/pull/2435


Demo
=====================
To see it all in action check out examples/long_running_with_tm, run
Expand Down
2 changes: 1 addition & 1 deletion examples/long_running_with_tm/development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pyramid.includes = pyramid_debugtoolbar
pyramid_tm
pyramid_celery

sqlalchemy.url = postgresql://localhost/celery_demo
sqlalchemy.url = postgresql://localhost/%(database)s

[celery]
BROKER_URL = redis://localhost:6379/0
Expand Down
29 changes: 24 additions & 5 deletions pyramid_celery/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from celery import Celery
from celery import signals
from celery.bin import Option

from optparse import make_option
from pyramid.paster import bootstrap
from pyramid_celery.loaders import INILoader
from pyramid.settings import asbool
Expand All @@ -10,8 +10,17 @@


celery_app.user_options['preload'].add(
Option('-i', '--ini',
help='Paste ini configuration file.'),
make_option(
'-i', '--ini',
default=None,
help='Paste ini configuration file.'),
)

celery_app.user_options['preload'].add(
make_option(
'--ini-var',
default=None,
help='Comma separated list of key=value to pass to ini'),
)


Expand All @@ -31,12 +40,22 @@ def setup_app(registry, ini_location):
@signals.user_preload_options.connect
def on_preload_parsed(options, **kwargs):
ini_location = options['ini']
ini_vars = options['ini_var']

if isinstance(ini_location, tuple) and ini_location[0] == 'NO':
if ini_location is None:
print('You must provide the paste --ini argument')
exit(-1)

env = bootstrap(ini_location)
options = {}
if ini_vars is not None:
for pairs in ini_vars.split(','):
key, value = pairs.split('=')
options[key] = value

env = bootstrap(ini_location, options=options)
else:
env = bootstrap(ini_location)

registry = env['registry']
setup_app(registry, ini_location)

Expand Down
19 changes: 17 additions & 2 deletions tests/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def test_includeme_use_celeryconfig():
def test_preload_no_ini():
from pyramid_celery import on_preload_parsed
options = {
'ini': ('NO', 'DEFAULT'),
'ini': None,
'ini_var': None,
}

with pytest.raises(SystemExit):
Expand All @@ -61,7 +62,8 @@ def test_preload_no_ini():
def test_preload_ini():
from pyramid_celery import on_preload_parsed
options = {
'ini': 'tests/configs/dev.ini'
'ini': 'tests/configs/dev.ini',
'ini_var': None,
}

with mock.patch('pyramid_celery.bootstrap') as boot:
Expand All @@ -85,3 +87,16 @@ def test_celery_imports():
'myapp.tasks',
'otherapp.tasks'
]

@pytest.mark.unit
def test_preload_with_ini_vars():
from pyramid_celery import on_preload_parsed
options = {
'ini': 'tests/configs/dev.ini',
'ini_var': 'database=foo,password=bar',
}

with mock.patch('pyramid_celery.bootstrap') as boot:
on_preload_parsed(options)
expected_vars = {'database': 'foo', 'password': 'bar'}
assert boot.called_with('dev.ini', expected_vars)

0 comments on commit b946494

Please sign in to comment.