Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CronRunCommand.php
Go to the documentation of this file.
1 <?php
7 
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 
19 {
23  protected $deploymentConfig;
24 
28  protected $queue;
29 
33  protected $status;
34 
38  protected $readinessCheck;
39 
46  public function __construct(
48  Queue $queue,
51  ) {
52  $this->deploymentConfig = $deploymentConfig;
53  $this->queue = $queue;
54  $this->readinessCheck = $readinessCheck;
55  $this->status = $status;
56  parent::__construct();
57  }
58 
64  protected function configure()
65  {
66  $this->setName('setup:cron:run')
67  ->setDescription('Runs cron job scheduled for setup application');
68 
69  parent::configure();
70  }
71 
75  protected function execute(InputInterface $input, OutputInterface $output)
76  {
77  $notification = 'setup-cron: Please check var/log/update.log for execution summary.';
78 
79  if (!$this->deploymentConfig->isAvailable()) {
80  $output->writeln($notification);
81  $this->status->add('Magento is not installed.', \Psr\Log\LogLevel::INFO);
82  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
83  }
84 
85  if (!$this->checkRun()) {
86  $output->writeln($notification);
87  // we must have an exit code higher than zero to indicate something was wrong
88  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
89  }
90  try {
91  $this->status->toggleUpdateInProgress();
92  } catch (\RuntimeException $e) {
93  $this->status->add($e->getMessage(), \Psr\Log\LogLevel::ERROR);
94  $output->writeln($notification);
95  // we must have an exit code higher than zero to indicate something was wrong
96  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
97  }
98 
99  $returnCode = $this->executeJobsFromQueue();
100  if ($returnCode != \Magento\Framework\Console\Cli::RETURN_SUCCESS) {
101  $output->writeln($notification);
102  }
103 
104  return $returnCode;
105  }
106 
112  private function checkRun()
113  {
114  return $this->readinessCheck->runReadinessCheck()
115  && !$this->status->isUpdateInProgress()
116  && !$this->status->isUpdateError();
117  }
118 
124  private function executeJobsFromQueue()
125  {
127  try {
128  while (!empty($this->queue->peek()) && strpos($this->queue->peek()[Queue::KEY_JOB_NAME], 'setup:') === 0) {
129  $job = $this->queue->popQueuedJob();
130  $this->status->add(
131  sprintf('Job "%s" has started' . PHP_EOL, $job),
132  \Psr\Log\LogLevel::INFO
133  );
134  try {
135  $job->execute();
136  $this->status->add(
137  sprintf('Job "%s" has been successfully completed', $job),
138  \Psr\Log\LogLevel::INFO
139  );
140  } catch (\Exception $e) {
141  $this->status->toggleUpdateError(true);
142  $this->status->add(
143  sprintf('An error occurred while executing job "%s": %s', $job, $e->getMessage()),
144  \Psr\Log\LogLevel::ERROR
145  );
147  }
148  }
149  } catch (\Exception $e) {
150  $this->status->add($e->getMessage(), \Psr\Log\LogLevel::ERROR);
151  $this->status->toggleUpdateError(true);
153  } finally {
154  $this->status->toggleUpdateInProgress(false);
155  }
156  return $returnCode;
157  }
158 }
execute(InputInterface $input, OutputInterface $output)
__construct(DeploymentConfig $deploymentConfig, Queue $queue, ReadinessCheck $readinessCheck, Status $status)