Skip to content

Commit 24d7fcd

Browse files
committed
MAGE-848 Force ReplicaState change on rebuilds
1 parent 284288d commit 24d7fcd

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

Console/Command/ReplicaRebuildCommand.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use Algolia\AlgoliaSearch\Exception\ReplicaLimitExceededException;
1111
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
1212
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
13+
use Algolia\AlgoliaSearch\Registry\ReplicaState;
1314
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
14-
use Magento\Framework\App\State;
15+
use Magento\Framework\App\State as AppState;
1516
use Magento\Framework\Console\Cli;
1617
use Magento\Store\Model\StoreManagerInterface;
1718
use Symfony\Component\Console\Input\InputInterface;
@@ -28,12 +29,13 @@ public function __construct(
2829
protected ProductHelper $productHelper,
2930
protected ReplicaManagerInterface $replicaManager,
3031
protected StoreManagerInterface $storeManager,
31-
State $state,
32+
protected ReplicaState $replicaState,
33+
AppState $appState,
3234
StoreNameFetcher $storeNameFetcher,
3335
?string $name = null
3436
)
3537
{
36-
parent::__construct($state, $storeNameFetcher, $name);
38+
parent::__construct($appState, $storeNameFetcher, $name);
3739
}
3840

3941
protected function getReplicaCommandName(): string
@@ -66,6 +68,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6668
$output->writeln($this->decorateOperationAnnouncementMessage('Rebuilding replicas for {{target}}', $storeIds));
6769

6870
$this->deleteReplicas($storeIds);
71+
$this->forceState($storeIds);
72+
6973
try {
7074
$this->syncReplicas($storeIds);
7175
} catch (ReplicaLimitExceededException $e) {
@@ -83,4 +87,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8387
return Cli::RETURN_SUCCESS;
8488
}
8589

90+
/**
91+
* Force the replica change state to always sync the replica configuration
92+
* Also serves to avoid latency from Algolia API when reading replica configuration for comparison with local Magento config
93+
* @param int[] $storeIds
94+
* @return void
95+
*/
96+
protected function forceState(array $storeIds): void
97+
{
98+
if (!count($storeIds)) {
99+
$storeIds = array_keys($this->storeManager->getStores());
100+
}
101+
foreach ($storeIds as $storeId) {
102+
$this->replicaState->setChangeState(ReplicaState::REPLICA_STATE_CHANGED, $storeId);
103+
}
104+
}
105+
86106
}

Service/Product/ReplicaManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,17 @@ protected function getBareIndexNameFromReplicaSetting(string $replicaSetting): s
351351

352352
/**
353353
* @param array $replicasToDelete
354+
* @param bool $waitLastTask
354355
* @return void
355356
* @throws AlgoliaException
356357
*/
357-
protected function deleteIndices(array $replicasToDelete): void
358+
protected function deleteIndices(array $replicasToDelete, bool $waitLastTask = false): void
358359
{
359360
foreach ($replicasToDelete as $deletedReplica) {
360361
$this->algoliaHelper->deleteIndex($deletedReplica);
362+
if ($waitLastTask) {
363+
$this->algoliaHelper->waitLastTask($deletedReplica);
364+
}
361365
}
362366
}
363367

Setup/Patch/Data/RebuildReplicasPatch.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Algolia\AlgoliaSearch\Setup\Patch\Data;
44

55
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
6+
use Algolia\AlgoliaSearch\Registry\ReplicaState;
67
use Algolia\AlgoliaSearch\Service\Product\ReplicaManager;
7-
use Magento\Framework\App\State;
8+
use Magento\Framework\App\State as AppState;
89
use Magento\Framework\Setup\ModuleDataSetupInterface;
910
use Magento\Framework\Setup\Patch\DataPatchInterface;
1011
use Magento\Framework\Setup\Patch\PatchInterface;
@@ -17,7 +18,8 @@ public function __construct(
1718
protected StoreManagerInterface $storeManager,
1819
protected ReplicaManager $replicaManager,
1920
protected ProductHelper $productHelper,
20-
protected State $state
21+
protected AppState $appState,
22+
protected ReplicaState $replicaState
2123
)
2224
{}
2325

@@ -45,14 +47,16 @@ public function getAliases(): array
4547
public function apply(): PatchInterface
4648
{
4749
$this->moduleDataSetup->getConnection()->startSetup();
48-
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
50+
$this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
4951

5052
$storeIds = array_keys($this->storeManager->getStores());
5153
// Delete all replicas before resyncing in case of incorrect replica assignments
5254
foreach ($storeIds as $storeId) {
5355
$this->replicaManager->deleteReplicasFromAlgolia($storeId);
5456
}
57+
5558
foreach ($storeIds as $storeId) {
59+
$this->replicaState->setChangeState(ReplicaState::REPLICA_STATE_CHANGED, $storeId); // avoids latency
5660
$this->replicaManager->syncReplicasToAlgolia($storeId, $this->productHelper->getIndexSettings($storeId));
5761
}
5862

0 commit comments

Comments
 (0)