Skip to content

Commit 68e3228

Browse files
committed
MAGE-938 Implement replica rebuild operations via CLI
1 parent 8a09601 commit 68e3228

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

Console/Command/ReplicaRebuildCommand.php

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,88 @@
22

33
namespace Algolia\AlgoliaSearch\Console\Command;
44

5-
class ReplicaRebuildCommand extends AbstractReplicaCommand
5+
use Algolia\AlgoliaSearch\Api\Console\ReplicaDeleteCommandInterface;
6+
use Algolia\AlgoliaSearch\Api\Console\ReplicaSyncCommandInterface;
7+
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
8+
use Algolia\AlgoliaSearch\Console\Traits\ReplicaDeleteCommandTrait;
9+
use Algolia\AlgoliaSearch\Console\Traits\ReplicaSyncCommandTrait;
10+
use Algolia\AlgoliaSearch\Exception\ReplicaLimitExceededException;
11+
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
12+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
13+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
14+
use Magento\Framework\App\State;
15+
use Magento\Framework\Console\Cli;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
class ReplicaRebuildCommand
20+
extends AbstractReplicaCommand
21+
implements ReplicaSyncCommandInterface, ReplicaDeleteCommandInterface
622
{
23+
use ReplicaSyncCommandTrait;
24+
use ReplicaDeleteCommandTrait;
25+
26+
public function __construct(
27+
protected ReplicaManagerInterface $replicaManager,
28+
protected StoreNameFetcher $storeNameFetcher,
29+
protected ProductHelper $productHelper,
30+
State $state,
31+
?string $name = null
32+
)
33+
{
34+
parent::__construct($state, $name);
35+
}
36+
737
protected function getReplicaCommandName(): string
838
{
939
return 'rebuild';
1040
}
1141

12-
protected function getCommandDescription(): string {
42+
protected function getCommandDescription(): string
43+
{
1344
return 'Rebuild replica configuration for Magento sorting attributes';
1445
}
1546

1647
protected function getStoreArgumentDescription(): string
1748
{
18-
return 'ID(s) for store(s) to rebuild replicas';
49+
return 'ID(s) for store(s) to rebuild replicas (optional), if not specified all store replicas will be rebuilt';
1950
}
2051

2152
protected function getAdditionalDefinition(): array
2253
{
23-
return [];
54+
return [];
55+
}
56+
57+
protected function execute(InputInterface $input, OutputInterface $output): int
58+
{
59+
$this->output = $output;
60+
$this->setAreaCode();
61+
62+
$storeIds = $this->getStoreIds($input);
63+
64+
$msg = 'Rebuilding replicas for ' . ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
65+
if ($storeIds) {
66+
$output->writeln("<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>');
67+
} else {
68+
$output->writeln("<info>$msg</info>");
69+
}
70+
71+
$this->deleteReplicas($storeIds);
72+
try {
73+
$this->syncReplicas($storeIds);
74+
} catch (ReplicaLimitExceededException $e) {
75+
$this->output->writeln('<error>' . $e->getMessage() . '</error>');
76+
$this->output->writeln('<comment>Reduce the number of sorting attributes that have enabled virtual replicas and try again.</comment>');
77+
return CLI::RETURN_FAILURE;
78+
} catch (BadRequestException $e) {
79+
$this->output->writeln('<error>' . $e->getMessage() . '</error>');
80+
if ($storeIds) {
81+
$this->output->writeln('<comment>Your Algolia application may contain cris-crossed replicas. Try running "algolia:replicas:rebuild" for all stores to correct this.');
82+
}
83+
return CLI::RETURN_FAILURE;
84+
}
85+
86+
return Cli::RETURN_SUCCESS;
2487
}
2588

2689
}

0 commit comments

Comments
 (0)