-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Tasks panel showing tasks queued during the request #2407
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
Open
onxxdatas
wants to merge
11
commits into
django-commons:main
Choose a base branch
from
onxxdatas:tasks-panel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fb99809
Add Tasks panel showing tasks queued during the request
onxxdatas d43b177
i added a tasks demo page to the example project
onxxdatas 3d0ade3
Add changelog entry for Tasks panel
onxxdatas 907a0e4
Address reviews
onxxdatas 3fc366f
Merge branch 'main' into tasks-panel
matthiask 5d864c9
Address review feedback for Tasks panel
onxxdatas 847ed11
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0572288
- Restructure template to avoid negation (if tasks / elif tasks_avail…
onxxdatas 45e1bfa
Using 'python path' instead of 'Name'
onxxdatas 316bb01
Store TaskResult on the panel, flatten to dict only in generate_stats
onxxdatas 3d41ac2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from django import VERSION | ||
| from django.utils.translation import gettext_lazy as _, ngettext | ||
|
|
||
| from debug_toolbar.panels import Panel | ||
| from debug_toolbar.utils import sanitize_and_sort_request_vars | ||
|
|
||
| if VERSION >= (6, 0): | ||
| from django.tasks.signals import task_enqueued | ||
| else: | ||
| task_enqueued = None | ||
|
|
||
|
|
||
| class TasksPanel(Panel): | ||
| """ | ||
| Panel that displays the tasks queued during the request. | ||
|
|
||
| This relies on Django's built-in tasks framework (``django.tasks``), | ||
| which was added in Django 6.0. On older versions of Django, the panel | ||
| explains that upgrading Django is required in order to see task | ||
| information. | ||
| """ | ||
|
|
||
| template = "debug_toolbar/panels/tasks.html" | ||
|
|
||
| is_async = True | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.tasks: list = [] # populated with TaskResult instances in _record_task | ||
|
|
||
| nav_title = _("Tasks") | ||
|
|
||
| @property | ||
| def nav_subtitle(self): | ||
| if VERSION < (6, 0): | ||
| return _("Requires Django 6.0+") | ||
| tasks = self.get_stats()["tasks"] | ||
| return ngettext("%(count)d task", "%(count)d tasks", len(tasks)) % { | ||
| "count": len(tasks) | ||
| } | ||
|
|
||
| title = _("Tasks") | ||
|
|
||
| def _record_task(self, sender, task_result, **kwargs): | ||
| # Store the TaskResult as-is; it's only flattened into a | ||
| # JSON-serializable dict in generate_stats, right before the | ||
| # panel's stats get serialized (see debug_toolbar/store.py). | ||
| self.tasks.append(task_result) | ||
|
|
||
| def enable_instrumentation(self): | ||
| if task_enqueued is not None: | ||
| task_enqueued.connect(self._record_task) | ||
|
|
||
| def disable_instrumentation(self): | ||
| if task_enqueued is not None: | ||
| task_enqueued.disconnect(self._record_task) | ||
|
|
||
| def generate_stats(self, request, response): | ||
| tasks = [] | ||
| for task_result in self.tasks: | ||
| task = task_result.task | ||
| tasks.append( | ||
| { | ||
| "id": task_result.id, | ||
| "module_path": task.module_path, | ||
| "queue_name": task.queue_name, | ||
| "priority": task.priority, | ||
| "backend": task_result.backend, | ||
| "run_after": task.run_after, | ||
| "takes_context": task.takes_context, | ||
| "args": sanitize_and_sort_request_vars(task_result.args), | ||
| "kwargs": sanitize_and_sort_request_vars(task_result.kwargs), | ||
| "status": task_result.status, | ||
| } | ||
| ) | ||
| self.record_stats( | ||
| { | ||
| "tasks_available": VERSION >= (6, 0), | ||
| "tasks": tasks, | ||
| } | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| {% load i18n %} | ||
| {% if tasks %} | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th>{% translate "python path"|title context "noun" %}</th> | ||
| <th>{% translate "queue"|title context "noun" %}</th> | ||
| <th>{% translate "backend"|title %}</th> | ||
| <th>{% translate "priority"|title %}</th> | ||
| <th>{% translate "run after"|title %}</th> | ||
| <th>{% translate "status"|title %}</th> | ||
| <th>{% translate "arguments"|title %}</th> | ||
| <th>{% translate "keyword arguments"|title %}</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {% for task in tasks %} | ||
| <tr> | ||
| <td>{{ task.module_path|escape }}</td> | ||
| <td>{{ task.queue_name|escape }}</td> | ||
| <td>{{ task.backend|escape }}</td> | ||
| <td>{{ task.priority }}</td> | ||
| <td>{{ task.run_after|default:"—" }}</td> | ||
| <td>{{ task.status }}</td> | ||
| <td><code>{{ task.args.raw|pprint }}</code></td> | ||
| <td> | ||
| <code> | ||
| {% if task.kwargs.list %} | ||
| {% for key, value in task.kwargs.list %}{{ key }}={{ value|pprint }}{% if not forloop.last %}, {% endif %}{% endfor %} | ||
| {% else %} | ||
| {{ task.kwargs.raw|pprint }} | ||
| {% endif %} | ||
| </code> | ||
| </td> | ||
| </tr> | ||
| {% endfor %} | ||
| </tbody> | ||
| </table> | ||
| {% elif tasks_available %} | ||
| <p>{% translate "No tasks were queued during this request." %}</p> | ||
| {% else %} | ||
| <p> | ||
| {% blocktranslate %} | ||
| The Tasks panel requires Django 6.0 or later. Upgrade Django to see | ||
| information about tasks queued during this request. | ||
| {% endblocktranslate %} | ||
| </p> | ||
| {% endif %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
| <title>Tasks</title> | ||
| </head> | ||
| <body> | ||
| <h1>Tasks Test</h1> | ||
| {% if tasks_available %} | ||
| <p>Two tasks were just enqueued. Check the Tasks panel to see them.</p> | ||
| {% else %} | ||
| <p> | ||
| django.tasks requires Django 6.0+. Check the Tasks panel to see the | ||
| upgrade message. | ||
| </p> | ||
| {% endif %} | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import unittest | ||
|
|
||
| from debug_toolbar._compat import django_has_tasks_support, task | ||
| from debug_toolbar.panels.tasks import TasksPanel | ||
|
|
||
| from ..base import BaseTestCase | ||
|
|
||
|
|
||
| @task | ||
| def sample_task(x, y=1): | ||
| return x + y | ||
|
|
||
|
|
||
| class TasksPanelTestCase(BaseTestCase): | ||
| panel_id = TasksPanel.panel_id | ||
|
|
||
| @unittest.skipUnless(not django_has_tasks_support, "Requires Django < 6.0") | ||
| def test_nav_subtitle_without_tasks_support(self): | ||
| """ | ||
| On Django < 6.0, django.tasks doesn't exist, so the panel should | ||
| explain that instead of showing task data. | ||
| """ | ||
| self.panel.generate_stats(self.request, None) | ||
| self.assertEqual(str(self.panel.nav_subtitle), "Requires Django 6.0+") | ||
| stats = self.panel.get_stats() | ||
| self.assertEqual(stats, {"tasks_available": False, "tasks": []}) | ||
|
|
||
| @unittest.skipUnless(django_has_tasks_support, "Requires Django 6.0+") | ||
| def test_no_tasks_queued(self): | ||
| self.panel.generate_stats(self.request, None) | ||
| stats = self.panel.get_stats() | ||
| self.assertEqual(stats, {"tasks_available": True, "tasks": []}) | ||
| self.assertEqual(str(self.panel.nav_subtitle), "0 tasks") | ||
|
|
||
| @unittest.skipUnless(django_has_tasks_support, "Requires Django 6.0+") | ||
| def test_records_queued_task(self): | ||
| sample_task.enqueue(2, y=3) | ||
|
|
||
| self.panel.generate_stats(self.request, None) | ||
| stats = self.panel.get_stats() | ||
| self.assertEqual(len(stats["tasks"]), 1) | ||
|
|
||
| # id, priority, run_after, and status are runtime-dependent, so | ||
| # pull them from the actual result and assert the rest of the | ||
| # dict matches exactly around them. | ||
| recorded = stats["tasks"][0] | ||
| self.assertEqual( | ||
| stats, | ||
| { | ||
| "tasks_available": True, | ||
| "tasks": [ | ||
| { | ||
| "id": recorded["id"], | ||
| "module_path": f"{__name__}.sample_task", | ||
| "queue_name": "default", | ||
| "priority": recorded["priority"], | ||
| "backend": "default", | ||
| "run_after": recorded["run_after"], | ||
| "takes_context": False, | ||
| "args": {"raw": [2]}, | ||
| "kwargs": {"list": [("y", 3)]}, | ||
| "status": recorded["status"], | ||
| } | ||
| ], | ||
| }, | ||
| ) | ||
|
|
||
| @unittest.skipUnless(django_has_tasks_support, "Requires Django 6.0+") | ||
| def test_nav_subtitle_counts_multiple_tasks(self): | ||
| sample_task.enqueue(1) | ||
| sample_task.enqueue(2) | ||
|
|
||
| self.panel.generate_stats(self.request, None) | ||
|
|
||
| self.assertEqual(str(self.panel.nav_subtitle), "2 tasks") | ||
|
|
||
| @unittest.skipUnless(django_has_tasks_support, "Requires Django 6.0+") | ||
| def test_disable_instrumentation_stops_recording(self): | ||
| self.panel.disable_instrumentation() | ||
| try: | ||
| sample_task.enqueue(1) | ||
| finally: | ||
| # Restore instrumentation so tearDown's disable call is a no-op | ||
| # rather than raising for double-disconnecting. | ||
| self.panel.enable_instrumentation() | ||
|
|
||
| self.panel.generate_stats(self.request, None) | ||
| stats = self.panel.get_stats() | ||
| self.assertEqual(stats["tasks"], []) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a merge error, but there seems to be duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed!