Skip to content

Commit 17638f5

Browse files
committed
Move Framework modifications into module
1 parent 13bbeb1 commit 17638f5

File tree

6 files changed

+176
-70
lines changed

6 files changed

+176
-70
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cron\Shell;
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem;
10+
use Magento\Framework\OsInfo;
11+
use Magento\Framework\Shell\CommandRenderer;
12+
13+
class CommandRendererBackground extends CommandRenderer
14+
{
15+
/**
16+
* @param Filesystem $filesystem
17+
* @param OsInfo $osInfo
18+
*/
19+
public function __construct(
20+
private readonly Filesystem $filesystem,
21+
private readonly OsInfo $osInfo,
22+
) {
23+
}
24+
25+
/**
26+
* Render command with arguments
27+
*
28+
* @param string $command
29+
* @param array $arguments
30+
* @return string
31+
*/
32+
public function render($command, array $arguments = []): string
33+
{
34+
$command = parent::render($command, $arguments);
35+
36+
$logFile = '/dev/null';
37+
if ($groupId = $arguments[2] ?? null) {
38+
$logDir = $this->filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath();
39+
$logFile = escapeshellarg($logDir . 'magento.cron.' . $groupId . '.log');
40+
}
41+
42+
return $this->osInfo->isWindows() ?
43+
'start /B "magento background task" ' . $command
44+
: str_replace('2>&1', ">> $logFile 2>&1 &", $command);
45+
}
46+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Cron\Shell\Test\Unit;
11+
12+
use Magento\Cron\Shell\CommandRendererBackground;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\ReadInterface;
15+
use Magento\Framework\OsInfo;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @covers CommandRendererBackground
21+
*/
22+
class CommandRendererBackgroundTest extends TestCase
23+
{
24+
/**
25+
* Test path to Magento's var/log directory
26+
*
27+
* @var string
28+
*/
29+
protected $logPath = '/path/to/magento/var/log/';
30+
31+
/**
32+
* Test data for command
33+
*
34+
* @var string
35+
*/
36+
protected $testCommand = 'php -r test.php';
37+
38+
/**
39+
* @var Filesystem|MockObject
40+
*/
41+
protected $filesystem;
42+
43+
/**
44+
* @var OsInfo|MockObject
45+
*/
46+
protected $osInfo;
47+
48+
protected function setUp(): void
49+
{
50+
$this->osInfo = $this->getMockBuilder(OsInfo::class)
51+
->getMock();
52+
53+
$directoryMock = $this->getMockBuilder(ReadInterface::class)
54+
->getMock();
55+
$directoryMock->expects($this->any())
56+
->method('getAbsolutePath')
57+
->willReturn($this->logPath);
58+
59+
$this->filesystem = $this->getMockBuilder(Filesystem::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$this->filesystem->expects($this->any())
63+
->method('getDirectoryRead')
64+
->willReturn($directoryMock);
65+
}
66+
67+
/**
68+
* @covers ::render
69+
* @dataProvider commandPerOsTypeDataProvider
70+
*
71+
* @param bool $isWindows
72+
* @param string $expectedResults
73+
* @param string[] $arguments
74+
*/
75+
public function testRender($isWindows, $expectedResults, $arguments)
76+
{
77+
$this->osInfo->expects($this->once())
78+
->method('isWindows')
79+
->willReturn($isWindows);
80+
81+
$commandRenderer = new CommandRendererBackground($this->filesystem, $this->osInfo);
82+
$this->assertEquals(
83+
$expectedResults,
84+
$commandRenderer->render($this->testCommand, $arguments)
85+
);
86+
}
87+
88+
/**
89+
* Data provider for each os type
90+
*
91+
* @return array
92+
*/
93+
public function commandPerOsTypeDataProvider()
94+
{
95+
return [
96+
'windows' => [
97+
true,
98+
'start /B "magento background task" ' . $this->testCommand . ' 2>&1',
99+
[],
100+
],
101+
'unix-without-group-name' => [
102+
false,
103+
$this->testCommand . ' >> /dev/null 2>&1 &',
104+
[],
105+
],
106+
'unix-with-group-name' => [
107+
false,
108+
$this->testCommand . " >> '{$this->logPath}magento.cron.group-name.log' 2>&1 &",
109+
['php-executable', 'script-path', 'group-name'],
110+
],
111+
];
112+
}
113+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
</argument>
2929
</arguments>
3030
</virtualType>
31-
<!-- @api -->
32-
<virtualType name="shellBackground" type="Magento\Framework\Shell">
31+
<virtualType name="shellBackgroundCron" type="Magento\Framework\Shell">
3332
<arguments>
34-
<argument name="commandRenderer" xsi:type="object">Magento\Framework\Shell\CommandRendererBackground</argument>
33+
<argument name="commandRenderer" xsi:type="object">Magento\Cron\Shell\CommandRendererBackground</argument>
3534
</arguments>
3635
</virtualType>
3736
<type name="Magento\Cron\Observer\ProcessCronQueueObserver">
3837
<arguments>
39-
<argument name="shell" xsi:type="object">shellBackground</argument>
38+
<argument name="shell" xsi:type="object">shellBackgroundCron</argument>
4039
<argument name="logger" xsi:type="object">Magento\Cron\Model\VirtualLogger</argument>
4140
</arguments>
4241
</type>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<argument name="lock" xsi:type="object">RefreshLock</argument>
4444
</arguments>
4545
</type>
46+
<!-- @api -->
47+
<virtualType name="shellBackground" type="Magento\Framework\Shell">
48+
<arguments>
49+
<argument name="commandRenderer" xsi:type="object">Magento\Framework\Shell\CommandRendererBackground</argument>
50+
</arguments>
51+
</virtualType>
4652
<type name="Magento\MessageQueue\Model\Cron\ConsumersRunner">
4753
<arguments>
4854
<argument name="shellBackground" xsi:type="object">shellBackground</argument>

lib/internal/Magento/Framework/Shell/CommandRendererBackground.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,20 @@
55
*/
66
namespace Magento\Framework\Shell;
77

8-
use Magento\Framework\App\Filesystem\DirectoryList;
9-
use Magento\Framework\Filesystem;
108
use Magento\Framework\OsInfo;
119

1210
class CommandRendererBackground extends CommandRenderer
1311
{
14-
/**
15-
* @var Filesystem
16-
*/
17-
protected $filesystem;
18-
1912
/**
2013
* @var \Magento\Framework\OsInfo
2114
*/
2215
protected $osInfo;
2316

2417
/**
25-
* @param Filesystem $filesystem
2618
* @param OsInfo $osInfo
2719
*/
28-
public function __construct(
29-
Filesystem $filesystem,
30-
OsInfo $osInfo
31-
) {
32-
$this->filesystem = $filesystem;
20+
public function __construct(OsInfo $osInfo)
21+
{
3322
$this->osInfo = $osInfo;
3423
}
3524

@@ -44,14 +33,8 @@ public function render($command, array $arguments = [])
4433
{
4534
$command = parent::render($command, $arguments);
4635

47-
$logFile = '/dev/null';
48-
if ($groupId = $arguments[2] ?? null) {
49-
$logDir = $this->filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath();
50-
$logFile = escapeshellarg($logDir . 'magento.cron.' . $groupId . '.log');
51-
}
52-
5336
return $this->osInfo->isWindows() ?
5437
'start /B "magento background task" ' . $command
55-
: str_replace('2>&1', ">> $logFile 2>&1 &", $command);
38+
: str_replace('2>&1', '> /dev/null &', $command);
5639
}
5740
}

lib/internal/Magento/Framework/Shell/Test/Unit/CommandRendererBackgroundTest.php

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,20 @@
77

88
namespace Magento\Framework\Shell\Test\Unit;
99

10-
use Magento\Framework\Filesystem;
11-
use Magento\Framework\Filesystem\Directory\ReadInterface;
1210
use Magento\Framework\OsInfo;
1311
use Magento\Framework\Shell\CommandRendererBackground;
1412
use PHPUnit\Framework\MockObject\MockObject;
1513
use PHPUnit\Framework\TestCase;
1614

1715
class CommandRendererBackgroundTest extends TestCase
1816
{
19-
/**
20-
* Test path to Magento's var/log directory
21-
*
22-
* @var string
23-
*/
24-
protected $logPath = '/path/to/magento/var/log/';
25-
2617
/**
2718
* Test data for command
2819
*
2920
* @var string
3021
*/
3122
protected $testCommand = 'php -r test.php';
3223

33-
/**
34-
* @var Filesystem|MockObject
35-
*/
36-
protected $filesystem;
37-
3824
/**
3925
* @var OsInfo|MockObject
4026
*/
@@ -44,37 +30,23 @@ protected function setUp(): void
4430
{
4531
$this->osInfo = $this->getMockBuilder(OsInfo::class)
4632
->getMock();
47-
48-
$directoryMock = $this->getMockBuilder(ReadInterface::class)
49-
->getMock();
50-
$directoryMock->expects($this->any())
51-
->method('getAbsolutePath')
52-
->willReturn($this->logPath);
53-
54-
$this->filesystem = $this->getMockBuilder(Filesystem::class)
55-
->disableOriginalConstructor()
56-
->getMock();
57-
$this->filesystem->expects($this->any())
58-
->method('getDirectoryRead')
59-
->willReturn($directoryMock);
6033
}
6134

6235
/**
6336
* @dataProvider commandPerOsTypeDataProvider
6437
* @param bool $isWindows
6538
* @param string $expectedResults
66-
* @param string[] $arguments
6739
*/
68-
public function testRender($isWindows, $expectedResults, $arguments)
40+
public function testRender($isWindows, $expectedResults)
6941
{
7042
$this->osInfo->expects($this->once())
7143
->method('isWindows')
7244
->willReturn($isWindows);
7345

74-
$commandRenderer = new CommandRendererBackground($this->filesystem, $this->osInfo);
46+
$commandRenderer = new CommandRendererBackground($this->osInfo);
7547
$this->assertEquals(
7648
$expectedResults,
77-
$commandRenderer->render($this->testCommand, $arguments)
49+
$commandRenderer->render($this->testCommand)
7850
);
7951
}
8052

@@ -86,21 +58,8 @@ public function testRender($isWindows, $expectedResults, $arguments)
8658
public function commandPerOsTypeDataProvider()
8759
{
8860
return [
89-
'windows' => [
90-
true,
91-
'start /B "magento background task" ' . $this->testCommand . ' 2>&1',
92-
[],
93-
],
94-
'unix-without-group-name' => [
95-
false,
96-
$this->testCommand . ' >> /dev/null 2>&1 &',
97-
[],
98-
],
99-
'unix-with-group-name' => [
100-
false,
101-
$this->testCommand . " >> '{$this->logPath}magento.cron.group-name.log' 2>&1 &",
102-
['php-executable', 'script-path', 'group-name'],
103-
],
61+
'windows' => [true, 'start /B "magento background task" ' . $this->testCommand . ' 2>&1'],
62+
'unix' => [false, $this->testCommand . ' > /dev/null &'],
10463
];
10564
}
10665
}

0 commit comments

Comments
 (0)