Skip to content

Commit

Permalink
periodically check reindex status
Browse files Browse the repository at this point in the history
  • Loading branch information
AlternateIf authored Dec 2, 2024
1 parent 089274d commit 34fe702
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
abstract class AbstractOpenSearch extends ProductCentricBatchProcessingWorker implements IndexRefreshInterface
{
const REINDEX_TIMEOUT = 200;
const STORE_TABLE_NAME = 'ecommerceframework_productindex_store_opensearch';

const RELATION_FIELD = 'parentchildrelation';
Expand Down Expand Up @@ -868,7 +869,7 @@ protected function getNextIndexVersion(): int
*
* @param string $sourceIndexName the name of the source index in ES.
* @param string $targetIndexName the name of the target index in ES. If existing, will be deleted
*
* @throws \Exception
*/
protected function performReindex(string $sourceIndexName, string $targetIndexName): void
{
Expand Down Expand Up @@ -897,10 +898,34 @@ protected function performReindex(string $sourceIndexName, string $targetIndexNa
'body' => $body,
]);

$osClient->reindex([
// in case of long running reindexing this might lead to Gateway Timeout of Opensearch. Due to that
// query without waiting for completion and check task status periodically
$result = $osClient->reindex([
'body' => $body,
"wait_for_completion" => false,
]);

$taskId = $result['task'];
$taskResponse = null;
for ($checks = 1; $checks <= self::REINDEX_TIMEOUT; $checks++) {
sleep(15);
Logger::info('Waiting for reindex to finish. ' . $checks . '/' . self::REINDEX_TIMEOUT);
// query task status
$taskResponse = $osClient->tasks()->get(['task_id' => $taskId]);
// if task was completed delete it to not fill up index for tasks
if (isset($taskResponse['completed']) && $taskResponse['completed']) {
$osClient->delete([
'index' => '.tasks',
'id' => $taskId,
]);
break;
}
}

if ($taskResponse === null || !isset($taskResponse['completed']) || $taskResponse['completed'] === false) {

Check failure on line 925 in src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Strict comparison using === between array and null will always evaluate to false.

Check failure on line 925 in src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, highest, false)

Strict comparison using === between array and null will always evaluate to false.
throw new \Exception('reindex is not finished. Cleanup task index for task ' . $taskId);
}

Logger::info(sprintf('Completed re-index in %.02f seconds.', (time() - $startTime)));
}

Expand Down

0 comments on commit 34fe702

Please sign in to comment.