-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtasks.py
More file actions
80 lines (62 loc) · 2.36 KB
/
tasks.py
File metadata and controls
80 lines (62 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import functools
import django
from django.utils.translation import gettext_lazy as _, ngettext
from debug_toolbar.panels import Panel
class TasksPanel(Panel):
"""
Panel that displays Django tasks queued or executed during the
processing of the request.
"""
title = _("Tasks")
template = "debug_toolbar/panels/tasks.html"
is_async = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queued_tasks = []
@property
def nav_subtitle(self):
num_tasks = self.get_stats()["total_tasks"]
return ngettext(
"%(num_tasks)d task enqueued",
"%(num_tasks)d tasks enqueued",
num_tasks,
) % {"num_tasks": num_tasks}
def generate_stats(self, request, response):
stats = {"tasks": self.queued_tasks, "total_tasks": len(self.queued_tasks)}
self.record_stats(stats)
def enable_instrumentation(self):
"""Hook into task system to collect queued tasks"""
if self._tasks_available is False:
# Django tasks not available means that we cannot instrument
return
from django.tasks import Task
# Store original enqueue method
self._original_enqueue = Task.enqueue
@functools.wraps(self._original_enqueue)
def wrapped_enqueue(task, *args, **kwargs):
result = self._original_enqueue(task, *args, **kwargs).return_value
self._record_task(task, args, kwargs, result)
return result
Task.enqueue = wrapped_enqueue
def _record_task(self, task, args, kwargs, result):
"""Record a task that was queued"""
task_info = {
"name": getattr(task, "__name__", str(task)),
"args": repr(args) if args else "",
"kwargs": repr(kwargs) if kwargs else "",
}
self.queued_tasks.append(task_info)
def disable_instrumentation(self):
"""Restore original methods"""
try:
from django.tasks import Task
if hasattr(self, "_original_enqueue"):
Task.enqueue = self._original_enqueue
except (ImportError, AttributeError):
pass
@property
def _tasks_available(self) -> bool:
"""Check if Django tasks system is available"""
if django.VERSION < (6, 0):
return False
return True