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

MAGECLOUD-1364: Merge 2002.0 branch into develop #111

Merged
merged 4 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "magento/ece-tools",
"description": "Provides tools to build and deploy Magento 2 Enterprise Edition",
"version": "2002.0.3",
"version": "2002.0.4",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
11 changes: 11 additions & 0 deletions src/App/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\MagentoCloud\Command\Build;
use Magento\MagentoCloud\Command\DbDump;
use Magento\MagentoCloud\Command\CronUnlock;
use Magento\MagentoCloud\Command\Deploy;
use Magento\MagentoCloud\Command\ConfigDump;
use Magento\MagentoCloud\Command\PostDeploy;
Expand Down Expand Up @@ -235,6 +236,16 @@ function () use ($root, $config) {
'system/websites',
];
});
$this->container->when(DeployProcess\PreDeploy::class);
$this->container->when(CronUnlock::class)
->needs(ProcessInterface::class)
->give(function () {
return $this->container->makeWith(ProcessComposite::class, [
'processes' => [
$this->container->make(DeployProcess\UnlockCronJobs::class),
],
]);
});
$this->container->when(DeployProcess\PreDeploy::class)
->needs(ProcessInterface::class)
->give(function () {
Expand Down
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Composer\Composer;
use Magento\MagentoCloud\Command\Build;
use Magento\MagentoCloud\Command\CronUnlock;
use Magento\MagentoCloud\Command\Deploy;
use Magento\MagentoCloud\Command\ConfigDump;
use Magento\MagentoCloud\Command\DbDump;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected function getDefaultCommands()
$this->container->get(ConfigDump::class),
$this->container->get(DbDump::class),
$this->container->get(PostDeploy::class),
$this->container->get(CronUnlock::class),
]
);
}
Expand Down
69 changes: 69 additions & 0 deletions src/Command/CronUnlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\Command;

use Magento\MagentoCloud\Process\ProcessInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* CLI command for unlocking cron jobs that stuck in "running" state.
*/
class CronUnlock extends Command
{
const NAME = 'cron:unlock';

/**
* @var ProcessInterface
*/
private $process;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param ProcessInterface $process
* @param LoggerInterface $logger
*/
public function __construct(ProcessInterface $process, LoggerInterface $logger)
{
$this->process = $process;
$this->logger = $logger;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName(static::NAME)
->setDescription('Unlock cron jobs that stuck in "running" state.');

parent::configure();
}

/**
* @inheritdoc
*/
public function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->logger->info('Starting unlocking.');
$this->process->execute();
$this->logger->info('Unlocking completed.');
} catch (\Exception $exception) {
$this->logger->critical($exception->getMessage());

throw $exception;
}
}
}
18 changes: 18 additions & 0 deletions src/Test/Unit/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\MagentoCloud\Application;
use Magento\MagentoCloud\Command\Build;
use Magento\MagentoCloud\Command\ConfigDump;
use Magento\MagentoCloud\Command\CronUnlock;
use Magento\MagentoCloud\Command\Deploy;
use Magento\MagentoCloud\Command\DbDump;
use Magento\MagentoCloud\Command\PostDeploy;
Expand Down Expand Up @@ -71,6 +72,18 @@ public function setUp()
$this->mockCommand($configDumpCommand, ConfigDump::NAME);
$this->mockCommand($postDeployCommand, PostDeploy::NAME);
$this->mockCommand($dbDumpCommand, DbDump::NAME);
/**
* Command mocks.
*/
$buildCommandMock = $this->createMock(Build::class);
$deployCommandMock = $this->createMock(Deploy::class);
$configDumpCommand = $this->createMock(ConfigDump::class);
$cronUnlockCommand = $this->createMock(CronUnlock::class);

$this->mockCommand($buildCommandMock, Build::NAME);
$this->mockCommand($deployCommandMock, Deploy::NAME);
$this->mockCommand($configDumpCommand, ConfigDump::NAME);
$this->mockCommand($cronUnlockCommand, CronUnlock::NAME);

$this->containerMock->method('get')
->willReturnMap([
Expand All @@ -80,6 +93,10 @@ public function setUp()
[ConfigDump::class, $configDumpCommand],
[PostDeploy::class, $postDeployCommand],
[DbDump::class, $dbDumpCommand],
[Build::class, $buildCommandMock],
[Deploy::class, $deployCommandMock],
[ConfigDump::class, $configDumpCommand],
[CronUnlock::class, $cronUnlockCommand],
]);
$this->composerMock->method('getPackage')
->willReturn($this->packageMock);
Expand Down Expand Up @@ -134,5 +151,6 @@ public function testGetVersion()
$this->applicationVersion,
$this->application->getVersion()
);
$this->assertTrue($this->application->has(CronUnlock::NAME));
}
}
86 changes: 86 additions & 0 deletions src/Test/Unit/Command/CronUnlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\Test\Unit\Command;

use Magento\MagentoCloud\Command\CronUnlock;
use Magento\MagentoCloud\Process\ProcessInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit_Framework_MockObject_MockObject as Mock;

class CronUnlockTest extends TestCase
{
/**
* @var ProcessInterface|Mock
*/
private $processMock;

/**
* @var LoggerInterface|Mock
*/
private $loggerMock;

/**
* @var CronUnlock
*/
private $cronUnlockCommand;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->processMock = $this->getMockForAbstractClass(ProcessInterface::class);
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);

$this->cronUnlockCommand = new CronUnlock(
$this->processMock,
$this->loggerMock
);
}

public function testExecute()
{
$this->loggerMock->expects($this->exactly(2))
->method('info')
->withConsecutive(
['Starting unlocking.'],
['Unlocking completed.']
);
$this->processMock->expects($this->once())
->method('execute');

$tester = new CommandTester(
$this->cronUnlockCommand
);
$tester->execute([]);

$this->assertSame(0, $tester->getStatusCode());
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Some error
*/
public function testExecuteWithException()
{
$this->loggerMock->expects($this->once())
->method('info')
->with('Starting unlocking.');
$this->loggerMock->expects($this->once())
->method('critical')
->with('Some error');
$this->processMock->expects($this->once())
->method('execute')
->willThrowException(new \Exception('Some error'));

$tester = new CommandTester(
$this->cronUnlockCommand
);
$tester->execute([]);
}
}