|
6 | 6 |
|
7 | 7 | use Algolia\AlgoliaSearch\Algolia;
|
8 | 8 | use Algolia\AlgoliaSearch\Configuration\IngestionConfig;
|
| 9 | +use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException; |
| 10 | +use Algolia\AlgoliaSearch\Exceptions\NotFoundException; |
9 | 11 | use Algolia\AlgoliaSearch\Model\Ingestion\Authentication;
|
10 | 12 | use Algolia\AlgoliaSearch\Model\Ingestion\AuthenticationCreate;
|
11 | 13 | use Algolia\AlgoliaSearch\Model\Ingestion\AuthenticationCreateResponse;
|
@@ -2824,6 +2826,76 @@ public function validateSourceBeforeUpdate($sourceID, $sourceUpdate, $requestOpt
|
2824 | 2826 | return $this->sendRequest('POST', $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions);
|
2825 | 2827 | }
|
2826 | 2828 |
|
| 2829 | + /** |
| 2830 | + * Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests. |
| 2831 | + * |
| 2832 | + * @param string $indexName the `indexName` to replace `objects` in |
| 2833 | + * @param array $objects the array of `objects` to store in the given Algolia `indexName` |
| 2834 | + * @param array $action the `batch` `action` to perform on the given array of `objects`, defaults to `addObject` |
| 2835 | + * @param bool $waitForTasks whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable |
| 2836 | + * @param array $batchSize The size of the chunk of `objects`. The number of `push` calls will be equal to `length(objects) / batchSize`. Defaults to 1000. |
| 2837 | + * @param array $referenceIndexName This is required when targeting an index that does not have a push connector setup (e.g. a tmp index), but you wish to attach another index's transformation to it (e.g. the source index name). |
| 2838 | + * @param array $requestOptions Request options |
| 2839 | + */ |
| 2840 | + public function chunkedPush( |
| 2841 | + $indexName, |
| 2842 | + $objects, |
| 2843 | + $action = 'addObject', |
| 2844 | + $waitForTasks = true, |
| 2845 | + $referenceIndexName = null, |
| 2846 | + $batchSize = 1000, |
| 2847 | + $requestOptions = [] |
| 2848 | + ) { |
| 2849 | + $responses = []; |
| 2850 | + $records = []; |
| 2851 | + $count = 0; |
| 2852 | + |
| 2853 | + foreach ($objects as $object) { |
| 2854 | + $records[] = $object; |
| 2855 | + |
| 2856 | + if (sizeof($records) === $batchSize || $count === sizeof($objects) - 1) { |
| 2857 | + $responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions); |
| 2858 | + $records = []; |
| 2859 | + } |
| 2860 | + |
| 2861 | + ++$count; |
| 2862 | + } |
| 2863 | + |
| 2864 | + if (!empty($records)) { |
| 2865 | + $responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions); |
| 2866 | + } |
| 2867 | + |
| 2868 | + if ($waitForTasks && !empty($responses)) { |
| 2869 | + $timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout'; |
| 2870 | + |
| 2871 | + foreach ($responses as $response) { |
| 2872 | + $this->waitForTask($indexName, $response['taskID']); |
| 2873 | + $retry = 0; |
| 2874 | + |
| 2875 | + while ($retry < 50) { |
| 2876 | + try { |
| 2877 | + $resp = $this->getEvent($response->runID, $response->eventID); |
| 2878 | + |
| 2879 | + break; |
| 2880 | + } catch (NotFoundException $e) { |
| 2881 | + if (404 === $e->getCode()) { |
| 2882 | + return null; |
| 2883 | + } |
| 2884 | + } |
| 2885 | + |
| 2886 | + ++$retry; |
| 2887 | + usleep( |
| 2888 | + call_user_func_array($timeoutCalculation, [$this->config->getWaitTaskTimeBeforeRetry(), $retry]) |
| 2889 | + ); |
| 2890 | + } |
| 2891 | + |
| 2892 | + throw new ExceededRetriesException('Maximum number of retries (50) exceeded.'); |
| 2893 | + } |
| 2894 | + } |
| 2895 | + |
| 2896 | + return $responses; |
| 2897 | + } |
| 2898 | + |
2827 | 2899 | private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions, $useReadTransporter = false)
|
2828 | 2900 | {
|
2829 | 2901 | if (!isset($requestOptions['headers'])) {
|
|
0 commit comments