@@ -52,7 +52,6 @@ class DojoAsyncTask(Task):
5252 This class:
5353 - Injects user context into task kwargs
5454 - Tracks task calls for performance testing
55- - Handles sync/async execution based on user settings
5655 - Supports all Celery features (signatures, chords, groups, chains)
5756 """
5857
@@ -68,97 +67,18 @@ def apply_async(self, args=None, kwargs=None, **options):
6867 if "async_user" not in kwargs :
6968 kwargs ["async_user" ] = get_current_user ()
7069
71- # Track task call (only if not already tracked by __call__)
72- # Check if this is a direct call to apply_async (not from __call__)
73- # by checking if _dojo_tracked is not set
74- if not getattr (self , "_dojo_tracked" , False ):
75- dojo_async_task_counter .incr (
76- self .name ,
77- args = args ,
78- kwargs = kwargs ,
79- )
70+ # Control flag used for sync/async decision; never pass into the task itself
71+ kwargs .pop ("sync" , None )
8072
81- # Call parent to execute async
82- return super ().apply_async (args = args , kwargs = kwargs , ** options )
83-
84- def s (self , * args , ** kwargs ):
85- """Create a mutable signature with injected user context."""
86- from dojo .decorators import dojo_async_task_counter # noqa: PLC0415 circular import
87- from dojo .utils import get_current_user # noqa: PLC0415 circular import
88-
89- if "async_user" not in kwargs :
90- kwargs ["async_user" ] = get_current_user ()
91-
92- # Track task call
93- dojo_async_task_counter .incr (
94- self .name ,
95- args = args ,
96- kwargs = kwargs ,
97- )
98-
99- return super ().s (* args , ** kwargs )
100-
101- def si (self , * args , ** kwargs ):
102- """Create an immutable signature with injected user context."""
103- from dojo .decorators import dojo_async_task_counter # noqa: PLC0415 circular import
104- from dojo .utils import get_current_user # noqa: PLC0415 circular import
105-
106- if "async_user" not in kwargs :
107- kwargs ["async_user" ] = get_current_user ()
108-
109- # Track task call
110- dojo_async_task_counter .incr (
111- self .name ,
112- args = args ,
113- kwargs = kwargs ,
114- )
115-
116- return super ().si (* args , ** kwargs )
117-
118- def __call__ (self , * args , ** kwargs ):
119- """
120- Override __call__ to handle direct task calls with sync/async logic.
121-
122- This replicates the behavior of the dojo_async_task decorator wrapper.
123- """
124- # In Celery worker execution, __call__ is how tasks actually run.
125- # We only want the sync/async decision when tasks are called directly
126- # from application code (task(...)), not when the worker is executing a message.
127- if not getattr (self .request , "called_directly" , True ):
128- return super ().__call__ (* args , ** kwargs )
129-
130- from dojo .decorators import dojo_async_task_counter , we_want_async # noqa: PLC0415 circular import
131- from dojo .utils import get_current_user # noqa: PLC0415 circular import
132-
133- # Inject user context if not already present
134- if "async_user" not in kwargs :
135- kwargs ["async_user" ] = get_current_user ()
136-
137- # Track task call
73+ # Track dispatch
13874 dojo_async_task_counter .incr (
13975 self .name ,
14076 args = args ,
14177 kwargs = kwargs ,
14278 )
14379
144- # Extract countdown if present (don't pass to sync execution)
145- countdown = kwargs .pop ("countdown" , 0 )
146-
147- # Check if we should run async or sync
148- if we_want_async (* args , func = self , ** kwargs ):
149- # Mark as tracked to avoid double tracking in apply_async
150- self ._dojo_tracked = True
151- try :
152- # Run asynchronously
153- return self .apply_async (args = args , kwargs = kwargs , countdown = countdown )
154- finally :
155- # Clean up the flag
156- delattr (self , "_dojo_tracked" )
157- else :
158- # Run synchronously in-process, matching the original decorator behavior: func(*args, **kwargs)
159- # Remove sync from kwargs as it's a control flag, not a task argument.
160- kwargs .pop ("sync" , None )
161- return self .run (* args , ** kwargs )
80+ # Call parent to execute async
81+ return super ().apply_async (args = args , kwargs = kwargs , ** options )
16282
16383
16484@app .task (bind = True )
0 commit comments