@@ -2861,49 +2861,50 @@ public function chunkedPush(
2861
2861
$ responses = [];
2862
2862
$ records = [];
2863
2863
$ count = 0 ;
2864
+ $ offset = 0 ;
2865
+ $ waitBatchSize = (int ) ($ batchSize / 10 );
2866
+ if ($ waitBatchSize < 1 ) {
2867
+ $ waitBatchSize = $ batchSize ;
2868
+ }
2864
2869
2865
2870
foreach ($ objects as $ object ) {
2866
2871
$ records [] = $ object ;
2867
2872
$ ok = false ;
2873
+ ++$ count ;
2868
2874
2869
- if (sizeof ($ records ) === $ batchSize || $ count === sizeof ($ objects ) - 1 ) {
2875
+ if (sizeof ($ records ) === $ batchSize || $ count === sizeof ($ objects )) {
2870
2876
$ responses [] = $ this ->push ($ indexName , ['action ' => $ action , 'records ' => $ records ], false , $ referenceIndexName , $ requestOptions );
2871
2877
$ records = [];
2872
2878
}
2873
2879
2874
- ++ $ count;
2875
- }
2880
+ if ( $ waitForTasks && ! empty ( $ responses ) && ( 0 === sizeof ( $ responses ) % $ waitBatchSize || $ count === sizeof ( $ objects ))) {
2881
+ $ timeoutCalculation = ' Algolia\AlgoliaSearch\Support\Helpers::linearTimeout ' ;
2876
2882
2877
- if (!empty ($ records )) {
2878
- $ responses [] = $ this ->push ($ indexName , ['action ' => $ action , 'records ' => $ records ], false , $ referenceIndexName , $ requestOptions );
2879
- }
2883
+ foreach (array_slice ($ responses , $ offset , $ waitBatchSize ) as $ response ) {
2884
+ $ retry = 0 ;
2880
2885
2881
- if ($ waitForTasks && !empty ($ responses )) {
2882
- $ timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout ' ;
2886
+ while ($ retry < 50 ) {
2887
+ try {
2888
+ $ this ->getEvent ($ response ['runID ' ], $ response ['eventID ' ]);
2883
2889
2884
- foreach ($ responses as $ response ) {
2885
- $ retry = 0 ;
2890
+ $ ok = true ;
2886
2891
2887
- while ($ retry < 50 ) {
2888
- try {
2889
- $ this ->getEvent ($ response ['runID ' ], $ response ['eventID ' ]);
2892
+ break ;
2893
+ } catch (NotFoundException $ e ) {
2894
+ // just retry
2895
+ }
2890
2896
2891
- $ ok = true ;
2892
-
2893
- break ;
2894
- } catch (NotFoundException $ e ) {
2895
- // just retry
2897
+ ++$ retry ;
2898
+ usleep (
2899
+ call_user_func_array ($ timeoutCalculation , [$ this ->config ->getWaitTaskTimeBeforeRetry (), $ retry ])
2900
+ );
2896
2901
}
2897
2902
2898
- ++$ retry ;
2899
- usleep (
2900
- call_user_func_array ($ timeoutCalculation , [$ this ->config ->getWaitTaskTimeBeforeRetry (), $ retry ])
2901
- );
2902
- }
2903
-
2904
- if (false === $ ok ) {
2905
- throw new ExceededRetriesException ('Maximum number of retries (50) exceeded. ' );
2903
+ if (false === $ ok ) {
2904
+ throw new ExceededRetriesException ('Maximum number of retries (50) exceeded. ' );
2905
+ }
2906
2906
}
2907
+ $ offset = $ offset + $ waitBatchSize ;
2907
2908
}
2908
2909
}
2909
2910
0 commit comments