@@ -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
0 commit comments