Skip to content

Commit 4735164

Browse files
Merge pull request #111 from magento-thunder/MAGECLOUD-1364
MAGECLOUD-1364: Merge 2002.0 branch into develop
2 parents a978952 + 99c94d8 commit 4735164

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "magento/ece-tools",
33
"description": "Provides tools to build and deploy Magento 2 Enterprise Edition",
4-
"version": "2002.0.3",
4+
"version": "2002.0.4",
55
"license": [
66
"OSL-3.0",
77
"AFL-3.0"

src/App/Container.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\MagentoCloud\Command\Build;
99
use Magento\MagentoCloud\Command\DbDump;
10+
use Magento\MagentoCloud\Command\CronUnlock;
1011
use Magento\MagentoCloud\Command\Deploy;
1112
use Magento\MagentoCloud\Command\ConfigDump;
1213
use Magento\MagentoCloud\Command\PostDeploy;
@@ -235,6 +236,16 @@ function () use ($root, $config) {
235236
'system/websites',
236237
];
237238
});
239+
$this->container->when(DeployProcess\PreDeploy::class);
240+
$this->container->when(CronUnlock::class)
241+
->needs(ProcessInterface::class)
242+
->give(function () {
243+
return $this->container->makeWith(ProcessComposite::class, [
244+
'processes' => [
245+
$this->container->make(DeployProcess\UnlockCronJobs::class),
246+
],
247+
]);
248+
});
238249
$this->container->when(DeployProcess\PreDeploy::class)
239250
->needs(ProcessInterface::class)
240251
->give(function () {

src/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Composer\Composer;
99
use Magento\MagentoCloud\Command\Build;
10+
use Magento\MagentoCloud\Command\CronUnlock;
1011
use Magento\MagentoCloud\Command\Deploy;
1112
use Magento\MagentoCloud\Command\ConfigDump;
1213
use Magento\MagentoCloud\Command\DbDump;
@@ -57,6 +58,7 @@ protected function getDefaultCommands()
5758
$this->container->get(ConfigDump::class),
5859
$this->container->get(DbDump::class),
5960
$this->container->get(PostDeploy::class),
61+
$this->container->get(CronUnlock::class),
6062
]
6163
);
6264
}

