@@ -66,7 +66,7 @@ def __init__(
6666 self .fn = fn
6767 self .tracker = tracker
6868 self ._name : str = kwargs .pop ('name' , fn .__name__ if fn else 'unknown_task' )
69- self ._callback_ : Union [Callable , Awaitable ] = kwargs .pop ('callback' , None )
69+ self ._user_callback : Union [Callable , Awaitable ] = kwargs .pop ('callback' , None )
7070 job_status = kwargs .pop ('status' , 'pending' )
7171 if job_status not in ['pending' , 'running' , 'done' , 'failed' ]:
7272 raise ValueError (
@@ -76,12 +76,11 @@ def __init__(
7676 self .jitter : float = jitter
7777 # Create the Job Record at status "pending"
7878 # generate a list of arguments accepted by JobRecord:
79- job_args = {}
79+ job_args = {
80+ k : v for k , v in kwargs .items ()
81+ if not k .startswith ('_' ) and k in JobRecord .__fields__
82+ }
8083 content = kwargs .pop ('content' , None )
81- for k , v in kwargs .items ():
82- if not k .startswith ('_' ):
83- if k in JobRecord .__fields__ :
84- job_args [k ] = v
8584 self .job_record : JobRecord = JobRecord (
8685 name = self ._name ,
8786 content = content ,
@@ -113,7 +112,28 @@ def add_callback(self, callback: Union[Callable, Awaitable]):
113112 - callback (Union[Callable, Awaitable]):
114113 Callback function to be called after the task is executed.
115114 """
116- self ._callback_ = callback
115+ self ._user_callback = callback
116+
117+ async def _wrapped_callback (self , result , exc , loop ):
118+ """
119+ Internal wrapper callback that injects JobRecord information
120+ before calling the user's callback.
121+
122+ Args:
123+ - result: The result of the task execution.
124+ - exc: Exception raised during task execution, if any.
125+ - loop: The event loop in which the task was executed.
126+ """
127+ if self ._user_callback :
128+ # Call user callback with additional JobRecord info
129+ # New signature: callback(result, exc, loop, job_record, task_id)
130+ await self ._user_callback (
131+ result ,
132+ exc ,
133+ loop ,
134+ job_record = self .job_record ,
135+ task_id = self .job_record .task_id
136+ )
117137
118138 async def __call__ (self ):
119139 result = None
@@ -166,7 +186,9 @@ async def _finish(result: Any, exc: Exception):
166186 return result
167187 with ThreadPoolExecutor (max_workers = 1 ) as executor :
168188 coro = self .fn (* self .args , ** self .kwargs )
169- coroutine_in_thread (coro , self ._callback_ , on_complete = _finish )
189+ # Use the wrapped callback instead of the user callback directly
190+ callback_to_use = self ._wrapped_callback if self ._user_callback else None
191+ coroutine_in_thread (coro , callback_to_use , on_complete = _finish )
170192 return {"status" : "running" }
171193 except asyncio .CancelledError :
172194 self .logger .warning (
0 commit comments