Skip to content

Commit

Permalink
Merge pull request #71 from matt-in-a-hat/fix-reindex-all-job-index-n…
Browse files Browse the repository at this point in the history
…ame-error

Fix ReindexAllJob error using class name for index name
  • Loading branch information
wilr authored Jul 18, 2024
2 parents 1fe0229 + 78c6712 commit c584e2a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/Extensions/AlgoliaObjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function doImmediateIndexInAlgolia(): bool
return true;
}
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);

$schema = DataObject::getSchema();
$table = $schema->tableForField($this->owner->ClassName, 'AlgoliaError');
Expand Down Expand Up @@ -291,7 +291,7 @@ public function removeFromAlgolia(): bool

$this->markAsRemovedFromAlgoliaIndex();
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/AlgoliaDeleteItemJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function process()
$indexer = Injector::inst()->create(AlgoliaIndexer::class);
$indexer->deleteItem($this->itemClass, $this->itemUUID);
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);
}

$this->isComplete = true;
Expand Down
65 changes: 37 additions & 28 deletions src/Jobs/AlgoliaReindexAllJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,49 +104,58 @@ public function process()
return;
}

$algoliaService = Injector::inst()->create(AlgoliaService::class);
$task = new AlgoliaReindex();

$batchSize = $task->config()->get('batch_size');
$batching = $this->config()->get('use_batching');

foreach ($remainingChildren as $class => $ids) {
$take = array_slice($ids, 0, $batchSize);
$this->indexData[$class] = array_slice($ids, $batchSize);
foreach ($algoliaService->indexes as $indexName => $index) {
$classes = (isset($index['includeClasses'])) ? $index['includeClasses'] : [];

if (!empty($take)) {
$this->currentStep += count($take);
$errors = [];
if (!in_array($class, $classes)) {
continue;
}

try {
if ($batching) {
if ($task->indexItems($class, DataObject::get($class)->filter('ID', $take), false)) {
$this->addMessage('Successfully indexing ' . $class . ' [' . implode(', ', $take) . ']');
} else {
$this->addMessage('Error indexing ' . $class . ' [' . implode(', ', $take) . ']');
}
} else {
$items = DataObject::get($class)->filter('ID', $take);
$take = array_slice($ids, 0, $batchSize);
$this->indexData[$class] = array_slice($ids, $batchSize);

if (!empty($take)) {
$this->currentStep += count($take);
$errors = [];

foreach ($items as $item) {
if ($task->indexItem($item)) {
$this->addMessage('Successfully indexed ' . $class . ' [' . $item->ID . ']');
try {
if ($batching) {
if ($task->indexItems($indexName, DataObject::get($class)->filter('ID', $take), false)) {
$this->addMessage('Successfully indexing ' . $class . ' [' . implode(', ', $take) . ']');
} else {
$this->addMessage('Error indexing ' . $class . ' [' . $item->ID . ']');
$this->addMessage('Error indexing ' . $class . ' [' . implode(', ', $take) . ']');
}
} else {
$items = DataObject::get($class)->filter('ID', $take);

foreach ($items as $item) {
if ($task->indexItem($item)) {
$this->addMessage('Successfully indexed ' . $class . ' [' . $item->ID . ']');
} else {
$this->addMessage('Error indexing ' . $class . ' [' . $item->ID . ']');
}
}
}
}

$errors = $task->getErrors();
} catch (Throwable $e) {
$errors[] = $e->getMessage();
}
$errors = $task->getErrors();
} catch (Throwable $e) {
$errors[] = $e->getMessage();
}

if (!empty($errors)) {
$this->addMessage(implode(', ', $errors));
$task->clearErrors();
if (!empty($errors)) {
$this->addMessage(implode(', ', $errors));
$task->clearErrors();
}
} else {
unset($this->indexData[$class]);
}
} else {
unset($this->indexData[$class]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/AlgoliaIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function exportAttributesFromRelationship($item, $relationship, $attribut

$attributes->push($relationship, $data);
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/AlgoliaPageCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getMainContent(): string
}
}
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);
}

SSViewer::set_themes($oldThemes);
Expand Down
4 changes: 2 additions & 2 deletions src/Service/AlgoliaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function initIndexes($item = null, $excludeReplicas = true)
return [];
}
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);

if (Director::isDev()) {
Debug::message($e->getMessage());
Expand Down Expand Up @@ -277,7 +277,7 @@ function ($replica) {

$index->setSettings($data['indexSettings']);
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);


return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/AlgoliaReindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function indexBatch($indexName, $items): bool

return true;
} catch (Throwable $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);
Injector::inst()->get(LoggerInterface::class)->error($e);

if (Director::isDev()) {
Debug::message($e->getMessage());
Expand Down

0 comments on commit c584e2a

Please sign in to comment.