Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReindexAllJob error using class name for index name #71

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this diff looks so ugly, all that changed was I wrapped the meat of this in

foreach ($algoliaService->indexes as $indexName => $index) {
  $classes = (isset($index['includeClasses'])) ? $index['includeClasses'] : [];

  if (!in_array($class, $classes)) {
    continue;
  }

And changed the indexItems to take $indexName instead of $class

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
Loading