Skip to content

Commit dbd822c

Browse files
committed
[celery#305] Added a sanity check to task_props_extension
Added `get_task_props_extension` to `settings` module which will raise an `ImproperlyConfigured` when the task_props_extension doesn't complies with the Mapping protocol. `DatabaseBackend` will make use of `get_task_props_extension` to update a potential custom model with the custom properties --- Resolves celery#305 Fixes celery#314
1 parent 8799d8f commit dbd822c

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

django_celery_results/backends/database.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import binascii
22
import json
3+
from typing import Mapping
34

45
from celery import maybe_signature
56
from celery.backends.base import BaseDictBackend
@@ -13,7 +14,7 @@
1314

1415
from ..models import ChordCounter
1516
from ..models.helpers import taskresult_model, groupresult_model
16-
from ..settings import extend_task_props_callback
17+
from ..settings import get_task_props_extension
1718

1819
EXCEPTIONS_TO_CATCH = (InterfaceError,)
1920

@@ -125,14 +126,8 @@ def _store_result(
125126
'using': using,
126127
}
127128

128-
task_props.update(
129-
self._get_extended_properties(request, traceback)
130-
)
131-
132-
# TODO: Wrap this and make some sanity checks to complain the Mapping
133-
# protocol.
134-
task_props.update(
135-
extend_task_props_callback(request, dict(task_props)))
129+
task_props.update(self._get_extended_properties(request, traceback))
130+
task_props.update(get_task_props_extension(request, dict(task_props)))
136131

137132
self.TaskModel._default_manager.store_result(**task_props)
138133
return result

django_celery_results/settings.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.conf import settings
22
from django.core.exceptions import ImproperlyConfigured
3+
from collections.abc import Mapping
34

45

56
def get_callback_function(settings_name, default=None):
@@ -16,5 +17,21 @@ def get_callback_function(settings_name, default=None):
1617

1718

1819
extend_task_props_callback = get_callback_function(
19-
"CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK", dict
20+
"CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK"
2021
)
22+
23+
24+
def get_task_props_extension(request, task_props):
25+
"""Extend the task properties with custom properties to fill custom models."""
26+
27+
task_props_extension = extend_task_props_callback(request, task_props) or {}
28+
if task_props_extension is None:
29+
return {}
30+
31+
if not isinstance(task_props_extension, Mapping):
32+
raise ImproperlyConfigured(
33+
"CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK must return a Mapping "
34+
"instance."
35+
)
36+
37+
return task_props_extension

0 commit comments

Comments
 (0)