@@ -215,30 +215,33 @@ pub mod pallet {
215
215
/// This function will look for any pending scheduled tasks that can
216
216
/// be executed and will process them.
217
217
fn on_idle ( now : T :: BlockNumber , mut remaining_weight : Weight ) -> Weight {
218
- const MAX_TASKS_TO_PROCESS : usize = 10 ;
218
+ const MAX_TASKS_TO_PROCESS : usize = 5 ;
219
219
// reduce the weight used to read the task list
220
220
remaining_weight = remaining_weight. saturating_sub ( T :: WeightInfo :: remove_task ( ) ) ;
221
+ let cancel_weight = T :: WeightInfo :: cancel ( ) ;
222
+
223
+ // calculate count of tasks that can be processed with remaining weight
224
+ let possible_task_count: usize = remaining_weight
225
+ . saturating_div ( cancel_weight)
226
+ . try_into ( )
227
+ . unwrap_or ( MAX_TASKS_TO_PROCESS ) ;
221
228
222
229
ScheduledTasks :: < T > :: mutate ( |tasks| {
223
230
let mut task_list: Vec < _ > = tasks
224
231
. clone ( )
225
232
. into_iter ( )
233
+ . take ( possible_task_count)
226
234
// leave out tasks in the future
227
235
. filter ( |( _, ScheduledTask { when, task } ) | when <= & now && matches ! ( task, Task :: Cancel ) )
228
236
. collect ( ) ;
229
237
230
- // limit the max tasks to reduce computation
231
- task_list. truncate ( MAX_TASKS_TO_PROCESS ) ;
232
-
233
238
// order by oldest task to process
234
239
task_list. sort_by ( |( _, t) , ( _, x) | x. when . cmp ( & t. when ) ) ;
235
240
236
- let cancel_weight = T :: WeightInfo :: cancel ( ) ;
237
-
238
241
while !task_list. is_empty ( ) && remaining_weight >= cancel_weight {
239
242
if let Some ( ( account_pair, _) ) = task_list. pop ( ) {
240
243
remaining_weight = remaining_weight. saturating_sub ( cancel_weight) ;
241
- // remove the task form the tasks
244
+ // remove the task form the tasks storage
242
245
tasks. remove ( & account_pair) ;
243
246
244
247
// process the cancel payment
0 commit comments