-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resolve #305 abstract models #314
Open
jose-padin
wants to merge
29
commits into
celery:main
Choose a base branch
from
jose-padin:devel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−30
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
22c7c76
[#305] Added abstract models.
jose-padin c49ad2e
[#305] `ChordCounter` moved from `abstract` module to `generic` module.
jose-padin 25496f8
Issue 305: abstract models
jose-padin 97661fd
Update django_celery_results/models/__init__.py
auvipy a5554d7
[#305]: Improving abstract models implementation.
diegocastrum dbeb9a1
[#305]: `extend_task_props_callback` relocated.
diegocastrum 7565b36
[#305]: Added a default callable to `get_callback_function`
diegocastrum 1dfa0fc
Added newline to the end of
diegocastrum 14928be
[#305] Added a sanity check to `task_props_extension`
diegocastrum b70c44f
Fixed a NoneType error when the callback is not defined in project se…
diegocastrum cedccdf
[#305] Added documentation about this feature.
diegocastrum 0fc0b48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 242edab
Fixed a "wrong" description for the `ImproperlyConfigured` exception …
diegocastrum 7ddefa8
[#305] Fixed some pre-commit failures
diegocastrum a5681a6
Update docs/extending_task_results.rst
diegocastrum 48f278a
Update docs/extending_task_results.rst
diegocastrum fb5ea91
Update docs/extending_task_results.rst
diegocastrum 10a5c3a
feat(models): add `AbstractChordCounter` and update `ChordCounter`
diegocastrum d6b0d19
fix: refactor helper functions to avoid circular dependencies
diegocastrum 946ff07
fix: undefined name 'ChordCounter' and minor fixes
diegocastrum 39081b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cbdb7a4
fix: 'TypeError' introduced in previous commit
diegocastrum 29cc821
fix: include 'extra_fields' conditionally
diegocastrum c508179
fix: 'get_task_props_extensions()' missing 1 required argument
diegocastrum 101a24f
fix: TypeError introducedn on prev commit on 'AbstractChordCounter'
diegocastrum af843ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 90310b8
fix: ImportError introduced in previous commit in 'abstract.py'
diegocastrum e2a2bc0
style: Reformat import statements for better readability in 'database…
diegocastrum af5a226
fix: Update configuration for isort and black to enforce line length …
diegocastrum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ cover/ | |
.cache/ | ||
htmlcov/ | ||
coverage.xml | ||
.vscode |
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
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,3 @@ | ||
from .generic import ChordCounter, GroupResult, TaskResult | ||
|
||
__all__ = ["ChordCounter", "GroupResult", "TaskResult"] |
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,37 @@ | ||
"""Database models.""" | ||
|
||
from django_celery_results.models.abstract import ( | ||
AbstractChordCounter, | ||
AbstractGroupResult, | ||
AbstractTaskResult, | ||
) | ||
|
||
|
||
class TaskResult(AbstractTaskResult): | ||
"""Task result/status.""" | ||
|
||
class Meta(AbstractTaskResult.Meta): | ||
"""Table information.""" | ||
|
||
abstract = False | ||
app_label = "django_celery_results" | ||
|
||
|
||
class ChordCounter(AbstractChordCounter): | ||
"""Chord synchronisation.""" | ||
|
||
class Meta(AbstractChordCounter.Meta): | ||
"""Table information.""" | ||
|
||
abstract = False | ||
app_label = "django_celery_results" | ||
|
||
|
||
class GroupResult(AbstractGroupResult): | ||
"""Task Group result/status.""" | ||
|
||
class Meta(AbstractGroupResult.Meta): | ||
"""Table information.""" | ||
|
||
abstract = False | ||
app_label = "django_celery_results" |
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,72 @@ | ||
from django.apps import apps | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from .generic import ChordCounter, GroupResult, TaskResult | ||
|
||
|
||
def taskresult_model(): | ||
"""Return the TaskResult model that is active in this project.""" | ||
if not hasattr(settings, 'CELERY_RESULTS_TASKRESULT_MODEL'): | ||
return TaskResult | ||
|
||
try: | ||
return apps.get_model( | ||
settings.CELERY_RESULTS_TASKRESULT_MODEL | ||
) | ||
except ValueError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_TASKRESULT_MODEL must be of the form " | ||
"'app_label.model_name'" | ||
) | ||
except LookupError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_TASKRESULT_MODEL refers to model " | ||
f"'{settings.CELERY_RESULTS_TASKRESULT_MODEL}' that has not " | ||
"been installed" | ||
) | ||
|
||
|
||
def chordcounter_model(): | ||
"""Return the ChordCounter model that is active in this project.""" | ||
|
||
if not hasattr(settings, 'CELERY_RESULTS_CHORDCOUNTER_MODEL'): | ||
return ChordCounter | ||
|
||
try: | ||
return apps.get_model( | ||
settings.CELERY_RESULTS_CHORDCOUNTER_MODEL | ||
) | ||
except ValueError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_CHORDCOUNTER_MODEL must be of the form " | ||
"'app_label.model_name'" | ||
) | ||
except LookupError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_CHORDCOUNTER_MODEL refers to model " | ||
f"'{settings.CELERY_RESULTS_CHORDCOUNTER_MODEL}' that has not " | ||
"been installed" | ||
) | ||
|
||
|
||
def groupresult_model(): | ||
"""Return the GroupResult model that is active in this project.""" | ||
if not hasattr(settings, 'CELERY_RESULTS_GROUPRESULT_MODEL'): | ||
return GroupResult | ||
|
||
try: | ||
return apps.get_model( | ||
settings.CELERY_RESULTS_GROUPRESULT_MODEL | ||
) | ||
except ValueError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_GROUPRESULT_MODEL must be of the form " | ||
"'app_label.model_name'" | ||
) | ||
except LookupError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_GROUPRESULT_MODEL refers to model " | ||
f"'{settings.CELERY_RESULTS_GROUPRESULT_MODEL}' that has not " | ||
"been installed" | ||
) |
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,35 @@ | ||
from collections.abc import Mapping | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
|
||
def get_callback_function(settings_name, default=None): | ||
"""Return the callback function for the given settings name.""" | ||
callback = getattr(settings, settings_name, None) | ||
if not callback: | ||
return default | ||
|
||
if not callable(callback): | ||
raise ImproperlyConfigured(f"{settings_name} must be callable.") | ||
|
||
return callback | ||
|
||
|
||
extend_task_props_callback = get_callback_function( | ||
"CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK" | ||
) | ||
|
||
|
||
def get_task_props_extension(request, task_props): | ||
"""Extend the task properties with custom props to fill custom models.""" | ||
if not extend_task_props_callback: | ||
return {} | ||
|
||
task_props_extension = extend_task_props_callback(request, task_props) or {} # noqa E501 | ||
if not isinstance(task_props_extension, Mapping): | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK must return a Mapping." | ||
) | ||
|
||
return task_props_extension |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding some sanity checks on the return value of the callback.
For example that it complies to the
Mapping
protocol.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well,
get_callback_function
is quite generic, and could be used in the future for another purposes, returning different types of data. So far I can see, the sanity checks could be in_store_results()
whereextend_task_props_callback()
is called. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm personally not a fan of this generic function, I'd err of the side of caution and make the callback handling as clean and explicit as possible so any errors we need to raise have a clear source.
But this is more of a code-style thing so maybe one of the maintainers (@auvipy) can comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point @AllexVeldman, what do you think @auvipy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AllexVeldman & @auvipy I think I found a proper way to control the callback internally being able to check explicitly that return value.
I just created a new function called
get_task_props_extension()
into the settings module in charge to return an empty dict in case that theCELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK
is undefined and otherwise will check that the return value complies with the Mapping protocol.Let me know what you think!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM