IndexerSetModeCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Console\Command;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. /**
  13. * Command for setting index mode for indexers.
  14. */
  15. class IndexerSetModeCommand extends AbstractIndexerManageCommand
  16. {
  17. /**#@+
  18. * Names of input arguments or options
  19. */
  20. const INPUT_KEY_MODE = 'mode';
  21. const INPUT_KEY_REALTIME = 'realtime';
  22. const INPUT_KEY_SCHEDULE = 'schedule';
  23. /**#@- */
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this->setName('indexer:set-mode')
  30. ->setDescription('Sets index mode type')
  31. ->setDefinition($this->getInputList());
  32. parent::configure();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $errors = $this->validate($input);
  40. if ($errors) {
  41. throw new \InvalidArgumentException(implode("\n", $errors));
  42. }
  43. $indexers = $this->getIndexers($input);
  44. $returnValue = \Magento\Framework\Console\Cli::RETURN_SUCCESS;
  45. foreach ($indexers as $indexer) {
  46. try {
  47. $previousStatus = $indexer->isScheduled() ? 'Update by Schedule' : 'Update on Save';
  48. $indexer->setScheduled($input->getArgument(self::INPUT_KEY_MODE) === self::INPUT_KEY_SCHEDULE);
  49. $currentStatus = $indexer->isScheduled() ? 'Update by Schedule' : 'Update on Save';
  50. if ($previousStatus !== $currentStatus) {
  51. $output->writeln(
  52. 'Index mode for Indexer ' . $indexer->getTitle() . ' was changed from \''
  53. . $previousStatus . '\' to \'' . $currentStatus . '\''
  54. );
  55. } else {
  56. $output->writeln('Index mode for Indexer ' . $indexer->getTitle() . ' has not been changed');
  57. }
  58. } catch (LocalizedException $e) {
  59. $output->writeln($e->getMessage() . PHP_EOL);
  60. // we must have an exit code higher than zero to indicate something was wrong
  61. $returnValue = \Magento\Framework\Console\Cli::RETURN_FAILURE;
  62. } catch (\Exception $e) {
  63. $output->writeln($indexer->getTitle() . " indexer process unknown error:" . PHP_EOL);
  64. $output->writeln($e->getMessage() . PHP_EOL);
  65. // we must have an exit code higher than zero to indicate something was wrong
  66. $returnValue = \Magento\Framework\Console\Cli::RETURN_FAILURE;
  67. }
  68. }
  69. return $returnValue;
  70. }
  71. /**
  72. * Get list of arguments for the command
  73. *
  74. * @return InputOption[]
  75. */
  76. public function getInputList()
  77. {
  78. $modeOptions[] = new InputArgument(
  79. self::INPUT_KEY_MODE,
  80. InputArgument::OPTIONAL,
  81. 'Indexer mode type ['. self::INPUT_KEY_REALTIME . '|' . self::INPUT_KEY_SCHEDULE .']'
  82. );
  83. $optionsList = array_merge($modeOptions, parent::getInputList());
  84. return $optionsList;
  85. }
  86. /**
  87. * Check if all admin options are provided
  88. *
  89. * @param InputInterface $input
  90. * @return string[]
  91. */
  92. public function validate(InputInterface $input)
  93. {
  94. $errors = [];
  95. $acceptedValues = ' Accepted values for ' . self::INPUT_KEY_MODE . ' are \''
  96. . self::INPUT_KEY_REALTIME . '\' or \'' . self::INPUT_KEY_SCHEDULE . '\'';
  97. $inputMode = $input->getArgument(self::INPUT_KEY_MODE);
  98. if (!$inputMode) {
  99. $errors[] = 'Missing argument \'' . self::INPUT_KEY_MODE .'\'.' . $acceptedValues;
  100. } elseif (!in_array($inputMode, [self::INPUT_KEY_REALTIME, self::INPUT_KEY_SCHEDULE])) {
  101. $errors[] = $acceptedValues;
  102. }
  103. return $errors;
  104. }
  105. }