From 865592d1f2d423b37e0419ef79c051747ca1a886 Mon Sep 17 00:00:00 2001 From: Matt Lang Date: Thu, 18 Jul 2024 11:39:37 +1200 Subject: [PATCH 1/2] fix use get instead of create for logger instances --- src/Extensions/AlgoliaObjectExtension.php | 4 ++-- src/Jobs/AlgoliaDeleteItemJob.php | 2 +- src/Service/AlgoliaIndexer.php | 2 +- src/Service/AlgoliaPageCrawler.php | 2 +- src/Service/AlgoliaService.php | 4 ++-- src/Tasks/AlgoliaReindex.php | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Extensions/AlgoliaObjectExtension.php b/src/Extensions/AlgoliaObjectExtension.php index 09dcd61..b35e261 100644 --- a/src/Extensions/AlgoliaObjectExtension.php +++ b/src/Extensions/AlgoliaObjectExtension.php @@ -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'); @@ -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; } diff --git a/src/Jobs/AlgoliaDeleteItemJob.php b/src/Jobs/AlgoliaDeleteItemJob.php index 09ee3c4..4ad6a66 100644 --- a/src/Jobs/AlgoliaDeleteItemJob.php +++ b/src/Jobs/AlgoliaDeleteItemJob.php @@ -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; diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index d14d3cb..0399dcb 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -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); } } diff --git a/src/Service/AlgoliaPageCrawler.php b/src/Service/AlgoliaPageCrawler.php index 14575fa..828e41a 100644 --- a/src/Service/AlgoliaPageCrawler.php +++ b/src/Service/AlgoliaPageCrawler.php @@ -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); diff --git a/src/Service/AlgoliaService.php b/src/Service/AlgoliaService.php index d6c5c01..fced815 100644 --- a/src/Service/AlgoliaService.php +++ b/src/Service/AlgoliaService.php @@ -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()); @@ -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; diff --git a/src/Tasks/AlgoliaReindex.php b/src/Tasks/AlgoliaReindex.php index 4ee5030..151a693 100644 --- a/src/Tasks/AlgoliaReindex.php +++ b/src/Tasks/AlgoliaReindex.php @@ -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()); From 78c67123a9d8dda00e7af36ea42a4bbd036d3db4 Mon Sep 17 00:00:00 2001 From: Matt Lang Date: Thu, 18 Jul 2024 11:41:24 +1200 Subject: [PATCH 2/2] fix error using class name for index name e.g. with the set up below it was giving the error "Index SilverStripe\CMS\Model\SiteTree not found, must be one of [Page]" indexes: Page: includeClasses: - SilverStripe\CMS\Model\SiteTree --- src/Jobs/AlgoliaReindexAllJob.php | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/Jobs/AlgoliaReindexAllJob.php b/src/Jobs/AlgoliaReindexAllJob.php index 2130ca7..0de985b 100644 --- a/src/Jobs/AlgoliaReindexAllJob.php +++ b/src/Jobs/AlgoliaReindexAllJob.php @@ -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]); } } }