Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
CronCommand.php
Go to the documentation of this file.
1 <?php
8 
9 use Symfony\Component\Console\Command\Command;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputOption;
21 
25 class CronCommand extends Command
26 {
30  const INPUT_KEY_GROUP = 'group';
31 
37  private $objectManagerFactory;
38 
44  private $deploymentConfig;
45 
50  public function __construct(
51  ObjectManagerFactory $objectManagerFactory,
52  DeploymentConfig $deploymentConfig = null
53  ) {
54  $this->objectManagerFactory = $objectManagerFactory;
55  $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(
56  DeploymentConfig::class
57  );
58  parent::__construct();
59  }
60 
64  protected function configure()
65  {
66  $options = [
67  new InputOption(
68  self::INPUT_KEY_GROUP,
69  null,
70  InputOption::VALUE_REQUIRED,
71  'Run jobs only from specified group'
72  ),
73  new InputOption(
75  null,
76  InputOption::VALUE_REQUIRED,
77  'Add or override parameters of the bootstrap'
78  ),
79  ];
80  $this->setName('cron:run')
81  ->setDescription('Runs jobs by schedule')
82  ->setDefinition($options);
83  parent::configure();
84  }
85 
91  protected function execute(InputInterface $input, OutputInterface $output)
92  {
93  if (!$this->deploymentConfig->get('cron/enabled', 1)) {
94  $output->writeln('<info>' . 'Cron is disabled. Jobs were not run.' . '</info>');
95  return;
96  }
97  $omParams = $_SERVER;
98  $omParams[StoreManager::PARAM_RUN_CODE] = 'admin';
99  $omParams[Store::CUSTOM_ENTRY_POINT_PARAM] = true;
100  $objectManager = $this->objectManagerFactory->create($omParams);
101 
102  $params[self::INPUT_KEY_GROUP] = $input->getOption(self::INPUT_KEY_GROUP);
104  $bootstrap = $input->getOption(Cli::INPUT_KEY_BOOTSTRAP);
105  if ($bootstrap) {
106  $bootstrapProcessor = new ComplexParameter(Cli::INPUT_KEY_BOOTSTRAP);
107  $bootstrapOptionValues = $bootstrapProcessor->getFromString(
108  '--' . Cli::INPUT_KEY_BOOTSTRAP . '=' . $bootstrap
109  );
110  $bootstrapOptionValue = $bootstrapOptionValues[ProcessCronQueueObserver::STANDALONE_PROCESS_STARTED];
111  if ($bootstrapOptionValue) {
113  }
114  }
116  $cronObserver = $objectManager->create(\Magento\Framework\App\Cron::class, ['parameters' => $params]);
117  $cronObserver->launch();
118  $output->writeln('<info>' . 'Ran jobs by schedule.' . '</info>');
119  }
120 }
$objectManager
Definition: bootstrap.php:17
const CUSTOM_ENTRY_POINT_PARAM
Definition: Store.php:57
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
$objectManagerFactory
__construct(ObjectManagerFactory $objectManagerFactory, DeploymentConfig $deploymentConfig=null)
Definition: CronCommand.php:50
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18