Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StartConsumerCommand.php
Go to the documentation of this file.
1 <?php
7 
8 use Symfony\Component\Console\Command\Command;
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
15 
19 class StartConsumerCommand extends Command
20 {
21  const ARGUMENT_CONSUMER = 'consumer';
22  const OPTION_NUMBER_OF_MESSAGES = 'max-messages';
23  const OPTION_BATCH_SIZE = 'batch-size';
24  const OPTION_AREACODE = 'area-code';
25  const PID_FILE_PATH = 'pid-file-path';
26  const COMMAND_QUEUE_CONSUMERS_START = 'queue:consumers:start';
27 
31  private $consumerFactory;
32 
36  private $appState;
37 
41  private $pidConsumerManager;
42 
52  public function __construct(
53  \Magento\Framework\App\State $appState,
54  ConsumerFactory $consumerFactory,
55  $name = null,
56  PidConsumerManager $pidConsumerManager = null
57  ) {
58  $this->appState = $appState;
59  $this->consumerFactory = $consumerFactory;
60  $this->pidConsumerManager = $pidConsumerManager ?: \Magento\Framework\App\ObjectManager::getInstance()
61  ->get(PidConsumerManager::class);
62  parent::__construct($name);
63  }
64 
68  protected function execute(InputInterface $input, OutputInterface $output)
69  {
70  $consumerName = $input->getArgument(self::ARGUMENT_CONSUMER);
71  $numberOfMessages = $input->getOption(self::OPTION_NUMBER_OF_MESSAGES);
72  $batchSize = (int)$input->getOption(self::OPTION_BATCH_SIZE);
73  $areaCode = $input->getOption(self::OPTION_AREACODE);
74  $pidFilePath = $input->getOption(self::PID_FILE_PATH);
75 
76  if ($pidFilePath && $this->pidConsumerManager->isRun($pidFilePath)) {
77  $output->writeln('<error>Consumer with the same PID is running</error>');
78  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
79  }
80 
81  if ($pidFilePath) {
82  $this->pidConsumerManager->savePid($pidFilePath);
83  }
84 
85  if ($areaCode !== null) {
86  $this->appState->setAreaCode($areaCode);
87  } else {
88  $this->appState->setAreaCode('global');
89  }
90 
91  $consumer = $this->consumerFactory->get($consumerName, $batchSize);
92  $consumer->process($numberOfMessages);
93  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
94  }
95 
99  protected function configure()
100  {
101  $this->setName(self::COMMAND_QUEUE_CONSUMERS_START);
102  $this->setDescription('Start MessageQueue consumer');
103  $this->addArgument(
104  self::ARGUMENT_CONSUMER,
105  InputArgument::REQUIRED,
106  'The name of the consumer to be started.'
107  );
108  $this->addOption(
109  self::OPTION_NUMBER_OF_MESSAGES,
110  null,
111  InputOption::VALUE_REQUIRED,
112  'The number of messages to be processed by the consumer before process termination. '
113  . 'If not specified - terminate after processing all queued messages.'
114  );
115  $this->addOption(
116  self::OPTION_BATCH_SIZE,
117  null,
118  InputOption::VALUE_REQUIRED,
119  'The number of messages per batch. Applicable for the batch consumer only.'
120  );
121  $this->addOption(
122  self::OPTION_AREACODE,
123  null,
124  InputOption::VALUE_REQUIRED,
125  'The preferred area (global, adminhtml, etc...) '
126  . 'default is global.'
127  );
128  $this->addOption(
129  self::PID_FILE_PATH,
130  null,
131  InputOption::VALUE_REQUIRED,
132  'The file path for saving PID'
133  );
134  $this->setHelp(
135  <<<HELP
136 This command starts MessageQueue consumer by its name.
137 
138 To start consumer which will process all queued messages and terminate execution:
139 
140  <comment>%command.full_name% someConsumer</comment>
141 
142 To specify the number of messages which should be processed by consumer before its termination:
143 
144  <comment>%command.full_name% someConsumer --max-messages=50</comment>
145 
146 To specify the number of messages per batch for the batch consumer:
147 
148  <comment>%command.full_name% someConsumer --batch-size=500</comment>
149 
150 To specify the preferred area:
151 
152  <comment>%command.full_name% someConsumer --area-code='adminhtml'</comment>
153 
154 To save PID enter path:
155 
156  <comment>%command.full_name% someConsumer --pid-file-path='/var/someConsumer.pid'</comment>
157 HELP
158  );
159  parent::configure();
160  }
161 }
__construct(\Magento\Framework\App\State $appState, ConsumerFactory $consumerFactory, $name=null, PidConsumerManager $pidConsumerManager=null)
execute(InputInterface $input, OutputInterface $output)
if(!isset($_GET['name'])) $name
Definition: log.php:14