src/Command/CronUnlock.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Command;
7+
8+
use Magento\MagentoCloud\Process\ProcessInterface;
9+
use Psr\Log\LoggerInterface;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
/**
15+
* CLI command for unlocking cron jobs that stuck in "running" state.
16+
*/
17+
class CronUnlock extends Command
18+
{
19+
const NAME = 'cron:unlock';
20+
21+
/**
22+
* @var ProcessInterface
23+
*/
24+
private $process;
25+
26+
/**
27+
* @var LoggerInterface
28+
*/
29+
private $logger;
30+
31+
/**
32+
* @param ProcessInterface $process
33+
* @param LoggerInterface $logger
34+
*/
35+
public function __construct(ProcessInterface $process, LoggerInterface $logger)
36+
{
37+
$this->process = $process;
38+
$this->logger = $logger;
39+
40+
parent::__construct();
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function configure()
47+
{
48+
$this->setName(static::NAME)
49+
->setDescription('Unlock cron jobs that stuck in "running" state.');
50+
51+
parent::configure();
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function execute(InputInterface $input, OutputInterface $output)
58+
{
59+
try {
60+
$this->logger->info('Starting unlocking.');
61+
$this->process->execute();
62+
$this->logger->info('Unlocking completed.');
63+
} catch (\Exception $exception) {
64+
$this->logger->critical($exception->getMessage());
65+
66+
throw $exception;
67+
}
68+
}
69+
}

src/Test/Unit/ApplicationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\MagentoCloud\Application;
1111
use Magento\MagentoCloud\Command\Build;
1212
use Magento\MagentoCloud\Command\ConfigDump;
13+
use Magento\MagentoCloud\Command\CronUnlock;
1314
use Magento\MagentoCloud\Command\Deploy;
1415
use Magento\MagentoCloud\Command\DbDump;
1516
use Magento\MagentoCloud\Command\PostDeploy;
@@ -71,6 +72,18 @@ public function setUp()
7172
$this->mockCommand($configDumpCommand, ConfigDump::NAME);
7273
$this->mockCommand($postDeployCommand, PostDeploy::NAME);
7374
$this->mockCommand($dbDumpCommand, DbDump::NAME);
75+
/**
76+
* Command mocks.
77+
*/
78+
$buildCommandMock = $this->createMock(Build::class);
79+
$deployCommandMock = $this->createMock(Deploy::class);
80+
$configDumpCommand = $this->createMock(ConfigDump::class);
81+
$cronUnlockCommand = $this->createMock(CronUnlock::class);
82+
83+
$this->mockCommand($buildCommandMock, Build::NAME);
84+
$this->mockCommand($deployCommandMock, Deploy::NAME);
85+
$this->mockCommand($configDumpCommand, ConfigDump::NAME);
86+
$this->mockCommand($cronUnlockCommand, CronUnlock::NAME);
7487

7588
$this->containerMock->method('get')
7689
->willReturnMap([
@@ -80,6 +93,10 @@ public function setUp()
8093
[ConfigDump::class, $configDumpCommand],
8194
[PostDeploy::class, $postDeployCommand],
8295
[DbDump::class, $dbDumpCommand],
96+
[Build::class, $buildCommandMock],
97+
[Deploy::class, $deployCommandMock],
98+
[ConfigDump::class, $configDumpCommand],
99+
[CronUnlock::class, $cronUnlockCommand],
83100
]);
84101
$this->composerMock->method('getPackage')
85102
->willReturn($this->packageMock);
@@ -134,5 +151,6 @@ public function testGetVersion()
134151
$this->applicationVersion,
135152
$this->application->getVersion()
136153
);
154+
$this->assertTrue($this->application->has(CronUnlock::NAME));
137155
}
138156
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Test\Unit\Command;
7+
8+
use Magento\MagentoCloud\Command\CronUnlock;
9+
use Magento\MagentoCloud\Process\ProcessInterface;
10+
use PHPUnit\Framework\TestCase;
11+
use Psr\Log\LoggerInterface;
12+
use Symfony\Component\Console\Tester\CommandTester;
13+
use PHPUnit_Framework_MockObject_MockObject as Mock;
14+
15+
class CronUnlockTest extends TestCase
16+
{
17+
/**
18+
* @var ProcessInterface|Mock
19+
*/
20+
private $processMock;
21+
22+
/**
23+
* @var LoggerInterface|Mock
24+
*/
25+
private $loggerMock;
26+
27+
/**
28+
* @var CronUnlock
29+
*/
30+
private $cronUnlockCommand;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp()
36+
{
37+
$this->processMock = $this->getMockForAbstractClass(ProcessInterface::class);
38+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
39+
40+
$this->cronUnlockCommand = new CronUnlock(
41+
$this->processMock,
42+
$this->loggerMock
43+
);
44+
}
45+
46+
public function testExecute()
47+
{
48+
$this->loggerMock->expects($this->exactly(2))
49+
->method('info')
50+
->withConsecutive(
51+
['Starting unlocking.'],
52+
['Unlocking completed.']
53+
);
54+
$this->processMock->expects($this->once())
55+
->method('execute');
56+
57+
$tester = new CommandTester(
58+
$this->cronUnlockCommand
59+
);
60+
$tester->execute([]);
61+
62+
$this->assertSame(0, $tester->getStatusCode());
63+
}
64+
65+
/**
66+
* @expectedException \Exception
67+
* @expectedExceptionMessage Some error
68+
*/
69+
public function testExecuteWithException()
70+
{
71+
$this->loggerMock->expects($this->once())
72+
->method('info')
73+
->with('Starting unlocking.');
74+
$this->loggerMock->expects($this->once())
75+
->method('critical')
76+
->with('Some error');
77+
$this->processMock->expects($this->once())
78+
->method('execute')
79+
->willThrowException(new \Exception('Some error'));
80+
81+
$tester = new CommandTester(
82+
$this->cronUnlockCommand
83+
);
84+
$tester->execute([]);
85+
}
86+
}

0 commit comments

Comments
 (0)