@@ -36,22 +36,39 @@ pub enum ProcessEventError {
3636 BuildFilterError ( #[ from] BuildRindexerFilterError ) ,
3737}
3838
39- pub async fn process_event ( config : EventProcessingConfig ) -> Result < ( ) , ProcessEventError > {
39+ pub async fn process_event (
40+ config : EventProcessingConfig ,
41+ block_until_indexed : bool ,
42+ ) -> Result < ( ) , ProcessEventError > {
4043 debug ! ( "{} - Processing events" , config. info_log_name) ;
4144
42- process_event_logs ( Arc :: new ( config) , false ) . await ?;
45+ process_event_logs ( Arc :: new ( config) , false , block_until_indexed ) . await ?;
4346
4447 Ok ( ( ) )
4548}
4649
50+ /// note block_until_indexed:
51+ /// Whether to wait for all indexing tasks to complete for an event before returning
52+ // (needed for dependency indexing)
4753async fn process_event_logs (
4854 config : Arc < EventProcessingConfig > ,
4955 force_no_live_indexing : bool ,
56+ block_until_indexed : bool ,
5057) -> Result < ( ) , Box < ProviderError > > {
5158 let mut logs_stream = fetch_logs_stream ( Arc :: clone ( & config) , force_no_live_indexing) ;
59+ let mut tasks = Vec :: new ( ) ;
5260
5361 while let Some ( result) = logs_stream. next ( ) . await {
54- handle_logs_result ( Arc :: clone ( & config) , result)
62+ let task = handle_logs_result ( Arc :: clone ( & config) , result)
63+ . await
64+ . map_err ( |e| Box :: new ( ProviderError :: CustomError ( e. to_string ( ) ) ) ) ?;
65+
66+ tasks. push ( task) ;
67+ }
68+
69+ if block_until_indexed {
70+ // Wait for all tasks in parallel
71+ futures:: future:: try_join_all ( tasks)
5572 . await
5673 . map_err ( |e| Box :: new ( ProviderError :: CustomError ( e. to_string ( ) ) ) ) ?;
5774 }
@@ -153,7 +170,7 @@ async fn process_contract_events_with_dependencies(
153170 ) ) ?;
154171
155172 // forces live indexing off as it has to handle it a bit differently
156- process_event_logs ( Arc :: clone ( event_processing_config) , true ) . await ?;
173+ process_event_logs ( Arc :: clone ( event_processing_config) , true , true ) . await ?;
157174
158175 if event_processing_config. live_indexing {
159176 let rindexer_event_filter = event_processing_config. to_event_filter ( ) ?;
@@ -392,7 +409,18 @@ async fn live_indexing_for_contract_event_dependencies<'a>(
392409 . await ;
393410
394411 match result {
395- Ok ( _) => {
412+ Ok ( task) => {
413+ let complete = task. await ;
414+ if let Err ( e) = complete {
415+ error ! (
416+ "{} - {} - Error indexing task: {} - will try again in 200ms" ,
417+ & config. info_log_name,
418+ IndexingEventProgressStatus :: Live . log( ) ,
419+ e
420+ ) ;
421+ drop ( permit) ;
422+ break ;
423+ }
396424 ordering_live_indexing_details
397425 . last_seen_block_number = to_block;
398426 if logs_empty {
@@ -476,7 +504,7 @@ async fn live_indexing_for_contract_event_dependencies<'a>(
476504async fn handle_logs_result (
477505 config : Arc < EventProcessingConfig > ,
478506 result : Result < FetchLogsResult , Box < dyn std:: error:: Error + Send > > ,
479- ) -> Result < ( ) , Box < dyn std:: error:: Error + Send > > {
507+ ) -> Result < JoinHandle < ( ) > , Box < dyn std:: error:: Error + Send > > {
480508 match result {
481509 Ok ( result) => {
482510 debug ! ( "Processing logs {} - length {}" , config. event_name, result. logs. len( ) ) ;
@@ -495,18 +523,21 @@ async fn handle_logs_result(
495523 . collect :: < Vec < _ > > ( ) ;
496524
497525 if !fn_data. is_empty ( ) {
498- if config. index_event_in_order {
526+ return if config. index_event_in_order {
499527 config. trigger_event ( fn_data) . await ;
500528 update_progress_and_last_synced ( config, result. to_block ) ;
529+ Ok ( tokio:: spawn ( async { } ) ) // Return a completed task
501530 } else {
502- tokio:: spawn ( async move {
531+ let task = tokio:: spawn ( async move {
503532 config. trigger_event ( fn_data) . await ;
504533 update_progress_and_last_synced ( config, result. to_block ) ;
505534 } ) ;
535+
536+ Ok ( task)
506537 }
507538 }
508539
509- Ok ( ( ) )
540+ Ok ( tokio :: spawn ( async { } ) ) // Return a completed task
510541 }
511542 Err ( e) => {
512543 error ! ( "Error fetching logs: {:?}" , e) ;
0 commit comments