CronCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cron\Console\Command;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Framework\App\ObjectManagerFactory;
  13. use Magento\Store\Model\Store;
  14. use Magento\Store\Model\StoreManager;
  15. use Magento\Cron\Observer\ProcessCronQueueObserver;
  16. use Magento\Framework\App\DeploymentConfig;
  17. use Magento\Framework\Console\Cli;
  18. use Magento\Framework\Shell\ComplexParameter;
  19. /**
  20. * Command for executing cron jobs
  21. */
  22. class CronCommand extends Command
  23. {
  24. /**
  25. * Name of input option
  26. */
  27. const INPUT_KEY_GROUP = 'group';
  28. /**
  29. * Object manager factory
  30. *
  31. * @var ObjectManagerFactory
  32. */
  33. private $objectManagerFactory;
  34. /**
  35. * Application deployment configuration
  36. *
  37. * @var DeploymentConfig
  38. */
  39. private $deploymentConfig;
  40. /**
  41. * @param ObjectManagerFactory $objectManagerFactory
  42. * @param DeploymentConfig $deploymentConfig Application deployment configuration
  43. */
  44. public function __construct(
  45. ObjectManagerFactory $objectManagerFactory,
  46. DeploymentConfig $deploymentConfig = null
  47. ) {
  48. $this->objectManagerFactory = $objectManagerFactory;
  49. $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(
  50. DeploymentConfig::class
  51. );
  52. parent::__construct();
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function configure()
  58. {
  59. $options = [
  60. new InputOption(
  61. self::INPUT_KEY_GROUP,
  62. null,
  63. InputOption::VALUE_REQUIRED,
  64. 'Run jobs only from specified group'
  65. ),
  66. new InputOption(
  67. Cli::INPUT_KEY_BOOTSTRAP,
  68. null,
  69. InputOption::VALUE_REQUIRED,
  70. 'Add or override parameters of the bootstrap'
  71. ),
  72. ];
  73. $this->setName('cron:run')
  74. ->setDescription('Runs jobs by schedule')
  75. ->setDefinition($options);
  76. parent::configure();
  77. }
  78. /**
  79. * Runs cron jobs if cron is not disabled in Magento configurations
  80. *
  81. * {@inheritdoc}
  82. */
  83. protected function execute(InputInterface $input, OutputInterface $output)
  84. {
  85. if (!$this->deploymentConfig->get('cron/enabled', 1)) {
  86. $output->writeln('<info>' . 'Cron is disabled. Jobs were not run.' . '</info>');
  87. return;
  88. }
  89. $omParams = $_SERVER;
  90. $omParams[StoreManager::PARAM_RUN_CODE] = 'admin';
  91. $omParams[Store::CUSTOM_ENTRY_POINT_PARAM] = true;
  92. $objectManager = $this->objectManagerFactory->create($omParams);
  93. $params[self::INPUT_KEY_GROUP] = $input->getOption(self::INPUT_KEY_GROUP);
  94. $params[ProcessCronQueueObserver::STANDALONE_PROCESS_STARTED] = '0';
  95. $bootstrap = $input->getOption(Cli::INPUT_KEY_BOOTSTRAP);
  96. if ($bootstrap) {
  97. $bootstrapProcessor = new ComplexParameter(Cli::INPUT_KEY_BOOTSTRAP);
  98. $bootstrapOptionValues = $bootstrapProcessor->getFromString(
  99. '--' . Cli::INPUT_KEY_BOOTSTRAP . '=' . $bootstrap
  100. );
  101. $bootstrapOptionValue = $bootstrapOptionValues[ProcessCronQueueObserver::STANDALONE_PROCESS_STARTED];
  102. if ($bootstrapOptionValue) {
  103. $params[ProcessCronQueueObserver::STANDALONE_PROCESS_STARTED] = $bootstrapOptionValue;
  104. }
  105. }
  106. /** @var \Magento\Framework\App\Cron $cronObserver */
  107. $cronObserver = $objectManager->create(\Magento\Framework\App\Cron::class, ['parameters' => $params]);
  108. $cronObserver->launch();
  109. $output->writeln('<info>' . 'Ran jobs by schedule.' . '</info>');
  110. }
  111. }