Skip to content

Commit e8f2b0c

Browse files
committed
Merge branch 'MAGETWO-51540' of github.corp.magento.com:magento-troll/magento2ce into MAGETWO-51814
2 parents 52097fd + 0bea076 commit e8f2b0c

File tree

2 files changed

+97
-15
lines changed

2 files changed

+97
-15
lines changed

app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
*/
1717
class IndexerReindexCommand extends AbstractIndexerManageCommand
1818
{
19+
/**
20+
* @var array
21+
*/
22+
private $sharedIndexesComplete = [];
23+
24+
/**
25+
* @var \Magento\Framework\Indexer\ConfigInterface
26+
*/
27+
private $config;
28+
1929
/**
2030
* {@inheritdoc}
2131
*/
@@ -34,21 +44,18 @@ protected function configure()
3444
protected function execute(InputInterface $input, OutputInterface $output)
3545
{
3646
$indexers = $this->getIndexers($input);
37-
$config = $this->getConfig();
38-
$sharedIndexesComplete = [];
3947
foreach ($indexers as $indexer) {
4048
try {
4149
$startTime = microtime(true);
42-
$indexerConfig = $config->getIndexer($indexer->getId());
50+
$indexerConfig = $this->getConfig()->getIndexer($indexer->getId());
51+
$sharedIndex = $indexerConfig['shared_index'];
4352

4453
// Skip indexers having shared index that was already complete
45-
if (!in_array($indexerConfig['shared_index'], $sharedIndexesComplete)) {
54+
if (!in_array($sharedIndex, $this->sharedIndexesComplete)) {
4655
$indexer->reindexAll();
47-
} else {
48-
$indexer->getState()->setStatus(StateInterface::STATUS_VALID)->save();
49-
}
50-
if ($indexerConfig['shared_index']) {
51-
$sharedIndexesComplete[] = $indexerConfig['shared_index'];
56+
if ($sharedIndex) {
57+
$this->validateSharedIndex($sharedIndex);
58+
}
5259
}
5360
$resultTime = microtime(true) - $startTime;
5461
$output->writeln(
@@ -63,6 +70,54 @@ protected function execute(InputInterface $input, OutputInterface $output)
6370
}
6471
}
6572

73+
/**
74+
* Get indexer ids that have common shared index
75+
*
76+
* @param $sharedIndex
77+
* @return array
78+
*/
79+
private function getIndexerIdsBySharedIndex($sharedIndex)
80+
{
81+
$indexers = $this->getConfig()->getIndexers();
82+
$result = [];
83+
foreach ($indexers as $indexerCode => $indexerConfig) {
84+
if ($indexerConfig['shared_index'] == $sharedIndex) {
85+
$result[] = $indexerConfig['indexer_id'];
86+
}
87+
}
88+
return $result;
89+
}
90+
91+
/**
92+
* Validate indexers by shared index ID
93+
*
94+
* @param $sharedIndex
95+
* @return $this
96+
*/
97+
private function validateSharedIndex($sharedIndex)
98+
{
99+
if (empty($sharedIndex)) {
100+
throw new \InvalidArgumentException('sharedIndex must be a valid shared index identifier');
101+
}
102+
$indexerIds = $this->getIndexerIdsBySharedIndex($sharedIndex);
103+
if (empty($indexerIds)) {
104+
return $this;
105+
}
106+
$indexerFactory = $this->getObjectManager()->create('Magento\Indexer\Model\IndexerFactory');
107+
foreach ($indexerIds as $indexerId) {
108+
/** @var \Magento\Indexer\Model\Indexer $indexer */
109+
$indexer = $indexerFactory->create();
110+
$indexer->load($indexerId);
111+
/** @var \Magento\Indexer\Model\Indexer\State $state */
112+
$state = $indexer->getState();
113+
$state->setStatus(\Magento\Framework\Indexer\StateInterface::STATUS_VALID);
114+
$state->save();
115+
}
116+
$this->sharedIndexesComplete[] = $sharedIndex;
117+
return $this;
118+
}
119+
120+
66121
/**
67122
* Get config
68123
*
@@ -71,6 +126,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
71126
*/
72127
private function getConfig()
73128
{
74-
return $this->getObjectManager()->get(ConfigInterface::class);
129+
if (!$this->config) {
130+
$this->config = $this->getObjectManager()->get(ConfigInterface::class);
131+
}
132+
return $this->config;
75133
}
76134
}

app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerReindexCommandTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,60 @@ public function testExecuteWithIndex()
8686
['id_indexer3', ['title' => 'Title_indexer3', 'shared_index' => 'with_indexer_3']]
8787
]
8888
));
89+
$this->configMock->expects($this->any())
90+
->method('getIndexers')
91+
->will($this->returnValue(
92+
[
93+
'id_indexerOne' => [
94+
'indexer_id' => 'id_indexerOne',
95+
'title' => 'Title_indexerOne',
96+
'shared_index' => null
97+
],
98+
'id_indexerTwo' => [
99+
'indexer_id' => 'id_indexerTwo',
100+
'title' => 'Title_indexerTwo',
101+
'shared_index' => 'with_indexer_3'
102+
],
103+
'id_indexer3' => [
104+
'indexer_id' => 'id_indexer3',
105+
'title' => 'Title_indexer3',
106+
'shared_index' => 'with_indexer_3'
107+
]
108+
]
109+
));
89110

