From 4b0a88b3ce0012edb17921754cb34dbe2cbc9463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Angeluci?= Date: Thu, 6 Mar 2025 17:54:33 -0300 Subject: [PATCH 1/5] shadow name is the primary task name --- django_celery_results/backends/database.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index a4e364a6..d6cbefbe 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -88,12 +88,14 @@ def _get_extended_properties(self, request, traceback): _, _, task_kwargs = self.encode_content(task_kwargs) periodic_task_name = getattr(request, 'periodic_task_name', None) + shadow_task_name = getattr(request, 'shadow_task_name', None) + task_name = shadow_task_name or getattr(request, 'task_name', None) extended_props.update({ 'periodic_task_name': periodic_task_name, 'task_args': task_args, 'task_kwargs': task_kwargs, - 'task_name': getattr(request, 'task', None), + 'task_name': task_name, 'traceback': traceback, 'worker': getattr(request, 'hostname', None), }) From d7dd9135f213e516b7dd9d132149128414dd0c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Angeluci?= Date: Tue, 11 Mar 2025 10:20:47 -0300 Subject: [PATCH 2/5] preparing mock objects --- t/unit/backends/test_database.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/t/unit/backends/test_database.py b/t/unit/backends/test_database.py index cbf23e0d..c7f203fe 100644 --- a/t/unit/backends/test_database.py +++ b/t/unit/backends/test_database.py @@ -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_task_name = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -834,6 +835,7 @@ def test_callback_failure(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" + request.shadow_task_name = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -880,6 +882,7 @@ def test_on_chord_part_return_failure(self): request.id = tid1 request.group = gid request.task = "my_task" + request.shadow_task_name = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -1013,6 +1016,7 @@ def test_on_chord_part_return_multiple_databases(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" + request.shadow_task_name = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" From eb3b175d1d880c274627aa610e70390e5b64bce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Angeluci?= Date: Tue, 11 Mar 2025 11:15:02 -0300 Subject: [PATCH 3/5] must be and empty string --- t/unit/backends/test_database.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/t/unit/backends/test_database.py b/t/unit/backends/test_database.py index c7f203fe..0a11aa86 100644 --- a/t/unit/backends/test_database.py +++ b/t/unit/backends/test_database.py @@ -784,7 +784,7 @@ def test_on_chord_part_return(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" - request.shadow_task_name = None + request.shadow_task_name = "" request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -835,7 +835,7 @@ def test_callback_failure(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" - request.shadow_task_name = None + request.shadow_task_name = "" request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -882,7 +882,7 @@ def test_on_chord_part_return_failure(self): request.id = tid1 request.group = gid request.task = "my_task" - request.shadow_task_name = None + request.shadow_task_name = "" request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -1016,7 +1016,7 @@ def test_on_chord_part_return_multiple_databases(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" - request.shadow_task_name = None + request.shadow_task_name = "" request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" From 4a0266a7d2fc31953b6e5904b5c10429b6c49e67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Angeluci?= Date: Tue, 11 Mar 2025 14:50:01 -0300 Subject: [PATCH 4/5] using the correct name for the shadow property --- django_celery_results/backends/database.py | 4 ++-- t/unit/backends/test_database.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index d6cbefbe..207c7d6b 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -88,8 +88,8 @@ def _get_extended_properties(self, request, traceback): _, _, task_kwargs = self.encode_content(task_kwargs) periodic_task_name = getattr(request, 'periodic_task_name', None) - shadow_task_name = getattr(request, 'shadow_task_name', None) - task_name = shadow_task_name or getattr(request, 'task_name', None) + shadow_task_name = getattr(request, 'shadow', None) + task_name = shadow_task_name or getattr(request, 'task', None) extended_props.update({ 'periodic_task_name': periodic_task_name, diff --git a/t/unit/backends/test_database.py b/t/unit/backends/test_database.py index 0a11aa86..18e63c2c 100644 --- a/t/unit/backends/test_database.py +++ b/t/unit/backends/test_database.py @@ -784,7 +784,7 @@ def test_on_chord_part_return(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" - request.shadow_task_name = "" + request.shadow = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -835,7 +835,7 @@ def test_callback_failure(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" - request.shadow_task_name = "" + request.shadow = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -882,7 +882,7 @@ def test_on_chord_part_return_failure(self): request.id = tid1 request.group = gid request.task = "my_task" - request.shadow_task_name = "" + request.shadow = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" @@ -1016,7 +1016,7 @@ def test_on_chord_part_return_multiple_databases(self): request.id = subtasks[0].id request.group = gid request.task = "my_task" - request.shadow_task_name = "" + request.shadow = None request.args = ["a", 1, "password"] request.kwargs = {"c": 3, "d": "e", "password": "password"} request.argsrepr = "argsrepr" From 3728fddf9c14f572f97a139d39945041737f249a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Angeluci?= Date: Wed, 19 Mar 2025 16:01:41 -0300 Subject: [PATCH 5/5] adding a potential configuration --- django_celery_results/backends/database.py | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index 207c7d6b..29c08462 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -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 @@ -88,20 +89,36 @@ def _get_extended_properties(self, request, traceback): _, _, task_kwargs = self.encode_content(task_kwargs) periodic_task_name = getattr(request, 'periodic_task_name', None) - shadow_task_name = getattr(request, 'shadow', None) - task_name = shadow_task_name or getattr(request, 'task', None) extended_props.update({ 'periodic_task_name': periodic_task_name, 'task_args': task_args, 'task_kwargs': task_kwargs, - 'task_name': task_name, + '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.