Skip to content

Commit afa2297

Browse files
committed
Experiment with tasks panel
1 parent d4b08ed commit afa2297

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

debug_toolbar/panels/tasks.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,55 @@ def generate_stats(self, request, response):
3333
self.record_stats(stats)
3434

3535
def enable_instrumentation(self):
36-
pass
36+
"""Hook into task system to collect queued tasks"""
37+
try:
38+
import django
39+
40+
if django.VERSION < (6, 0):
41+
return
42+
from django.tasks import Task
43+
44+
print("[TasksPanel] instrumentation enabled:", hasattr(Task, "enqueue"))
45+
46+
# Store original enqueue method
47+
if hasattr(Task, "enqueue"):
48+
self._original_enqueue = Task.enqueue
49+
50+
def wrapped_enqueue(task, *args, **kwargs):
51+
result = self._original_enqueue(task, *args, **kwargs).return_value
52+
self._record_task(task, args, kwargs, result)
53+
return result
54+
55+
Task.enqueue = wrapped_enqueue
56+
except (ImportError, AttributeError):
57+
pass
58+
59+
def _record_task(self, task, args, kwargs, result):
60+
"""Record a task that was queued"""
61+
task_info = {
62+
"name": getattr(task, "__name__", str(task)),
63+
"args": repr(args) if args else "",
64+
"kwargs": repr(kwargs) if kwargs else "",
65+
}
66+
self.queued_tasks.append(task_info)
3767

3868
def disable_instrumentation(self):
39-
pass
69+
"""Restore original methods"""
70+
try:
71+
from django.tasks import Task
72+
73+
if hasattr(self, "_original_enqueue"):
74+
Task.enqueue = self._original_enqueue
75+
except (ImportError, AttributeError):
76+
pass
77+
78+
def _check_tasks_available(self):
79+
"""Check if Django tasks system is available"""
80+
try:
81+
import django
82+
83+
if django.VERSION < (6, 0):
84+
return False
85+
return True
86+
except (ImportError, AttributeError):
87+
return False

example/async_/tasks.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
try:
2+
from django.tasks import task
3+
except ImportError:
4+
# Define a fallback decorator
5+
def task(func=None, **kwargs):
6+
def decorator(f):
7+
return f
8+
9+
return decorator if func is None else decorator(func)
10+
11+
12+
@task
13+
def send_welcome_message(message):
14+
return f"Sent message: {message}"
15+
16+
17+
@task
18+
def generate_report(report_id):
19+
return f"Report {report_id} generated"

example/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22

3+
import django
34
from asgiref.sync import sync_to_async
45
from django.contrib.auth.models import User
56
from django.core.cache import cache
@@ -21,6 +22,13 @@ def jinja2_view(request):
2122

2223

2324
async def async_home(request):
25+
if django.VERSION >= (6, 0):
26+
from .async_.tasks import generate_report, send_welcome_message
27+
28+
# Queue some tasks
29+
send_welcome_message.enqueue(message="hi there")
30+
generate_report.enqueue(report_id=456)
31+
2432
return await sync_to_async(render)(request, "index.html")
2533

2634

0 commit comments

Comments
 (0)