Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable cron:run when maintenance mode is enabled #21181

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/code/Magento/Cron/Console/Command/CronCommand.php
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Magento\Framework\App\MaintenanceMode;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -43,18 +44,30 @@ class CronCommand extends Command
*/
private $deploymentConfig;

/**
* Maintenance mode
*
* @var MaintenanceMode
*/
private $maintenanceMode;

/**
* @param ObjectManagerFactory $objectManagerFactory
* @param DeploymentConfig $deploymentConfig Application deployment configuration
* @param MaintenanceMode $maintenanceMode
*/
public function __construct(
ObjectManagerFactory $objectManagerFactory,
DeploymentConfig $deploymentConfig = null
DeploymentConfig $deploymentConfig = null,
MaintenanceMode $maintenanceMode = null
) {
$this->objectManagerFactory = $objectManagerFactory;
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(
DeploymentConfig::class
);
$this->maintenanceMode = $maintenanceMode ?: ObjectManager::getInstance()->get(
MaintenanceMode::class
);
parent::__construct();
}

Expand Down Expand Up @@ -90,6 +103,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->maintenanceMode->isOn()) {
$output->writeln('<info>' . 'Cron is disabled because the maintenance mode is enabled.' . '</info>');
return;
}
if (!$this->deploymentConfig->get('cron/enabled', 1)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pay attention to this configuration. You already may disable cron. Add cron/enabled as 0 to configuration file. There is no special CLI command for this setting for now.

$output->writeln('<info>' . 'Cron is disabled. Jobs were not run.' . '</info>');
return;
Expand Down
Expand Up @@ -7,6 +7,7 @@

use Magento\Cron\Console\Command\CronCommand;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\MaintenanceMode;
use Magento\Framework\App\ObjectManagerFactory;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\Console\Tester\CommandTester;
Expand All @@ -23,10 +24,16 @@ class CronCommandTest extends \PHPUnit\Framework\TestCase
*/
private $deploymentConfigMock;

/**
* @var MaintenanceMode|MockObject
*/
private $maintenanceModeMock;

protected function setUp()
{
$this->objectManagerFactory = $this->createMock(ObjectManagerFactory::class);
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
$this->maintenanceModeMock = $this->createMock(MaintenanceMode::class);
}

/**
Expand All @@ -36,14 +43,17 @@ protected function setUp()
*/
public function testExecuteWithDisabledCrons()
{
$this->maintenanceModeMock->expects($this->once())
->method('isOn')
->willReturn(false);
$this->objectManagerFactory->expects($this->never())
->method('create');
$this->deploymentConfigMock->expects($this->once())
->method('get')
->with('cron/enabled', 1)
->willReturn(0);
$commandTester = new CommandTester(
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock, $this->maintenanceModeMock)
);
$commandTester->execute([]);
$expectedMsg = 'Cron is disabled. Jobs were not run.' . PHP_EOL;
Expand All @@ -64,6 +74,9 @@ public function testExecute()
->willReturn($cron);
$cron->expects($this->once())
->method('launch');
$this->maintenanceModeMock->expects($this->once())
->method('isOn')
->willReturn(false);
$this->objectManagerFactory->expects($this->once())
->method('create')
->willReturn($objectManager);
Expand All @@ -72,10 +85,32 @@ public function testExecute()
->with('cron/enabled', 1)
->willReturn(1);
$commandTester = new CommandTester(
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock, $this->maintenanceModeMock)
);
$commandTester->execute([]);
$expectedMsg = 'Ran jobs by schedule.' . PHP_EOL;
$this->assertEquals($expectedMsg, $commandTester->getDisplay());
}

/**
* Test command with maintenance mode enabled
*
* @return void
*/
public function testWithMaintenanceModeEnabled()
{
$this->maintenanceModeMock->expects($this->once())
->method('isOn')
->willReturn(true);
$this->objectManagerFactory->expects($this->never())
->method('create');
$this->deploymentConfigMock->expects($this->never())
->method('get');
$commandTester = new CommandTester(
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock, $this->maintenanceModeMock)
);
$commandTester->execute([]);
$expectedMsg = 'Cron is disabled because the maintenance mode is enabled.' . PHP_EOL;
$this->assertEquals($expectedMsg, $commandTester->getDisplay());
}
}