Skip to content

Commit d4e9597

Browse files
committed
Added support for django-celery-transactions. Closes django-haystack#7.
1 parent 4ac6bf5 commit d4e9597

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ By default a few dependencies will automatically be installed:
3434

3535
- django-appconf_ -- An app to gracefully handle application settings.
3636

37+
- `django-celery-transactions`_ -- An app that "holds on to Celery tasks
38+
until the current database transaction is committed, avoiding potential
39+
race conditions as described in `Celery's user guide`_."
40+
3741
.. _django-appconf: http://pypi.python.org/pypi/django-appconf
42+
.. _`django-celery-transactions`: https://github.com/chrisdoble/django-celery-transactions
43+
.. _`Celery's user guide`: http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions
3844

3945
Setup
4046
-----

celery_haystack/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CeleryHaystack(AppConf):
88
RETRY_DELAY = 5 * 60
99
MAX_RETRIES = 1
1010
DEFAULT_TASK = 'celery_haystack.tasks.CeleryHaystackSignalHandler'
11+
TRANSACTION_SAFE = True
1112

1213
COMMAND_BATCH_SIZE = None
1314
COMMAND_AGE = None

celery_haystack/tasks.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from django.core.management import call_command
33
from django.db.models.loading import get_model
44

5-
from celery.task import Task
65
from celery_haystack.conf import settings
76

87
try:
@@ -18,6 +17,11 @@
1817
except ImportError, e:
1918
raise ImproperlyConfigured("Haystack couldn't be imported: %s" % e)
2019

20+
if settings.CELERY_HAYSTACK_TRANSACTION_SAFE:
21+
from djcelery_transactions import PostTransactionTask as Task
22+
else:
23+
from celery.task import Task # noqa
24+
2125

2226
class CeleryHaystackSignalHandler(Task):
2327
using = settings.CELERY_HAYSTACK_DEFAULT_ALIAS
@@ -112,8 +116,8 @@ def run(self, action, identifier, **kwargs):
112116
current_index = self.get_index(model_class, **kwargs)
113117

114118
if action == 'delete':
115-
# If the object is gone, we'll use just the identifier against the
116-
# index.
119+
# If the object is gone, we'll use just the identifier
120+
# against the index.
117121
try:
118122
handler_options = self.get_handler_options(**kwargs)
119123
current_index.remove_object(identifier, **handler_options)

docs/changelog.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
Changelog
22
=========
33

4+
v0.6 (2012-06-27)
5+
-----------------
6+
7+
* *backwards incompatible change* Added support for
8+
`django-celery-transactions`_ to make sure the tasks are respecting
9+
Django's transaction management. It holds on to Celery tasks
10+
until the current database transaction is committed, avoiding potential
11+
race conditions as described in `Celery's user guide`_.
12+
13+
This is **enabled by default** but can be disabled in case you want
14+
to manually manage the tranasctions:
15+
16+
CELERY_HAYSTACK_TRANSACTION_SAFE = False
17+
18+
.. _`django-celery-transactions`: https://github.com/chrisdoble/django-celery-transactions
19+
.. _`Celery's user guide`: http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions
20+
421
v0.5 (2012-05-23)
522
-----------------
623

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def find_version(*file_paths):
3939
'Topic :: Utilities',
4040
],
4141
install_requires=[
42-
'django-appconf >= 0.4.1',
42+
'django-appconf>=0.4.1',
43+
'django-celery-transactions>=0.1.2'
4344
],
4445
zip_safe=False,
4546
)

0 commit comments

Comments
 (0)