From d8f6df61d736028cfe7daa4e8ba073da093b054d Mon Sep 17 00:00:00 2001
From: Safwan Khan <safwkhan@ebay.com>
Date: Thu, 30 Apr 2015 15:50:38 -0500
Subject: [PATCH] MAGETWO-36079: Move dev/shell/cron.sh

- Refactored based on CR feedback.
---
 .../Cron/Console/Command/CronCommand.php      | 25 ++++++++++++++++---
 app/code/Magento/Cron/Model/Observer.php      | 17 ++++++++-----
 .../Magento/Framework/Console/Cli.php         |  7 +++++-
 pub/cron.php                                  | 25 ++++++-------------
 4 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/app/code/Magento/Cron/Console/Command/CronCommand.php b/app/code/Magento/Cron/Console/Command/CronCommand.php
index 2d51530988e..649b098ed73 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 800e1e73271..275ad3635ea 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 9d74f61e249..2e62b25d371 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 9f1b816d2f7..3f7793e674c 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;
-- 
GitLab