90111
$this->configureAdminArea();
91112
$indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
92113
$indexerOne->expects($this->once())->method('reindexAll');
93114
$indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne');
94115
$indexerOne->expects($this->any())->method('getId')->willReturn('id_indexerOne');
95-
$indexerOne->expects($this->once())->method('load')->with('id_indexerOne')->willReturn($indexerOne);
116+
$indexerOne->expects($this->any())->method('load')->with('id_indexerOne')->willReturn($indexerOne);
96117

97118
$indexerTwo = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
98119
$indexerTwo->expects($this->once())->method('reindexAll');
99120
$indexerTwo->expects($this->once())->method('getTitle')->willReturn('Title_indexerTwo');
100121
$indexerTwo->expects($this->any())->method('getId')->willReturn('id_indexerTwo');
101-
$indexerTwo->expects($this->once())->method('load')->with('id_indexerTwo')->willReturn($indexerTwo);
122+
$indexerTwo->expects($this->any())->method('load')->with('id_indexerTwo')->willReturn($indexerTwo);
102123

103124
$indexer3 = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false);
104125
$indexer3->expects($this->never())->method('reindexAll');
105126
$indexer3->expects($this->once())->method('getTitle')->willReturn('Title_indexer3');
106127
$indexer3->expects($this->any())->method('getId')->willReturn('id_indexer3');
107-
$indexer3->expects($this->once())->method('load')->with('id_indexer3')->willReturn($indexer3);
128+
$indexer3->expects($this->any())->method('load')->with('id_indexer3')->willReturn($indexer3);
108129

109130
$stateMock = $this->getMock(\Magento\Indexer\Model\Indexer\State::class, [], [], '', false);
110-
$stateMock->expects($this->once())->method('setStatus')->will($this->returnSelf());
111-
$stateMock->expects($this->once())->method('save');
131+
$stateMock->expects($this->exactly(2))->method('setStatus')->will($this->returnSelf());
132+
$stateMock->expects($this->exactly(2))->method('save');
112133

113134
$indexer3->expects($this->once())->method('getState')->willReturn($stateMock);
135+
$indexerTwo->expects($this->once())->method('getState')->willReturn($stateMock);
114136

115137
$this->collectionFactory->expects($this->never())->method('create');
116138
$this->indexerFactory->expects($this->at(0))->method('create')->willReturn($indexerOne);
117139
$this->indexerFactory->expects($this->at(1))->method('create')->willReturn($indexerTwo);
118140
$this->indexerFactory->expects($this->at(2))->method('create')->willReturn($indexer3);
141+
$this->indexerFactory->expects($this->at(3))->method('create')->willReturn($indexerTwo);
142+
$this->indexerFactory->expects($this->at(4))->method('create')->willReturn($indexer3);
119143

120144
$this->command = new IndexerReindexCommand($this->objectManagerFactory);
121145
$commandTester = new CommandTester($this->command);

0 commit comments

Comments
 (0)