Example #1
0
 /**
  * @param \Symfony\Component\Console\Input\InputInterface $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  * @return int|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->detectMagento($output, true);
     if ($this->initMagento()) {
         $this->disableObservers();
         try {
             \Mage::dispatchEvent('shell_reindex_init_process');
             $indexCollection = $this->_getIndexerModel()->getProcessesCollection();
             foreach ($indexCollection as $indexer) {
                 $output->writeln('<info>Started reindex of: <comment>' . $indexer->getIndexerCode() . '</comment></info>');
                 /**
                  * Try to estimate runtime. If index was aborted or never created we have a timestamp < 0
                  */
                 $runtimeInSeconds = $this->getRuntimeInSeconds($indexer);
                 if ($runtimeInSeconds > 0) {
                     $estimatedEnd = new \DateTime('now', new \DateTimeZone('UTC'));
                     $estimatedEnd->add(new \DateInterval('PT' . $runtimeInSeconds . 'S'));
                     $output->writeln('<info>Estimated end: <comment>' . $estimatedEnd->format('Y-m-d H:i:s T') . '</comment></info>');
                 }
                 $startTime = new \DateTime('now');
                 $dateTimeUtils = new \N98\Util\DateTime();
                 $indexer->reindexEverything();
                 \Mage::dispatchEvent($indexer->getIndexerCode() . '_shell_reindex_after');
                 $endTime = new \DateTime('now');
                 $output->writeln('<info>Successfully reindexed <comment>' . $indexer->getIndexerCode() . '</comment> (Runtime: <comment>' . $dateTimeUtils->getDifferenceAsString($startTime, $endTime) . '</comment>)</info>');
             }
             \Mage::dispatchEvent('shell_reindex_init_process');
         } catch (\Exception $e) {
             \Mage::dispatchEvent('shell_reindex_init_process');
         }
     }
 }
Example #2
0
 /**
  * @param \Symfony\Component\Console\Input\InputInterface $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  * @return int|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->detectMagento($output, true);
     if ($this->initMagento()) {
         $this->writeSection($output, 'Reindex');
         $this->disableObservers();
         $indexCode = $input->getArgument('index_code');
         $indexerList = $this->getIndexerList();
         if ($indexCode === null) {
             $question = array();
             foreach ($indexerList as $key => $indexer) {
                 $question[] = '<comment>' . str_pad('[' . ($key + 1) . ']', 4, ' ', STR_PAD_RIGHT) . '</comment> ' . str_pad($indexer['code'], 40, ' ', STR_PAD_RIGHT) . ' <info>(last runtime: ' . $indexer['last_runtime'] . ')</info>' . "\n";
             }
             $question[] = '<question>Please select a indexer:</question>';
             $indexCodes = $this->getHelper('dialog')->askAndValidate($output, $question, function ($typeInput) use($indexerList) {
                 if (strstr($typeInput, ',')) {
                     $typeInputs = \N98\Util\String::trimExplodeEmpty(',', $typeInput);
                 } else {
                     $typeInputs = array($typeInput);
                 }
                 $returnCodes = array();
                 foreach ($typeInputs as $typeInput) {
                     if (!isset($indexerList[$typeInput - 1])) {
                         throw new \InvalidArgumentException('Invalid indexer');
                     }
                     $returnCodes[] = $indexerList[$typeInput - 1]['code'];
                 }
                 return $returnCodes;
             });
         } else {
             // take cli argument
             $indexCodes = \N98\Util\String::trimExplodeEmpty(',', $indexCode);
         }
         foreach ($indexCodes as $indexCode) {
             try {
                 \Mage::dispatchEvent('shell_reindex_init_process');
                 $process = $this->_getIndexerModel()->getProcessByCode($indexCode);
                 if (!$process) {
                     throw new \InvalidArgumentException('Indexer was not found!');
                 }
                 $output->writeln('<info>Started reindex of: <comment>' . $indexCode . '</comment></info>');
                 /**
                  * Try to estimate runtime. If index was aborted or never created we have a timestamp < 0
                  */
                 $runtimeInSeconds = $this->getRuntimeInSeconds($process);
                 if ($runtimeInSeconds > 0) {
                     $estimatedEnd = new \DateTime('now', new \DateTimeZone('UTC'));
                     $estimatedEnd->add(new \DateInterval('PT' . $runtimeInSeconds . 'S'));
                     $output->writeln('<info>Estimated end: <comment>' . $estimatedEnd->format('Y-m-d H:i:s T') . '</comment></info>');
                 }
                 $startTime = new \DateTime('now');
                 $dateTimeUtils = new \N98\Util\DateTime();
                 $process->reindexEverything();
                 \Mage::dispatchEvent($process->getIndexerCode() . '_shell_reindex_after');
                 $endTime = new \DateTime('now');
                 $output->writeln('<info>Successfully reindexed <comment>' . $indexCode . '</comment> (Runtime: <comment>' . $dateTimeUtils->getDifferenceAsString($startTime, $endTime) . '</comment>)</info>');
                 \Mage::dispatchEvent('shell_reindex_finalize_process');
             } catch (\Exception $e) {
                 $output->writeln('<error>' . $e->getMessage() . '</error>');
                 \Mage::dispatchEvent('shell_reindex_finalize_process');
             }
         }
     }
 }