diff --git a/django_celery_results/admin.py b/django_celery_results/admin.py index b4a36a51..220874d3 100644 --- a/django_celery_results/admin.py +++ b/django_celery_results/admin.py @@ -18,8 +18,10 @@ class TaskResultAdmin(admin.ModelAdmin): model = TaskResult date_hierarchy = 'date_done' - list_display = ('task_id', 'task_name', 'date_done', 'status', 'worker') - list_filter = ('status', 'date_done', 'task_name', 'worker') + list_display = ('task_id', 'periodic_task_name', 'task_name', 'date_done', + 'status', 'worker') + list_filter = ('status', 'date_done', 'periodic_task_name', 'task_name', + 'worker') readonly_fields = ('date_created', 'date_done', 'result', 'meta') search_fields = ('task_name', 'task_id', 'status', 'task_args', 'task_kwargs') diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index ea936421..2eb73254 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -40,6 +40,8 @@ def _store_result( ) task_name = getattr(request, 'task', None) + properties = getattr(request, 'properties', {}) + periodic_task_name = properties.get('periodic_task_name', None) worker = getattr(request, 'hostname', None) # Get input arguments @@ -72,6 +74,7 @@ def _store_result( status, traceback=traceback, meta=meta, + periodic_task_name=periodic_task_name, task_name=task_name, task_args=task_args, task_kwargs=task_kwargs, diff --git a/django_celery_results/managers.py b/django_celery_results/managers.py index 3c0789f5..3242e1d7 100644 --- a/django_celery_results/managers.py +++ b/django_celery_results/managers.py @@ -119,6 +119,7 @@ def get_task(self, task_id): def store_result(self, content_type, content_encoding, task_id, result, status, traceback=None, meta=None, + periodic_task_name=None, task_name=None, task_args=None, task_kwargs=None, worker=None, using=None): """Store the result and status of a task. @@ -128,6 +129,7 @@ def store_result(self, content_type, content_encoding, content_type (str): Mime-type of result and meta content. content_encoding (str): Type of encoding (e.g. binary/utf-8). task_id (str): Id of task. + periodic_task_name (str): Celery Periodic task name. task_name (str): Celery task name. task_args (str): Task arguments. task_kwargs (str): Task kwargs. @@ -157,6 +159,7 @@ def store_result(self, content_type, content_encoding, 'meta': meta, 'content_encoding': content_encoding, 'content_type': content_type, + 'periodic_task_name': periodic_task_name, 'task_name': task_name, 'task_args': task_args, 'task_kwargs': task_kwargs, diff --git a/django_celery_results/migrations/0011_taskresult_periodic_task_name.py b/django_celery_results/migrations/0011_taskresult_periodic_task_name.py new file mode 100644 index 00000000..5cfb3c23 --- /dev/null +++ b/django_celery_results/migrations/0011_taskresult_periodic_task_name.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.8 on 2021-11-10 08:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_celery_results', '0010_remove_duplicate_indices'), + ] + + operations = [ + migrations.AddField( + model_name='taskresult', + name='periodic_task_name', + field=models.CharField( + help_text='Name of the Periodic Task which was run', + max_length=255, + null=True, + verbose_name='Periodic Task Name'), + ), + ] diff --git a/django_celery_results/models.py b/django_celery_results/models.py index 5558f8f5..70a3e420 100644 --- a/django_celery_results/models.py +++ b/django_celery_results/models.py @@ -27,6 +27,10 @@ class TaskResult(models.Model): unique=True, verbose_name=_('Task ID'), help_text=_('Celery ID for the Task that was run')) + periodic_task_name = models.CharField( + null=True, max_length=255, + verbose_name=_('Periodic Task Name'), + help_text=_('Name of the Periodic Task which was run')) task_name = models.CharField( null=True, max_length=255, verbose_name=_('Task Name'), diff --git a/docs/includes/introduction.txt b/docs/includes/introduction.txt index 4e5c31f8..339ffe42 100644 --- a/docs/includes/introduction.txt +++ b/docs/includes/introduction.txt @@ -13,6 +13,9 @@ It defines 2 models (``django_celery_results.models.TaskResult`` and ``django_ce used to store task and group results, and you can query these database tables like any other Django model. +If your `django-celery-beat` carries `request["properties"]["periodic_task_name"]`, +it will be stored in `TaskResult.periodic_task_name` to track the periodic task. + Installing ========== diff --git a/t/unit/backends/test_database.py b/t/unit/backends/test_database.py index 87cdd255..ccc62714 100644 --- a/t/unit/backends/test_database.py +++ b/t/unit/backends/test_database.py @@ -722,6 +722,7 @@ def test_on_chord_part_return(self): request.argsrepr = "argsrepr" request.kwargsrepr = "kwargsrepr" request.hostname = "celery@ip-0-0-0-0" + request.properties = {"periodic_task_name": "my_periodic_task"} request.ignore_result = False result = {"foo": "baz"} @@ -770,6 +771,7 @@ def test_callback_failure(self): request.argsrepr = "argsrepr" request.kwargsrepr = "kwargsrepr" request.hostname = "celery@ip-0-0-0-0" + request.properties = {"periodic_task_name": "my_periodic_task"} request.ignore_result = False request.chord.id = cid result = {"foo": "baz"} @@ -815,6 +817,7 @@ def test_on_chord_part_return_failure(self): request.argsrepr = "argsrepr" request.kwargsrepr = "kwargsrepr" request.hostname = "celery@ip-0-0-0-0" + request.properties = {"periodic_task_name": "my_periodic_task"} request.chord.id = cid result = {"foo": "baz"}