Skip to content

Commit

Permalink
feat: add periodic_task_name (#261)
Browse files Browse the repository at this point in the history
* feta: add periodic_task_name

* flake8

* fix MagicMock

* add doc

Co-authored-by: mba <[email protected]>
  • Loading branch information
lvelvee and mba authored Dec 15, 2021
1 parent e8bff31 commit 5e67a31
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django_celery_results/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions django_celery_results/backends/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions django_celery_results/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
),
]
4 changes: 4 additions & 0 deletions django_celery_results/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
3 changes: 3 additions & 0 deletions docs/includes/introduction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========

Expand Down
3 changes: 3 additions & 0 deletions t/unit/backends/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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"}

Expand Down

0 comments on commit 5e67a31

Please sign in to comment.