Skip to content

Commit

Permalink
Merge pull request #7 from edx/renzo/avoid-accidental-initialization
Browse files Browse the repository at this point in the history
Avoid initialization of Celery when used as a package
  • Loading branch information
Renzo Lucioni committed Jan 6, 2016
2 parents 4a28204 + 6e4dd1a commit 13560b5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
7 changes: 3 additions & 4 deletions ecommerce_worker/celery_app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
from celery import Celery

from ecommerce_worker.configuration import CONFIGURATION_MODULE

# Environment variable indicating which configuration module to use.
CONFIGURATION = 'WORKER_CONFIGURATION_MODULE'

# Set the default configuration module, if one is not aleady defined.
os.environ.setdefault(CONFIGURATION, 'ecommerce_worker.configuration.local')
os.environ.setdefault(CONFIGURATION_MODULE, 'ecommerce_worker.configuration.local')

app = Celery('ecommerce_worker')
# See http://celery.readthedocs.org/en/latest/userguide/application.html#config-from-envvar.
app.config_from_envvar(CONFIGURATION)
app.config_from_envvar(CONFIGURATION_MODULE)
6 changes: 6 additions & 0 deletions ecommerce_worker/configuration/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import os


# Environment variable indicating which configuration module to use
# when running the worker.
CONFIGURATION_MODULE = 'WORKER_CONFIGURATION_MODULE'


def get_overrides_filename(variable):
"""
Get the name of the file containing configuration overrides
Expand Down
2 changes: 2 additions & 0 deletions ecommerce_worker/fulfillment/v1/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import jwt
import mock

# Ensures that a Celery app is initialized when tests are run.
from ecommerce_worker import celery_app # pylint: disable=unused-import
from ecommerce_worker.fulfillment.v1.tasks import fulfill_order
from ecommerce_worker.utils import get_configuration

Expand Down
4 changes: 2 additions & 2 deletions ecommerce_worker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import sys

from ecommerce_worker.celery_app import CONFIGURATION
from ecommerce_worker.configuration import CONFIGURATION_MODULE


def get_configuration(variable):
Expand All @@ -17,7 +17,7 @@ def get_configuration(variable):
Returns:
The value corresponding to the variable, or None if the variable is not found.
"""
name = os.environ.get(CONFIGURATION)
name = os.environ.get(CONFIGURATION_MODULE)

# __import__ performs a full import, but only returns the top-level
# package, not the targeted module. sys.modules is a dictionary
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='edx-ecommerce-worker',
version='0.2.0',
version='0.3.0',
description='Celery tasks supporting the operations of edX\'s ecommerce service',
long_description=long_description,
classifiers=[
Expand Down

0 comments on commit 13560b5

Please sign in to comment.