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

Set Task Shadow Names as the Primary Task Name #469

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion django_celery_results/backends/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from celery.result import GroupResult, allow_join_result, result_from_tuple
from celery.utils.log import get_logger
from celery.utils.serialization import b64decode, b64encode
from django.conf import settings
from django.db import connection, router, transaction
from django.db.models.functions import Now
from django.db.utils import InterfaceError
Expand Down Expand Up @@ -93,13 +94,31 @@ def _get_extended_properties(self, request, traceback):
'periodic_task_name': periodic_task_name,
'task_args': task_args,
'task_kwargs': task_kwargs,
'task_name': getattr(request, 'task', None),
'task_name': self._get_task_name(request),
'traceback': traceback,
'worker': getattr(request, 'hostname', None),
})

return extended_props

def _get_task_name(self, request):
"""
Get the task name from the request, optionally using the shadow name.

If `DJANGO_CELERY_RESULTS_USE_SHADOW_NAME` is enabled and a shadow task
name is available, return the shadow task name; otherwise, return the
regular task name.
"""
use_shadow_name = (
getattr(settings, "DJANGO_CELERY_RESULTS_USE_SHADOW_NAME", False)
is True
)
task_name = getattr(request, "task", None)
if not use_shadow_name:
return task_name
shadow_task_name = getattr(request, "shadow", None)
return shadow_task_name or task_name

def _get_meta_from_request(self, request=None):
"""
Use the request or get_current_task to evaluate the `meta` attribute.
Expand Down
4 changes: 4 additions & 0 deletions t/unit/backends/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ def test_on_chord_part_return(self):
request.id = subtasks[0].id
request.group = gid
request.task = "my_task"
request.shadow = None
request.args = ["a", 1, "password"]
request.kwargs = {"c": 3, "d": "e", "password": "password"}
request.argsrepr = "argsrepr"
Expand Down Expand Up @@ -834,6 +835,7 @@ def test_callback_failure(self):
request.id = subtasks[0].id
request.group = gid
request.task = "my_task"
request.shadow = None
request.args = ["a", 1, "password"]
request.kwargs = {"c": 3, "d": "e", "password": "password"}
request.argsrepr = "argsrepr"
Expand Down Expand Up @@ -880,6 +882,7 @@ def test_on_chord_part_return_failure(self):
request.id = tid1
request.group = gid
request.task = "my_task"
request.shadow = None
request.args = ["a", 1, "password"]
request.kwargs = {"c": 3, "d": "e", "password": "password"}
request.argsrepr = "argsrepr"
Expand Down Expand Up @@ -1026,6 +1029,7 @@ def test_on_chord_part_return_multiple_databases(self):
request.id = subtasks[0].id
request.group = gid
request.task = "my_task"
request.shadow = None
request.args = ["a", 1, "password"]
request.kwargs = {"c": 3, "d": "e", "password": "password"}
request.argsrepr = "argsrepr"
Expand Down