Skip to content

Commit

Permalink
ENGCOM-6533: #26206 Add information about currently reindexed index. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets authored Jan 6, 2020
2 parents 42e6d7a + a296099 commit 5bf78eb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
63 changes: 31 additions & 32 deletions app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

namespace Magento\Indexer\Console\Command;

use Magento\Framework\App\ObjectManagerFactory;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Indexer\Config\DependencyInfoProvider;
use Magento\Framework\Indexer\ConfigInterface;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Indexer\StateInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\Indexer\ConfigInterface;
use Magento\Framework\App\ObjectManagerFactory;

/**
* Command to run indexers
Expand Down Expand Up @@ -78,6 +78,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($this->getIndexers($input) as $indexer) {
try {
$this->validateIndexerStatus($indexer);

$output->write($indexer->getTitle() . ' index ');

$startTime = microtime(true);
$indexerConfig = $this->getConfig()->getIndexer($indexer->getId());
$sharedIndex = $indexerConfig['shared_index'];
Expand All @@ -90,17 +93,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
$resultTime = microtime(true) - $startTime;

$output->writeln(
$indexer->getTitle() . ' index has been rebuilt successfully in ' . gmdate('H:i:s', $resultTime)
__('has been rebuilt successfully in %time', ['time' => gmdate('H:i:s', $resultTime)])
);
$returnValue = Cli::RETURN_SUCCESS;
} catch (LocalizedException $e) {
$output->writeln($e->getMessage());
$output->writeln(__('exception: %message', ['message' => $e->getMessage()]));
} catch (\Exception $e) {
$output->writeln($indexer->getTitle() . ' indexer process unknown error:');
$output->writeln('process unknown error:');
$output->writeln($e->getMessage());

$output->writeln($e->getTraceAsString(), OutputInterface::VERBOSITY_DEBUG);
}
}

return $returnValue;
}

Expand All @@ -111,25 +118,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function getIndexers(InputInterface $input)
{
$indexers = parent::getIndexers($input);
$indexers = parent::getIndexers($input);
$allIndexers = $this->getAllIndexers();
if (!array_diff_key($allIndexers, $indexers)) {
return $indexers;
}

$relatedIndexers = [];
$dependentIndexers = [];
$relatedIndexers = [[]];
$dependentIndexers = [[]];

foreach ($indexers as $indexer) {
$relatedIndexers = array_merge(
$relatedIndexers,
$this->getRelatedIndexerIds($indexer->getId())
);
$dependentIndexers = array_merge(
$dependentIndexers,
$this->getDependentIndexerIds($indexer->getId())
);
$relatedIndexers[] = $this->getRelatedIndexerIds($indexer->getId());
$dependentIndexers[] = $this->getDependentIndexerIds($indexer->getId());
}

$relatedIndexers = array_merge(...$relatedIndexers);
$dependentIndexers = array_merge(...$dependentIndexers);

$invalidRelatedIndexers = [];
foreach (array_unique($relatedIndexers) as $relatedIndexer) {
if ($allIndexers[$relatedIndexer]->isInvalid()) {
Expand Down Expand Up @@ -157,18 +162,15 @@ protected function getIndexers(InputInterface $input)
* @param string $indexerId
* @return array
*/
private function getRelatedIndexerIds(string $indexerId)
private function getRelatedIndexerIds(string $indexerId): array
{
$relatedIndexerIds = [];
$relatedIndexerIds = [[]];
foreach ($this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($indexerId) as $relatedIndexerId) {
$relatedIndexerIds = array_merge(
$relatedIndexerIds,
[$relatedIndexerId],
$this->getRelatedIndexerIds($relatedIndexerId)
);
$relatedIndexerIds[] = [$relatedIndexerId];
$relatedIndexerIds[] = $this->getRelatedIndexerIds($relatedIndexerId);
}

return array_unique($relatedIndexerIds);
return array_unique(array_merge(...$relatedIndexerIds));
}

