Skip to content

Commit d8f6df6

Browse files
committed
MAGETWO-36079: Move dev/shell/cron.sh
- Refactored based on CR feedback.
1 parent bf706ef commit d8f6df6

File tree

4 files changed

+46
-28
lines changed

4 files changed

+46
-28
lines changed

app/code/Magento/Cron/Console/Command/CronCommand.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Framework\ObjectManagerInterface;
1515
use Magento\Store\Model\Store;
1616
use Magento\Store\Model\StoreManager;
17+
use Magento\Cron\Model\Observer;
18+
use Magento\Framework\Console\CLI;
19+
use Magento\Framework\Shell\ComplexParameter;
1720

1821
/**
1922
* Command for executing cron jobs
@@ -59,6 +62,12 @@ protected function configure()
5962
'Run jobs only from specified group',
6063
'default'
6164
),
65+
new InputOption(
66+
CLI::INPUT_KEY_BOOTSTRAP,
67+
null,
68+
InputOption::VALUE_REQUIRED,
69+
'Add or override parameters of the bootstrap'
70+
),
6271
];
6372
$this->setName('cron:run')
6473
->setDescription('Runs jobs by schedule')
@@ -71,9 +80,19 @@ protected function configure()
7180
*/
7281
protected function execute(InputInterface $input, OutputInterface $output)
7382
{
74-
$params['group'] = $input->getOption(self::INPUT_KEY_GROUP);
75-
// This 'standaloneProcessStarted' flag is for internal communication between processes only
76-
$params['standaloneProcessStarted'] = '0';
83+
$params[self::INPUT_KEY_GROUP] = $input->getOption(self::INPUT_KEY_GROUP);
84+
$params[Observer::STANDALONE_PROCESS_STARTED] = '0';
85+
$bootstrap = $input->getOption(CLI::INPUT_KEY_BOOTSTRAP);
86+
if ($bootstrap) {
87+
$bootstrapProcessor = new ComplexParameter(CLI::INPUT_KEY_BOOTSTRAP);
88+
$bootstrapOptionValues = $bootstrapProcessor->getFromString(
89+
'--' . CLI::INPUT_KEY_BOOTSTRAP . '=' . $bootstrap
90+
);
91+
$bootstrapOptionValue = $bootstrapOptionValues[Observer::STANDALONE_PROCESS_STARTED];
92+
if ($bootstrapOptionValue) {
93+
$params[Observer::STANDALONE_PROCESS_STARTED] = $bootstrapOptionValue;
94+
}
95+
}
7796
/** @var \Magento\Framework\App\Cron $cronObserver */
7897
$cronObserver = $this->objectManager->create('Magento\Framework\App\Cron', ['parameters' => $params]);
7998
$cronObserver->launch();

app/code/Magento/Cron/Model/Observer.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
namespace Magento\Cron\Model;
1111

12-
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\Console\CLI;
1313

1414
/**
1515
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -23,6 +23,12 @@ class Observer
2323

2424
const CACHE_KEY_LAST_HISTORY_CLEANUP_AT = 'cron_last_history_cleanup_at';
2525

26+
/**
27+
* Flag for internal communication between processes for running
28+
* all jobs in a group in parallel as a separate process
29+
*/
30+
const STANDALONE_PROCESS_STARTED = 'standaloneProcessStarted';
31+
2632
/**#@-*/
2733

2834
/**#@+
@@ -143,18 +149,17 @@ public function dispatch($observer)
143149
if ($this->_request->getParam('group') !== null && $this->_request->getParam('group') != $groupId) {
144150
continue;
145151
}
146-
if (($this->_request->getParam('standaloneProcessStarted') !== '1') && (
152+
if (($this->_request->getParam(self::STANDALONE_PROCESS_STARTED) !== '1') && (
147153
$this->_scopeConfig->getValue(
148154
'system/cron/' . $groupId . '/use_separate_process',
149155
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
150156
) == 1
151157
)) {
152158
$this->_shell->execute(
153-
'php -f %s -- --group=%s --standaloneProcessStarted=%s',
159+
'php %s cron:run --group=' . $groupId . ' --' . CLI::INPUT_KEY_BOOTSTRAP . '='
160+
. self::STANDALONE_PROCESS_STARTED . '=1',
154161
[
155-
BP . '/' . DirectoryList::PUB . '/cron.php',
156-
$groupId,
157-
'1'
162+
BP . '/bin/magento'
158163
]
159164
);
160165
continue;

lib/internal/Magento/Framework/Console/Cli.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
class Cli extends SymfonyApplication
1919
{
20+
/**
21+
* Name of input option
22+
*/
23+
const INPUT_KEY_BOOTSTRAP = 'bootstrap';
24+
2025
/**
2126
* {@inheritdoc}
2227
*/
@@ -41,7 +46,7 @@ protected function getApplicationCommands()
4146
$toolsCommands = [];
4247
$modulesCommands = [];
4348

44-
$bootstrapParam = new ComplexParameter('bootstrap');
49+
$bootstrapParam = new ComplexParameter(self::INPUT_KEY_BOOTSTRAP);
4550
$params = $bootstrapParam->mergeFromArgv($_SERVER, $_SERVER);
4651
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
4752
$bootstrap = Bootstrap::create(BP, $params);

pub/cron.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,16 @@
1414
if ($_GET){
1515
$opt = $_GET;
1616
} else {
17-
$usage = 'Usage: php -f pub/cron.php -- [--group=<groupId>]' . PHP_EOL;
18-
$longOpts = [
19-
'help',
20-
'group::',
21-
'standaloneProcessStarted::'
22-
];
23-
$opt = getopt('', $longOpts);
24-
if (isset($opt['help'])) {
25-
echo $usage;
26-
exit(0);
27-
}
28-
}
29-
if (empty($opt['group'])) {
30-
$opt['group'] = 'default';
31-
}
32-
// This flag is for internal communication between processes only; no user input is needed for this
33-
if (empty($opt['standaloneProcessStarted'])) {
34-
$opt['standaloneProcessStarted'] = '0';
17+
echo "You cannot run this from the command line." . PHP_EOL .
18+
"Run \"php bin/magento cron:run\" instead." . PHP_EOL;
19+
exit(1);
3520
}
3621

3722
try {
23+
if (empty($opt['group'])) {
24+
$opt['group'] = 'default';
25+
}
26+
$opt['standaloneProcessStarted'] = '0';
3827
$params = $_SERVER;
3928
$params[StoreManager::PARAM_RUN_CODE] = 'admin';
4029
$params[Store::CUSTOM_ENTRY_POINT_PARAM] = true;

0 commit comments

Comments
 (0)