Skip to content

Commit

Permalink
fix: clear error on successful index
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 30, 2024
1 parent 600debe commit 2658ce0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/Extensions/AlgoliaObjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,30 @@ public function doImmediateIndexInAlgolia(): bool
return false;
}


$schema = DataObject::getSchema();
$table = $schema->tableForField($this->owner->ClassName, 'AlgoliaError');
$indexer = Injector::inst()->get(AlgoliaIndexer::class);

try {
if ($indexer->indexItem($this->owner)) {
$this->touchAlgoliaIndexedDate();

DB::query(
sprintf(
'UPDATE %s SET AlgoliaError = \'\' WHERE ID = %s',
$table,
$this->owner->ID
)
);

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

$schema = DataObject::getSchema();
$table = $schema->tableForField($this->owner->ClassName, 'AlgoliaError');

DB::query(
sprintf(
'UPDATE %s SET AlgoliaError = \'%s\' WHERE ID = %s',
Expand Down
2 changes: 2 additions & 0 deletions src/Extensions/SubsitesVirtualPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function exportObjectToAlgolia($toIndex)
return $attributes;
});

$attributes->push('SubsiteID', $this->owner->SubsiteID);

return $result;
}
}
11 changes: 10 additions & 1 deletion src/Tasks/AlgoliaReindexItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use SilverStripe\CMS\Model\VirtualPage;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataObject;
use Wilr\SilverStripe\Algolia\Service\AlgoliaIndexer;

class AlgoliaReindexItem extends BuildTask
{
Expand Down Expand Up @@ -49,12 +51,19 @@ public function run($request)
$obj->assignAlgoliaUUID(true);
}

$indexer = Injector::inst()->get(AlgoliaIndexer::class);
$service = $indexer->getService();

echo 'Indexing to Algolia indexes (';
echo implode(', ', array_map(function ($indexName) use ($service) {
return $service->environmentizeIndex($indexName);
}, array_keys($service->initIndexes($obj)))) . ')' . PHP_EOL;

$result = $obj->doImmediateIndexInAlgolia();

echo sprintf(
'Indexed: %s%sUUID: %s%s%s',
$result ? 'true' : 'false',
$result ? 'true ' . '(timestamp ' . $obj->AlgoliaIndexed . ')' : 'false',
PHP_EOL,
$obj->AlgoliaUUID ? $obj->AlgoliaUUID : 'No ID set',
PHP_EOL,
Expand Down

0 comments on commit 2658ce0

Please sign in to comment.