/**
Expand All @@ -177,21 +179,18 @@ private function getRelatedIndexerIds(string $indexerId)
* @param string $indexerId
* @return array
*/
private function getDependentIndexerIds(string $indexerId)
private function getDependentIndexerIds(string $indexerId): array
{
$dependentIndexerIds = [];
$dependentIndexerIds = [[]];
foreach (array_keys($this->getConfig()->getIndexers()) as $id) {
$dependencies = $this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($id);
if (array_search($indexerId, $dependencies) !== false) {
$dependentIndexerIds = array_merge(
$dependentIndexerIds,
[$id],
$this->getDependentIndexerIds($id)
);
$dependentIndexerIds[] = [$id];
$dependentIndexerIds[] = $this->getDependentIndexerIds($id);
}
}

return array_unique($dependentIndexerIds);
return array_unique(array_merge(...$dependentIndexerIds));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
class IndexerReindexCommandTest extends AbstractIndexerCommandCommonSetup
{
const STUB_INDEXER_NAME = 'Indexer Name';
/**
* Command being tested
*
Expand Down Expand Up @@ -107,7 +108,7 @@ public function testExecuteAll()
[
$this->getIndexerMock(
['reindexAll', 'getStatus'],
['indexer_id' => 'id_indexerOne', 'title' => 'Title_indexerOne']
['indexer_id' => 'id_indexerOne', 'title' => self::STUB_INDEXER_NAME]
)
]
);
Expand All @@ -117,7 +118,10 @@ public function testExecuteAll()
$commandTester->execute([]);
$actualValue = $commandTester->getDisplay();
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
$this->assertStringStartsWith('Title_indexerOne index has been rebuilt successfully in', $actualValue);
$this->assertStringStartsWith(
self::STUB_INDEXER_NAME . ' index has been rebuilt successfully in',
$actualValue
);
}

/**
Expand Down Expand Up @@ -174,6 +178,7 @@ public function testExecuteWithIndex(
$this->objectManagerFactory,
$this->indexerRegistryMock
);

$commandTester = new CommandTester($this->command);
$commandTester->execute(['index' => $inputIndexers]);
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
Expand Down Expand Up @@ -344,7 +349,8 @@ public function executeWithIndexDataProvider()
],
'With dependencies and multiple indexers in request' => [
'inputIndexers' => [
'indexer_1', 'indexer_3'
'indexer_1',
'indexer_3'
],
'indexers' => [
'indexer_2' => [
Expand Down Expand Up @@ -405,7 +411,10 @@ public function executeWithIndexDataProvider()
public function testExecuteWithLocalizedException()
{
$this->configureAdminArea();
$indexerOne = $this->getIndexerMock(['reindexAll', 'getStatus'], ['indexer_id' => 'indexer_1']);
$indexerOne = $this->getIndexerMock(
['reindexAll', 'getStatus'],
['indexer_id' => 'indexer_1', 'title' => self::STUB_INDEXER_NAME]
);
$localizedException = new LocalizedException(new Phrase('Some Exception Message'));
$indexerOne->expects($this->once())->method('reindexAll')->will($this->throwException($localizedException));
$this->initIndexerCollectionByItems([$indexerOne]);
Expand All @@ -414,7 +423,10 @@ public function testExecuteWithLocalizedException()
$commandTester->execute(['index' => ['indexer_1']]);
$actualValue = $commandTester->getDisplay();
$this->assertSame(Cli::RETURN_FAILURE, $commandTester->getStatusCode());
$this->assertStringStartsWith('Some Exception Message', $actualValue);
$this->assertStringStartsWith(
self::STUB_INDEXER_NAME . ' index exception: Some Exception Message',
$actualValue
);
}

public function testExecuteWithException()
Expand All @@ -433,7 +445,7 @@ public function testExecuteWithException()
$commandTester->execute(['index' => ['indexer_1']]);
$actualValue = $commandTester->getDisplay();
$this->assertSame(Cli::RETURN_FAILURE, $commandTester->getStatusCode());
$this->assertStringStartsWith('Title_indexer_1' . ' indexer process unknown error:', $actualValue);
$this->assertStringStartsWith('Title_indexer_1' . ' index process unknown error:', $actualValue);
}

public function testExecuteWithExceptionInGetIndexers()
Expand Down

0 comments on commit 5bf78eb

Please sign in to comment.