Skip to content

Commit eff2592

Browse files
authored
MAGETWO-81579: Backported cron:install and cron:remove commands, support to manage multiple instances in the same crontab, based on installation directory #11361
2 parents 282a31b + 4432386 commit eff2592

File tree

11 files changed

+877
-2
lines changed

11 files changed

+877
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Cron\Console\Command;
87

98
use Symfony\Component\Console\Command\Command;
@@ -94,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9493
}
9594
}
9695
/** @var \Magento\Framework\App\Cron $cronObserver */
97-
$cronObserver = $objectManager->create('Magento\Framework\App\Cron', ['parameters' => $params]);
96+
$cronObserver = $objectManager->create(\Magento\Framework\App\Cron::class, ['parameters' => $params]);
9897
$cronObserver->launch();
9998
$output->writeln('<info>' . 'Ran jobs by schedule.' . '</info>');
10099
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cron\Console\Command;
7+
8+
use Magento\Framework\Crontab\CrontabManagerInterface;
9+
use Magento\Framework\Crontab\TasksProviderInterface;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Magento\Framework\Console\Cli;
15+
use Symfony\Component\Console\Input\InputOption;
16+
17+
/**
18+
* CronInstallCommand installs Magento cron tasks
19+
*/
20+
class CronInstallCommand extends Command
21+
{
22+
/**
23+
* @var CrontabManagerInterface
24+
*/
25+
private $crontabManager;
26+
27+
/**
28+
* @var TasksProviderInterface
29+
*/
30+
private $tasksProvider;
31+
32+
/**
33+
* @param CrontabManagerInterface $crontabManager
34+
* @param TasksProviderInterface $tasksProvider
35+
*/
36+
public function __construct(
37+
CrontabManagerInterface $crontabManager,
38+
TasksProviderInterface $tasksProvider
39+
) {
40+
$this->crontabManager = $crontabManager;
41+
$this->tasksProvider = $tasksProvider;
42+
43+
parent::__construct();
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function configure()
50+
{
51+
$this->setName('cron:install')
52+
->setDescription('Generates and installs crontab for current user')
53+
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force install tasks');
54+
55+
parent::configure();
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
protected function execute(InputInterface $input, OutputInterface $output)
62+
{
63+
if ($this->crontabManager->getTasks() && !$input->getOption('force')) {
64+
$output->writeln('<error>Crontab has already been generated and saved</error>');
65+
return Cli::RETURN_FAILURE;
66+
}
67+
68+
try {
69+
$this->crontabManager->saveTasks($this->tasksProvider->getTasks());
70+
} catch (LocalizedException $e) {
71+
$output->writeln('<error>' . $e->getMessage() . '</error>');
72+
return Cli::RETURN_FAILURE;
73+
}
74+
75+
$output->writeln('<info>Crontab has been generated and saved</info>');
76+
77+
return Cli::RETURN_SUCCESS;
78+
}
79+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cron\Console\Command;
7+
8+
use Magento\Framework\Crontab\CrontabManagerInterface;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Magento\Framework\Console\Cli;
13+
use Magento\Framework\Exception\LocalizedException;
14+
15+
/**
16+
* CronRemoveCommand removes Magento cron tasks
17+
*/
18+
class CronRemoveCommand extends Command
19+
{
20+
/**
21+
* @var CrontabManagerInterface
22+
*/
23+
private $crontabManager;
24+
25+
/**
26+
* @param CrontabManagerInterface $crontabManager
27+
*/
28+
public function __construct(CrontabManagerInterface $crontabManager)
29+
{
30+
$this->crontabManager = $crontabManager;
31+
32+
parent::__construct();
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected function configure()
39+
{
40+
$this->setName('cron:remove')
41+
->setDescription('Removes tasks from crontab');
42+
43+
parent::configure();
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
try {
52+
$this->crontabManager->removeTasks();
53+
} catch (LocalizedException $e) {
54+
$output->writeln('<error>' . $e->getMessage() . '</error>');
55+
return Cli::RETURN_FAILURE;
56+
}
57+
58+
$output->writeln('<info>Magento cron tasks have been removed</info>');
59+
60+
return Cli::RETURN_SUCCESS;
61+
}
62+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\Cron\Model\ConfigInterface" type="Magento\Cron\Model\Config" />
1010
<preference for="Magento\Framework\Shell\CommandRendererInterface" type="Magento\Framework\Shell\CommandRenderer" />
11+
<preference for="Magento\Framework\Crontab\CrontabManagerInterface" type="Magento\Framework\Crontab\CrontabManager" />
12+
<preference for="Magento\Framework\Crontab\TasksProviderInterface" type="Magento\Framework\Crontab\TasksProvider" />
1113
<type name="Magento\Cron\Model\Config\Reader\Db">
1214
<arguments>
1315
<argument name="defaultReader" xsi:type="object">DefaultScopeReader</argument>
@@ -33,6 +35,8 @@
3335
<arguments>
3436
<argument name="commands" xsi:type="array">
3537
<item name="cronCommand" xsi:type="object">Magento\Cron\Console\Command\CronCommand</item>
38+
<item name="cronInstall" xsi:type="object">Magento\Cron\Console\Command\CronInstallCommand</item>
39+
<item name="cronRemove" xsi:type="object">Magento\Cron\Console\Command\CronRemoveCommand</item>
3640
</argument>
3741
</arguments>
3842
</type>
@@ -43,4 +47,24 @@
4347
</argument>
4448
</arguments>
4549
</type>
50+
<type name="Magento\Framework\Crontab\CrontabManagerInterface">
51+
<arguments>
52+
<argument name="shell" xsi:type="object">Magento\Framework\App\Shell</argument>
53+
</arguments>
54+
</type>
55+
<type name="Magento\Framework\Crontab\TasksProviderInterface">
56+
<arguments>
57+
<argument name="tasks" xsi:type="array">
58+
<item name="cronMagento" xsi:type="array">
59+
<item name="command" xsi:type="string">{magentoRoot}bin/magento cron:run | grep -v "Ran jobs by schedule" >> {magentoLog}magento.cron.log</item>
60+
</item>
61+
<item name="cronUpdate" xsi:type="array">
62+
<item name="command" xsi:type="string">{magentoRoot}update/cron.php >> {magentoLog}update.cron.log</item>
63+
</item>
64+
<item name="cronSetup" xsi:type="array">
65+
<item name="command" xsi:type="string">{magentoRoot}bin/magento setup:cron:run >> {magentoLog}setup.cron.log</item>
66+
</item>
67+
</argument>
68+
</arguments>
69+
</type>
4670
</config>

0 commit comments

Comments
 (0)