@@ -80,7 +80,7 @@ pub struct OperationProcessor {
8080 running_tasks : HashMap < String , RunningTask > ,
8181 /// Completed results waiting to be collected
8282 completed_results : Vec < TaskResult > ,
83- task_result_receiver : Option < mpsc:: UnboundedReceiver < TaskResult > > ,
83+ task_result_receiver : mpsc:: UnboundedReceiver < TaskResult > ,
8484 task_result_sender : mpsc:: UnboundedSender < TaskResult > ,
8585}
8686
@@ -138,7 +138,7 @@ impl OperationProcessor {
138138 Self {
139139 running_tasks : HashMap :: new ( ) ,
140140 completed_results : Vec :: new ( ) ,
141- task_result_receiver : Some ( task_result_receiver ) ,
141+ task_result_receiver,
142142 task_result_sender,
143143 }
144144 }
@@ -195,18 +195,16 @@ impl OperationProcessor {
195195 }
196196
197197 /// Collect completed results from running tasks and remove them from the running tasks map.
198- pub fn collect_completed_results ( & mut self ) -> Vec < TaskResult > {
199- if let Some ( receiver) = & mut self . task_result_receiver {
200- while let Ok ( result) = receiver. try_recv ( ) {
201- self . running_tasks . remove ( & result. descriptor . operation_id ) ;
202- self . completed_results . push ( result) ;
203- }
198+ fn collect_completed_results ( & mut self ) {
199+ while let Ok ( result) = self . task_result_receiver . try_recv ( ) {
200+ self . running_tasks . remove ( & result. descriptor . operation_id ) ;
201+ self . completed_results . push ( result) ;
204202 }
205- std:: mem:: take ( & mut self . completed_results )
206203 }
207204
208205 /// Check for tasks that have exceeded their timeout and handle them appropriately.
209206 pub fn check_timeouts ( & mut self ) {
207+ self . collect_completed_results ( ) ;
210208 let now = std:: time:: Instant :: now ( ) ;
211209 let mut timed_out_tasks = Vec :: new ( ) ;
212210
@@ -231,7 +229,8 @@ impl OperationProcessor {
231229 }
232230
233231 /// Get the number of running tasks.
234- pub fn running_task_count ( & self ) -> usize {
232+ pub fn running_task_count ( & mut self ) -> usize {
233+ self . collect_completed_results ( ) ;
235234 self . running_tasks . len ( )
236235 }
237236
@@ -240,15 +239,19 @@ impl OperationProcessor {
240239 for ( _, task) in self . running_tasks . drain ( ) {
241240 task. task_handle . abort ( ) ;
242241 }
242+ while self . task_result_receiver . try_recv ( ) . is_ok ( ) { }
243243 self . completed_results . clear ( ) ;
244244 }
245+
245246 /// List running task ids.
246- pub fn list_running ( & self ) -> Vec < String > {
247+ pub fn list_running ( & mut self ) -> Vec < String > {
248+ self . collect_completed_results ( ) ;
247249 self . running_tasks . keys ( ) . cloned ( ) . collect ( )
248250 }
249251
250- /// Note: collectors should call collect_completed_results; this provides a snapshot of queued results.
251- pub fn peek_completed ( & self ) -> & [ TaskResult ] {
252+ /// Returns a snapshot of completed task results.
253+ pub fn peek_completed ( & mut self ) -> & [ TaskResult ] {
254+ self . collect_completed_results ( ) ;
252255 & self . completed_results
253256 }
254257
@@ -266,6 +269,7 @@ impl OperationProcessor {
266269
267270 /// Attempt to cancel a running task.
268271 pub fn cancel_task ( & mut self , task_id : & str ) -> bool {
272+ self . collect_completed_results ( ) ;
269273 if let Some ( task) = self . running_tasks . remove ( task_id) {
270274 task. task_handle . abort ( ) ;
271275 // Insert a cancelled result so callers can observe the terminal state.
@@ -281,6 +285,7 @@ impl OperationProcessor {
281285
282286 /// Retrieve a completed task result if available.
283287 pub fn take_completed_result ( & mut self , task_id : & str ) -> Option < TaskResult > {
288+ self . collect_completed_results ( ) ;
284289 if let Some ( position) = self
285290 . completed_results
286291 . iter ( )
0 commit comments