forked from celery/celery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Task class specialised for Django (celery#8491)
* Add a Task class for Django * Automatically use specialised Django Task class * Add unit tests for specialized Django task * Don't use specialized Django task if customised by user * Add patch to avoid side effects with other tests * Rename task class to DjangoTask * Add versionadded * Add reference page for new DjangoTask * Fix generation of reference documentation for DjangoTask * Fix links & extend documentation * Fix link to base task in docs * Improve links in DjangoTask docs * Improve more links in DjangoTask docs * Apply suggestions from code review Co-authored-by: Asif Saif Uddin <[email protected]> * Update Django example to demo the new delay_on_commit() method * Replace try/catch ImportError for documentation by autodoc_mock_imports --------- Co-authored-by: Asif Saif Uddin <[email protected]>
- Loading branch information
1 parent
86895a9
commit da1146a
Showing
12 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import functools | ||
|
||
from django.db import transaction | ||
|
||
from celery.app.task import Task | ||
|
||
|
||
class DjangoTask(Task): | ||
""" | ||
Extend the base :class:`~celery.app.task.Task` for Django. | ||
Provide a nicer API to trigger tasks at the end of the DB transaction. | ||
""" | ||
|
||
def delay_on_commit(self, *args, **kwargs): | ||
"""Call :meth:`~celery.app.task.Task.delay` with Django's ``on_commit()``.""" | ||
return transaction.on_commit(functools.partial(self.delay, *args, **kwargs)) | ||
|
||
def apply_async_on_commit(self, *args, **kwargs): | ||
"""Call :meth:`~celery.app.task.Task.apply_async` with Django's ``on_commit()``.""" | ||
return transaction.on_commit(functools.partial(self.apply_async, *args, **kwargs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,8 @@ | |
r'^http://localhost' | ||
], | ||
autodoc_mock_imports=[ | ||
'riak' | ||
'riak', | ||
'django', | ||
] | ||
)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
==================================== | ||
``celery.contrib.django.task`` | ||
==================================== | ||
|
||
.. versionadded:: 5.4 | ||
|
||
.. contents:: | ||
:local: | ||
|
||
API Reference | ||
============= | ||
|
||
.. currentmodule:: celery.contrib.django.task | ||
|
||
.. automodule:: celery.contrib.django.task | ||
:members: | ||
:undoc-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.patched_module( | ||
'django', | ||
'django.db', | ||
'django.db.transaction', | ||
) | ||
@pytest.mark.usefixtures("module") | ||
class test_DjangoTask: | ||
@pytest.fixture | ||
def task_instance(self): | ||
from celery.contrib.django.task import DjangoTask | ||
yield DjangoTask() | ||
|
||
@pytest.fixture(name="on_commit") | ||
def on_commit(self): | ||
with patch( | ||
'django.db.transaction.on_commit', | ||
side_effect=lambda f: f(), | ||
) as patched_on_commit: | ||
yield patched_on_commit | ||
|
||
def test_delay_on_commit(self, task_instance, on_commit): | ||
result = task_instance.delay_on_commit() | ||
assert result is not None | ||
|
||
def test_apply_async_on_commit(self, task_instance, on_commit): | ||
result = task_instance.apply_async_on_commit() | ||
assert result is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters