Skip to content
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

save extended properties only when asked for #316

Merged
merged 9 commits into from Jun 12, 2022
97 changes: 57 additions & 40 deletions django_celery_results/backends/database.py
Expand Up @@ -54,6 +54,52 @@ def exception_safe_to_retry(self, exc):
return True
return False

def _get_extended_properties(self, request, traceback, using):
AmitPhulera marked this conversation as resolved.
Show resolved Hide resolved
properties = getattr(request, 'properties', {}) or {}
extended_props = {
'periodic_task_name': None,
'task_args': None,
'task_kwargs': None,
'task_name': None,
'traceback': None,
'using': None,
AmitPhulera marked this conversation as resolved.
Show resolved Hide resolved
'worker': None,
}
if request and self.app.conf.find_value_for_key('extended', 'result'):

if getattr(request, 'argsrepr', None) is not None:
# task protocol 2
task_args = request.argsrepr
else:
# task protocol 1
task_args = getattr(request, 'args', None)

if getattr(request, 'kwargsrepr', None) is not None:
# task protocol 2
task_kwargs = request.kwargsrepr
else:
# task protocol 1
task_kwargs = getattr(request, 'kwargs', None)

# Encode input arguments
if task_args is not None:
_, _, task_args = self.encode_content(task_args)

if task_kwargs is not None:
_, _, task_kwargs = self.encode_content(task_kwargs)

extended_props.update({
'periodic_task_name': properties.get('periodic_task_name', None),
'task_args': task_kwargs,
'task_kwargs': task_args,
'task_name': getattr(request, 'task', None),
'traceback': traceback,
'using': using,
'worker': getattr(request, 'hostname', None),
})

return extended_props

def _store_result(
self,
task_id,
Expand All @@ -69,48 +115,19 @@ def _store_result(
{'children': self.current_task_children(request)}
)

task_name = getattr(request, 'task', None)
properties = getattr(request, 'properties', {}) or {}
periodic_task_name = properties.get('periodic_task_name', None)
worker = getattr(request, 'hostname', None)

# Get input arguments
if getattr(request, 'argsrepr', None) is not None:
# task protocol 2
task_args = request.argsrepr
else:
# task protocol 1
task_args = getattr(request, 'args', None)
task_props = {
'content_encoding': content_encoding,
'content_type' : content_type,
'meta': meta,
'result': result,
'status': status,
'task_id': task_id,
'traceback': traceback,
}

if getattr(request, 'kwargsrepr', None) is not None:
# task protocol 2
task_kwargs = request.kwargsrepr
else:
# task protocol 1
task_kwargs = getattr(request, 'kwargs', None)
task_props.update(self._get_extended_properties(request, traceback, using))

# Encode input arguments
if task_args is not None:
_, _, task_args = self.encode_content(task_args)

if task_kwargs is not None:
_, _, task_kwargs = self.encode_content(task_kwargs)

self.TaskModel._default_manager.store_result(
content_type,
content_encoding,
task_id,
result,
status,
traceback=traceback,
meta=meta,
periodic_task_name=periodic_task_name,
task_name=task_name,
task_args=task_args,
task_kwargs=task_kwargs,
worker=worker,
using=using,
)
self.TaskModel._default_manager.store_result(**task_props)
return result

def _get_task_meta_for(self, task_id):
Expand Down