Skip to content

Commit a03b752

Browse files
new test case with stats
1 parent db464ef commit a03b752

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

dojo/decorators.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import threading
23
from functools import wraps
34

45
from django.conf import settings
@@ -12,6 +13,47 @@
1213
logger = logging.getLogger(__name__)
1314

1415

16+
class ThreadLocalTaskCounter:
17+
def __init__(self):
18+
self._thread_local = threading.local()
19+
20+
def _get_task_list(self):
21+
if not hasattr(self._thread_local, "tasks"):
22+
self._thread_local.tasks = []
23+
return self._thread_local.tasks
24+
25+
def _get_recording(self):
26+
return getattr(self._thread_local, "recording", False)
27+
28+
def start(self):
29+
self._thread_local.recording = True
30+
self._get_task_list().clear()
31+
32+
def stop(self):
33+
self._thread_local.recording = False
34+
35+
def incr(self, task_name, model_id=None, args=None, kwargs=None):
36+
if not self._get_recording():
37+
return
38+
tasks = self._get_task_list()
39+
tasks.append({
40+
"task": task_name,
41+
"id": model_id,
42+
"args": args if args is not None else [],
43+
"kwargs": kwargs if kwargs is not None else {},
44+
})
45+
46+
def get(self):
47+
return len(self._get_task_list())
48+
49+
def get_tasks(self):
50+
return list(self._get_task_list())
51+
52+
53+
# Create a shared instance
54+
dojo_async_task_counter = ThreadLocalTaskCounter()
55+
56+
1557
def we_want_async(*args, func=None, **kwargs):
1658
from dojo.models import Dojo_User
1759
from dojo.utils import get_current_user
@@ -40,6 +82,13 @@ def __wrapper__(*args, **kwargs):
4082
from dojo.utils import get_current_user
4183
user = get_current_user()
4284
kwargs["async_user"] = user
85+
86+
dojo_async_task_counter.incr(
87+
func.__name__,
88+
args=args,
89+
kwargs=kwargs,
90+
)
91+
4392
countdown = kwargs.pop("countdown", 0)
4493
if we_want_async(*args, func=func, **kwargs):
4594
return func.apply_async(args=args, kwargs=kwargs, countdown=countdown)

0 commit comments

Comments
 (0)