/** * Check whether all indices are valid or not * * @return bool */ public function isDisplayed() { /** @var \Magento\Indexer\Model\Indexer $indexer */ foreach ($this->collection->getItems() as $indexer) { if ($indexer->getStatus() == \Magento\Framework\Indexer\StateInterface::STATUS_INVALID) { return true; } } return false; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->_state->setAreaCode('crontab'); $indexersData = $this->indexerCollection->getItems(); if (!is_null($input->getOption(self::INPUT_KEY_MAX_PRODUCTS))) { $productsNum = $input->getOption(self::INPUT_KEY_MAX_PRODUCTS); } else { $productsNum = $this->productCollection->getSize(); } if (!is_null($input->getOption(self::INPUT_KEY_MAX_CATEGORIES))) { $categoriesNum = $input->getOption(self::INPUT_KEY_MAX_CATEGORIES); } else { $categoriesNum = $this->categoryCollection->getSize(); } if (!is_null($input->getOption(self::INPUT_KEY_MAX_CUSTOMERS))) { $customersNum = $input->getOption(self::INPUT_KEY_MAX_CUSTOMERS); } else { $customersNum = $this->customerCollection->getSize(); } if (!is_null($input->getOption(self::INPUT_KEY_MAX_RULES))) { $rulesNum = $input->getOption(self::INPUT_KEY_MAX_RULES); } else { $rulesNum = $this->ruleCollection->getSize(); } //These indexers are not ready to run in single-row mode $excludeIndexers = ['catalogrule_rule', 'catalogsearch_fulltext']; $productIndexers = ['catalog_product_flat', 'catalog_product_price', 'catalog_product_attribute', 'cataloginventory_stock', 'catalogrule_product', 'targetrule_product_rule', 'catalogpermissions_product', 'catalogrule_rule', 'catalogsearch_fulltext']; $categoryIndexers = ['catalog_category_flat', 'catalog_category_product', 'catalog_product_category', 'catalogpermissions_category']; $ruleIndexers = ['targetrule_rule_product']; $customerIndexres = ['customer_grid']; $requestedIndexer = $input->getOption('indexer'); if ($requestedIndexer == 'all') { $runThese = []; } else { $runThese = explode(',', $input->getOption('indexer')); } foreach ($indexersData as $indexer) { if (in_array($indexer->getIndexerId(), $excludeIndexers)) { continue; } if (count($runThese) > 0 && !in_array($indexer->getIndexerId(), $runThese)) { continue; } $max = 0; $entity = ''; if (in_array($indexer->getIndexerId(), $productIndexers)) { $max = $productsNum; $entity = 'Product'; } else { if (in_array($indexer->getIndexerId(), $categoryIndexers)) { $max = $categoriesNum; $entity = 'Category'; } else { if (in_array($indexer->getIndexerId(), $customerIndexres)) { $max = $customersNum; $entity = 'Customer'; } else { if (in_array($indexer->getIndexerId(), $ruleIndexers)) { $max = $rulesNum; $entity = 'Rule'; } } } } if (!is_null($input->getOption(self::INPUT_KEY_REINDEX_ID))) { $id = $input->getOption(self::INPUT_KEY_REINDEX_ID); } else { $id = mt_rand(1, $max); } $output->writeln('<info>' . $entity . '# id ' . $id . ' (out of ' . $max . ') for ' . $indexer->getIndexerId() . ' </info>'); $this->indexRegistry->get($indexer->getIndexerId())->reindexRow($id); $output->writeln('<info>' . $indexer->getDescription() . ' completed.</info>'); } }