appState = $appState;
$this->consumerFactory = $consumerFactory;
$this->lockManager = $lockManager ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(LockManagerInterface::class);
parent::__construct($name);
}
/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$consumerName = $input->getArgument(self::ARGUMENT_CONSUMER);
$numberOfMessages = $input->getOption(self::OPTION_NUMBER_OF_MESSAGES);
$batchSize = (int)$input->getOption(self::OPTION_BATCH_SIZE);
$areaCode = $input->getOption(self::OPTION_AREACODE);
if ($input->getOption(self::PID_FILE_PATH)) {
$input->setOption(self::OPTION_SINGLE_THREAD, true);
}
$singleThread = $input->getOption(self::OPTION_SINGLE_THREAD);
$multiProcess = $input->getOption(self::OPTION_MULTI_PROCESS);
if ($multiProcess && !$this->lockManager->lock(md5($consumerName . '-' . $multiProcess),0)) { //phpcs:ignore
$output->writeln('Consumer with the same name is running');
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
if ($singleThread && !$this->lockManager->lock(md5($consumerName),0)) { //phpcs:ignore
$output->writeln('Consumer with the same name is running');
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
$this->appState->setAreaCode($areaCode ?? 'global');
$consumer = $this->consumerFactory->get($consumerName, $batchSize);
$consumer->process($numberOfMessages);
if ($singleThread) {
$this->lockManager->unlock(md5($consumerName)); //phpcs:ignore
}
if ($multiProcess) {
$this->lockManager->unlock(md5($consumerName . '-' . $multiProcess)); //phpcs:ignore
}
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}
/**
* @inheritdoc
*/
protected function configure()
{
$this->setName(self::COMMAND_QUEUE_CONSUMERS_START);
$this->setDescription('Start MessageQueue consumer');
$this->addArgument(
self::ARGUMENT_CONSUMER,
InputArgument::REQUIRED,
'The name of the consumer to be started.'
);
$this->addOption(
self::OPTION_NUMBER_OF_MESSAGES,
null,
InputOption::VALUE_REQUIRED,
'The number of messages to be processed by the consumer before process termination. '
. 'If not specified - terminate after processing all queued messages.'
);
$this->addOption(
self::OPTION_BATCH_SIZE,
null,
InputOption::VALUE_REQUIRED,
'The number of messages per batch. Applicable for the batch consumer only.'
);
$this->addOption(
self::OPTION_AREACODE,
null,
InputOption::VALUE_REQUIRED,
'The preferred area (global, adminhtml, etc...) '
. 'default is global.'
);
$this->addOption(
self::OPTION_SINGLE_THREAD,
null,
InputOption::VALUE_NONE,
'This option prevents running multiple copies of one consumer simultaneously.'
);
$this->addOption(
self::OPTION_MULTI_PROCESS,
null,
InputOption::VALUE_OPTIONAL,
'The number of processes per consumer.'
);
$this->addOption(
self::PID_FILE_PATH,
null,
InputOption::VALUE_REQUIRED,
'The file path for saving PID (This option is deprecated, use --single-thread instead)'
);
$this->setHelp(
<<%command.full_name% someConsumer
To specify the number of messages which should be processed by consumer before its termination:
%command.full_name% someConsumer --max-messages=50
To specify the number of messages per batch for the batch consumer:
%command.full_name% someConsumer --batch-size=500
To specify the preferred area:
%command.full_name% someConsumer --area-code='adminhtml'
To do not run multiple copies of one consumer simultaneously:
%command.full_name% someConsumer --single-thread
To save PID enter path (This option is deprecated, use --single-thread instead):
%command.full_name% someConsumer --pid-file-path='/var/someConsumer.pid'
To define the number of processes per consumer:
%command.full_name% someConsumer --multi-process=4
HELP
);
parent::configure();
}
}