diff --git a/app/code/Magento/Cron/Console/Command/CronCommand.php b/app/code/Magento/Cron/Console/Command/CronCommand.php index 2d51530988e60267e261790d14a9453f3deb456b..649b098ed73b10e4633d310423498eb9e54afbae 100644 --- a/app/code/Magento/Cron/Console/Command/CronCommand.php +++ b/app/code/Magento/Cron/Console/Command/CronCommand.php @@ -14,6 +14,9 @@ use Magento\Framework\App\ObjectManagerFactory; use Magento\Framework\ObjectManagerInterface; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManager; +use Magento\Cron\Model\Observer; +use Magento\Framework\Console\CLI; +use Magento\Framework\Shell\ComplexParameter; /** * Command for executing cron jobs @@ -59,6 +62,12 @@ class CronCommand extends Command 'Run jobs only from specified group', 'default' ), + new InputOption( + CLI::INPUT_KEY_BOOTSTRAP, + null, + InputOption::VALUE_REQUIRED, + 'Add or override parameters of the bootstrap' + ), ]; $this->setName('cron:run') ->setDescription('Runs jobs by schedule') @@ -71,9 +80,19 @@ class CronCommand extends Command */ protected function execute(InputInterface $input, OutputInterface $output) { - $params['group'] = $input->getOption(self::INPUT_KEY_GROUP); - // This 'standaloneProcessStarted' flag is for internal communication between processes only - $params['standaloneProcessStarted'] = '0'; + $params[self::INPUT_KEY_GROUP] = $input->getOption(self::INPUT_KEY_GROUP); + $params[Observer::STANDALONE_PROCESS_STARTED] = '0'; + $bootstrap = $input->getOption(CLI::INPUT_KEY_BOOTSTRAP); + if ($bootstrap) { + $bootstrapProcessor = new ComplexParameter(CLI::INPUT_KEY_BOOTSTRAP); + $bootstrapOptionValues = $bootstrapProcessor->getFromString( + '--' . CLI::INPUT_KEY_BOOTSTRAP . '=' . $bootstrap + ); + $bootstrapOptionValue = $bootstrapOptionValues[Observer::STANDALONE_PROCESS_STARTED]; + if ($bootstrapOptionValue) { + $params[Observer::STANDALONE_PROCESS_STARTED] = $bootstrapOptionValue; + } + } /** @var \Magento\Framework\App\Cron $cronObserver */ $cronObserver = $this->objectManager->create('Magento\Framework\App\Cron', ['parameters' => $params]); $cronObserver->launch(); diff --git a/app/code/Magento/Cron/Model/Observer.php b/app/code/Magento/Cron/Model/Observer.php index 800e1e732710d455b0a12a728df25a01f486ba5f..275ad3635eaa410a0d4a6a388a5f958e0995cfb2 100644 --- a/app/code/Magento/Cron/Model/Observer.php +++ b/app/code/Magento/Cron/Model/Observer.php @@ -9,7 +9,7 @@ */ namespace Magento\Cron\Model; -use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Console\CLI; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -23,6 +23,12 @@ class Observer const CACHE_KEY_LAST_HISTORY_CLEANUP_AT = 'cron_last_history_cleanup_at'; + /** + * Flag for internal communication between processes for running + * all jobs in a group in parallel as a separate process + */ + const STANDALONE_PROCESS_STARTED = 'standaloneProcessStarted'; + /**#@-*/ /**#@+ @@ -143,18 +149,17 @@ class Observer if ($this->_request->getParam('group') !== null && $this->_request->getParam('group') != $groupId) { continue; } - if (($this->_request->getParam('standaloneProcessStarted') !== '1') && ( + if (($this->_request->getParam(self::STANDALONE_PROCESS_STARTED) !== '1') && ( $this->_scopeConfig->getValue( 'system/cron/' . $groupId . '/use_separate_process', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) == 1 )) { $this->_shell->execute( - 'php -f %s -- --group=%s --standaloneProcessStarted=%s', + 'php %s cron:run --group=' . $groupId . ' --' . CLI::INPUT_KEY_BOOTSTRAP . '=' + . self::STANDALONE_PROCESS_STARTED . '=1', [ - BP . '/' . DirectoryList::PUB . '/cron.php', - $groupId, - '1' + BP . '/bin/magento' ] ); continue; diff --git a/lib/internal/Magento/Framework/Console/Cli.php b/lib/internal/Magento/Framework/Console/Cli.php index 9d74f61e2495a6075c003fcebf8fe478ba94b1fc..2e62b25d371eec25787180c45f4b4b512bba03af 100644 --- a/lib/internal/Magento/Framework/Console/Cli.php +++ b/lib/internal/Magento/Framework/Console/Cli.php @@ -17,6 +17,11 @@ use Magento\Framework\Shell\ComplexParameter; */ class Cli extends SymfonyApplication { + /** + * Name of input option + */ + const INPUT_KEY_BOOTSTRAP = 'bootstrap'; + /** * {@inheritdoc} */ @@ -41,7 +46,7 @@ class Cli extends SymfonyApplication $toolsCommands = []; $modulesCommands = []; - $bootstrapParam = new ComplexParameter('bootstrap'); + $bootstrapParam = new ComplexParameter(self::INPUT_KEY_BOOTSTRAP); $params = $bootstrapParam->mergeFromArgv($_SERVER, $_SERVER); $params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null; $bootstrap = Bootstrap::create(BP, $params); diff --git a/pub/cron.php b/pub/cron.php index 9f1b816d2f74261f2437803559aeecf6095b63c4..3f7793e674cba132f4c128b1dc6cadb44cb050e6 100755 --- a/pub/cron.php +++ b/pub/cron.php @@ -14,27 +14,16 @@ require dirname(__DIR__) . '/app/bootstrap.php'; if ($_GET){ $opt = $_GET; } else { - $usage = 'Usage: php -f pub/cron.php -- [--group=<groupId>]' . PHP_EOL; - $longOpts = [ - 'help', - 'group::', - 'standaloneProcessStarted::' - ]; - $opt = getopt('', $longOpts); - if (isset($opt['help'])) { - echo $usage; - exit(0); - } -} -if (empty($opt['group'])) { - $opt['group'] = 'default'; -} -// This flag is for internal communication between processes only; no user input is needed for this -if (empty($opt['standaloneProcessStarted'])) { - $opt['standaloneProcessStarted'] = '0'; + echo "You cannot run this from the command line." . PHP_EOL . + "Run \"php bin/magento cron:run\" instead." . PHP_EOL; + exit(1); } try { + if (empty($opt['group'])) { + $opt['group'] = 'default'; + } + $opt['standaloneProcessStarted'] = '0'; $params = $_SERVER; $params[StoreManager::PARAM_RUN_CODE] = 'admin'; $params[Store::CUSTOM_ENTRY_POINT_PARAM] = true;