Skip to content

Commit

Permalink
Merge pull request #1416 from magento-helix/MAGETWO-71434
Browse files Browse the repository at this point in the history
Fixed Issues:
- MAGETWO-71434 Generated pages with "html" extension are not cached by Varnish
- MAGETWO-69559 Declaration of dependencies for indexers
  • Loading branch information
Sergii Kovalenko authored Aug 17, 2017
2 parents 6df4b3c + 3a61096 commit 86eed24
Show file tree
Hide file tree
Showing 44 changed files with 3,128 additions and 489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
namespace Magento\Indexer\Console\Command;

use Magento\Backend\App\Area\FrontNameResolver;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ObjectManager\ConfigLoader;
use Magento\Framework\ObjectManagerInterface;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\Indexer\IndexerInterface;
Expand Down Expand Up @@ -49,13 +47,23 @@ public function __construct(
}

/**
* Get all indexers
* Return the array of all indexers with keys as indexer ids.
*
* @return IndexerInterface[]
*/
protected function getAllIndexers()
{
return $this->getCollectionFactory()->create()->getItems();
$indexers = $this->getCollectionFactory()->create()->getItems();
return array_combine(
array_map(
function ($item) {
/** @var IndexerInterface $item */
return $item->getId();
},
$indexers
),
$indexers
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
*/
namespace Magento\Indexer\Console\Command;

use Magento\Framework\Indexer\IndexerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Indexer\Model\IndexerFactory;

/**
* An Abstract class for all Indexer related commands.
Expand All @@ -24,26 +20,7 @@ abstract class AbstractIndexerManageCommand extends AbstractIndexerCommand
const INPUT_KEY_INDEXERS = 'index';

/**
* @var IndexerFactory
*/
private $indexerFactory;

/**
* Constructor
*
* @param ObjectManagerFactory $objectManagerFactory
* @param IndexerFactory|null $indexerFactory
*/
public function __construct(
ObjectManagerFactory $objectManagerFactory,
IndexerFactory $indexerFactory = null
) {
parent::__construct($objectManagerFactory);
$this->indexerFactory = $indexerFactory;
}

/**
* Gets list of indexers
* Returns the ordered list of indexers.
*
* @param InputInterface $input
* @return IndexerInterface[]
Expand All @@ -56,32 +33,21 @@ protected function getIndexers(InputInterface $input)
$requestedTypes = $input->getArgument(self::INPUT_KEY_INDEXERS);
$requestedTypes = array_filter(array_map('trim', $requestedTypes), 'strlen');
}

if (empty($requestedTypes)) {
return $this->getAllIndexers();
$indexers = $this->getAllIndexers();
} else {
$indexers = [];
$unsupportedTypes = [];
foreach ($requestedTypes as $code) {
$indexer = $this->getIndexerFactory()->create();
try {
$indexer->load($code);
$indexers[] = $indexer;
} catch (\Exception $e) {
$unsupportedTypes[] = $code;
}
}
$availableIndexers = $this->getAllIndexers();
$unsupportedTypes = array_diff($requestedTypes, array_keys($availableIndexers));
if ($unsupportedTypes) {
$availableTypes = [];
$indexers = $this->getAllIndexers();
foreach ($indexers as $indexer) {
$availableTypes[] = $indexer->getId();
}
throw new \InvalidArgumentException(
"The following requested index types are not supported: '" . join("', '", $unsupportedTypes)
. "'." . PHP_EOL . 'Supported types: ' . join(", ", $availableTypes)
. "'." . PHP_EOL . 'Supported types: ' . join(", ", array_keys($availableIndexers))
);
}
$indexers = array_intersect_key($availableIndexers, array_flip($requestedTypes));
}

return $indexers;
}

Expand All @@ -100,18 +66,4 @@ public function getInputList()
),
];
}

/**
* Get indexer factory
*
* @return IndexerFactory
* @deprecated 100.2.0
*/
private function getIndexerFactory()
{
if (null === $this->indexerFactory) {
$this->indexerFactory = $this->getObjectManager()->get(IndexerFactory::class);
}
return $this->indexerFactory;
}
}
165 changes: 133 additions & 32 deletions app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
*/
namespace Magento\Indexer\Console\Command;

use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Indexer\Config\DependencyInfoProvider;
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;
use Magento\Indexer\Model\IndexerFactory;

/**
* Command to run indexers
Expand All @@ -24,27 +27,33 @@ class IndexerReindexCommand extends AbstractIndexerManageCommand
private $sharedIndexesComplete = [];

/**
* @var \Magento\Framework\Indexer\ConfigInterface
* @var ConfigInterface
*/
private $config;

/**
* @var IndexerFactory
* @var IndexerRegistry
*/
private $indexerFactory;
private $indexerRegistry;

/**
* @var DependencyInfoProvider|null
*/
private $dependencyInfoProvider;

/**
* Constructor
*
* @param ObjectManagerFactory $objectManagerFactory
* @param IndexerFactory|null $indexerFactory
* @param IndexerRegistry|null $indexerRegistry
* @param DependencyInfoProvider|null $dependencyInfoProvider
*/
public function __construct(
ObjectManagerFactory $objectManagerFactory,
IndexerFactory $indexerFactory = null
IndexerRegistry $indexerRegistry = null,
DependencyInfoProvider $dependencyInfoProvider = null
) {
parent::__construct($objectManagerFactory, $indexerFactory);
$this->indexerFactory = $indexerFactory;
$this->indexerRegistry = $indexerRegistry;
$this->dependencyInfoProvider = $dependencyInfoProvider;
parent::__construct($objectManagerFactory);
}

/**
Expand All @@ -64,9 +73,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$indexers = $this->getIndexers($input);
$returnValue = \Magento\Framework\Console\Cli::RETURN_SUCCESS;
foreach ($indexers as $indexer) {
$returnValue = Cli::RETURN_FAILURE;
foreach ($this->getIndexers($input) as $indexer) {
try {
$this->validateIndexerStatus($indexer);
$startTime = microtime(true);
Expand All @@ -84,30 +92,115 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(
$indexer->getTitle() . ' index has been rebuilt successfully in ' . gmdate('H:i:s', $resultTime)
);
$returnValue = Cli::RETURN_SUCCESS;
} catch (LocalizedException $e) {
$output->writeln($e->getMessage());
// we must have an exit code higher than zero to indicate something was wrong
$returnValue = \Magento\Framework\Console\Cli::RETURN_FAILURE;
} catch (\Exception $e) {
$output->writeln($indexer->getTitle() . ' indexer process unknown error:');
$output->writeln($e->getMessage());
// we must have an exit code higher than zero to indicate something was wrong
$returnValue = \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
}
return $returnValue;
}

/**
* {@inheritdoc} Returns the ordered list of specified indexers and related indexers.
*/
protected function getIndexers(InputInterface $input)
{
$indexers = parent::getIndexers($input);
$allIndexers = $this->getAllIndexers();
if (!array_diff_key($allIndexers, $indexers)) {
return $indexers;
}

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

$invalidRelatedIndexers = [];
foreach (array_unique($relatedIndexers) as $relatedIndexer) {
if ($allIndexers[$relatedIndexer]->isInvalid()) {
$invalidRelatedIndexers[] = $relatedIndexer;
}
}

return array_intersect_key(
$allIndexers,
array_flip(
array_unique(
array_merge(
array_keys($indexers),
$invalidRelatedIndexers,
$dependentIndexers
)
)
)
);
}

/**
* Return all indexer Ids on which the current indexer depends (directly or indirectly).
*
* @param string $indexerId
* @return array
*/
private function getRelatedIndexerIds(string $indexerId)
{
$relatedIndexerIds = [];
foreach ($this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($indexerId) as $relatedIndexerId) {
$relatedIndexerIds = array_merge(
$relatedIndexerIds,
[$relatedIndexerId],
$this->getRelatedIndexerIds($relatedIndexerId)
);
}

return array_unique($relatedIndexerIds);
}

/**
* Return all indexer Ids which depend on the current indexer (directly or indirectly).
*
* @param string $indexerId
* @return array
*/
private function getDependentIndexerIds(string $indexerId)
{
$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)
);
}
};

