Skip to content

Commit df49d06

Browse files
authored
ENGCOM-5717: #24186: CLI: cron:install. Break tasks to default and non-optional. #24187
2 parents 9cb846b + 891024c commit df49d06

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Cron\Console\Command;
79

810
use Magento\Framework\Crontab\CrontabManagerInterface;
@@ -19,6 +21,9 @@
1921
*/
2022
class CronInstallCommand extends Command
2123
{
24+
private const COMMAND_OPTION_FORCE = 'force';
25+
private const COMMAND_OPTION_NON_OPTIONAL = 'non-optional';
26+
2227
/**
2328
* @var CrontabManagerInterface
2429
*/
@@ -44,19 +49,27 @@ public function __construct(
4449
}
4550

4651
/**
47-
* {@inheritdoc}
52+
* @inheritdoc
4853
*/
4954
protected function configure()
5055
{
5156
$this->setName('cron:install')
5257
->setDescription('Generates and installs crontab for current user')
53-
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force install tasks');
58+
->addOption(self::COMMAND_OPTION_FORCE, 'f', InputOption::VALUE_NONE, 'Force install tasks')
59+
// @codingStandardsIgnoreStart
60+
->addOption(self::COMMAND_OPTION_NON_OPTIONAL, 'd', InputOption::VALUE_NONE, 'Install only the non-optional (default) tasks');
61+
// @codingStandardsIgnoreEnd
5462

5563
parent::configure();
5664
}
5765

5866
/**
59-
* {@inheritdoc}
67+
* Executes "cron:install" command.
68+
*
69+
* @param InputInterface $input
70+
* @param OutputInterface $output
71+
* @return int|null
72+
* @throws LocalizedException
6073
*/
6174
protected function execute(InputInterface $input, OutputInterface $output)
6275
{
@@ -65,8 +78,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
6578
return Cli::RETURN_FAILURE;
6679
}
6780

81+
$tasks = $this->tasksProvider->getTasks();
82+
if ($input->getOption(self::COMMAND_OPTION_NON_OPTIONAL)) {
83+
$tasks = $this->extractNonOptionalTasks($tasks);
84+
}
85+
6886
try {
69-
$this->crontabManager->saveTasks($this->tasksProvider->getTasks());
87+
$this->crontabManager->saveTasks($tasks);
7088
} catch (LocalizedException $e) {
7189
$output->writeln('<error>' . $e->getMessage() . '</error>');
7290
return Cli::RETURN_FAILURE;
@@ -76,4 +94,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
7694

7795
return Cli::RETURN_SUCCESS;
7896
}
97+
98+
/**
99+
* Returns an array of non-optional tasks
100+
*
101+
* @param array $tasks
102+
* @return array
103+
*/
104+
private function extractNonOptionalTasks(array $tasks = []): array
105+
{
106+
$defaultTasks = [];
107+
108+
foreach ($tasks as $taskCode => $taskParams) {
109+
if (!$taskParams['optional']) {
110+
$defaultTasks[$taskCode] = $taskParams;
111+
}
112+
}
113+
114+
return $defaultTasks;
115+
}
79116
}

app/code/Magento/Cron/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@
6666
<argument name="tasks" xsi:type="array">
6767
<item name="cronMagento" xsi:type="array">
6868
<item name="command" xsi:type="string">{magentoRoot}bin/magento cron:run | grep -v "Ran jobs by schedule" >> {magentoLog}magento.cron.log</item>
69+
<item name="optional" xsi:type="boolean">false</item>
6970
</item>
7071
<item name="cronUpdate" xsi:type="array">
7172
<item name="command" xsi:type="string">{magentoRoot}update/cron.php >> {magentoLog}update.cron.log</item>
73+
<item name="optional" xsi:type="boolean">true</item>
7274
</item>
7375
<item name="cronSetup" xsi:type="array">
7476
<item name="command" xsi:type="string">{magentoRoot}bin/magento setup:cron:run >> {magentoLog}setup.cron.log</item>
77+
<item name="optional" xsi:type="boolean">true</item>
7578
</item>
7679
</argument>
7780
</arguments>

0 commit comments

Comments
 (0)