return array_unique($dependentIndexerIds);
}

/**
* Validate that indexer is not locked
*
* @param \Magento\Framework\Indexer\IndexerInterface $indexer
* @param IndexerInterface $indexer
* @return void
* @throws LocalizedException
*/
private function validateIndexerStatus(\Magento\Framework\Indexer\IndexerInterface $indexer)
private function validateIndexerStatus(IndexerInterface $indexer)
{
if ($indexer->getStatus() == \Magento\Framework\Indexer\StateInterface::STATUS_WORKING) {
if ($indexer->getStatus() == StateInterface::STATUS_WORKING) {
throw new LocalizedException(
__(
'%1 index is locked by another reindex process. Skipping.',
Expand Down Expand Up @@ -151,12 +244,10 @@ private function validateSharedIndex($sharedIndex)
return $this;
}
foreach ($indexerIds as $indexerId) {
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $this->getIndexerFactory()->create();
$indexer->load($indexerId);
$indexer = $this->getIndexerRegistry()->get($indexerId);
/** @var \Magento\Indexer\Model\Indexer\State $state */
$state = $indexer->getState();
$state->setStatus(\Magento\Framework\Indexer\StateInterface::STATUS_VALID);
$state->setStatus(StateInterface::STATUS_VALID);
$state->save();
}
$this->sharedIndexesComplete[] = $sharedIndex;
Expand All @@ -166,7 +257,7 @@ private function validateSharedIndex($sharedIndex)
/**
* Get config
*
* @return \Magento\Framework\Indexer\ConfigInterface
* @return ConfigInterface
* @deprecated 100.1.0
*/
private function getConfig()
Expand All @@ -178,16 +269,26 @@ private function getConfig()
}

/**
* Get indexer factory
*
* @return IndexerFactory
* @return IndexerRegistry
* @deprecated 100.2.0
*/
private function getIndexerRegistry()
{
if (!$this->indexerRegistry) {
$this->indexerRegistry = $this->getObjectManager()->get(IndexerRegistry::class);
}
return $this->indexerRegistry;
}

/**
* @return DependencyInfoProvider
* @deprecated 100.2.0
*/
private function getIndexerFactory()
private function getDependencyInfoProvider()
{
if (null === $this->indexerFactory) {
$this->indexerFactory = $this->getObjectManager()->get(IndexerFactory::class);
if (!$this->dependencyInfoProvider) {
$this->dependencyInfoProvider = $this->getObjectManager()->get(DependencyInfoProvider::class);
}
return $this->indexerFactory;
return $this->dependencyInfoProvider;
}
}
2 changes: 0 additions & 2 deletions app/code/Magento/Indexer/Cron/ReindexAllInvalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
namespace Magento\Indexer\Cron;

use Magento\Indexer\Model\Indexer;

class ReindexAllInvalid
{
/**
Expand Down
Loading

0 comments on commit 86eed24

Please sign in to comment.