Skip to content

Commit a69199d

Browse files
Merge remote-tracking branch '32690/cron-log' into 37018-test
2 parents 8f36bc3 + 37d1481 commit a69199d

File tree

7 files changed

+194
-18
lines changed

7 files changed

+194
-18
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
23
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
4+
* Copyright 2017 Adobe
5+
* All Rights Reserved.
56
*/
67

78
namespace Magento\Cron\Console\Command;
@@ -135,10 +136,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
135136
$params[ProcessCronQueueObserver::STANDALONE_PROCESS_STARTED] = $bootstrapOptionValue;
136137
}
137138
}
139+
138140
/** @var Cron $cronObserver */
139141
$cronObserver = $objectManager->create(Cron::class, ['parameters' => $params]);
140142
$cronObserver->launch();
141-
$output->writeln('<info>' . 'Ran jobs by schedule.' . '</info>');
143+
144+
// phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged,Magento2.Exceptions.TryProcessSystemResources.MissingTryCatch
145+
if (stream_isatty(STDOUT)) {
146+
$output->writeln('<info>' . 'Ran jobs by schedule.' . '</info>');
147+
}
142148

143149
return Cli::RETURN_SUCCESS;
144150
}

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2016 Adobe
4+
* All Rights Reserved.
55
*/
66

77
/**
@@ -278,10 +278,12 @@ function ($a, $b) {
278278
&& $this->getCronGroupConfigurationValue($groupId, 'use_separate_process') == 1
279279
) {
280280
$this->_shell->execute(
281-
$phpPath . ' %s cron:run --group=' . $groupId . ' --' . Cli::INPUT_KEY_BOOTSTRAP . '='
281+
'%s %s cron:run --group=%s --' . Cli::INPUT_KEY_BOOTSTRAP . '='
282282
. self::STANDALONE_PROCESS_STARTED . '=1',
283283
[
284-
BP . '/bin/magento'
284+
$phpPath,
285+
BP . '/bin/magento',
286+
$groupId,
285287
]
286288
);
287289
continue;
@@ -848,7 +850,7 @@ private function processPendingJobs(string $groupId, array $jobsRoot, int $curre
848850
/** @var Schedule $schedule */
849851
foreach ($pendingJobs as $schedule) {
850852
if (isset($processedJobs[$schedule->getJobCode()])) {
851-
// process only on job per run
853+
// process only one of each job per run
852854
continue;
853855
}
854856
$jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2017 Adobe
5+
* All Rights Reserved.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Cron\Shell;
11+
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\OsInfo;
15+
use Magento\Framework\Shell\CommandRenderer;
16+
17+
class CommandRendererBackground extends CommandRenderer
18+
{
19+
/**
20+
* @param Filesystem $filesystem
21+
* @param OsInfo $osInfo
22+
*/
23+
public function __construct(
24+
private readonly Filesystem $filesystem,
25+
private readonly OsInfo $osInfo,
26+
) {
27+
}
28+
29+
/**
30+
* @inheritDoc
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+
// @phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
40+
$logFile = escapeshellarg($logDir . 'magento.cron.' . $groupId . '.log');
41+
}
42+
43+
return $this->osInfo->isWindows() ?
44+
'start /B "magento background task" ' . $command
45+
: str_replace('2>&1', ">> $logFile 2>&1 &", $command);
46+
}
47+
}

app/code/Magento/Cron/Test/Unit/Console/Command/CronCommandTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
4+
* Copyright 2017 Adobe
5+
* All Rights Reserved.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\Cron\Test\Unit\Console\Command;
@@ -80,7 +82,7 @@ public function testExecute()
8082
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
8183
);
8284
$commandTester->execute([]);
83-
$expectedMsg = 'Ran jobs by schedule.' . PHP_EOL;
85+
$expectedMsg = '';
8486
$this->assertEquals($expectedMsg, $commandTester->getDisplay());
8587
}
8688
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2017 Adobe
5+
* All Rights Reserved.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Cron\Test\Unit\Shell;
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 \Magento\Cron\Shell\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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2017 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
@@ -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>
@@ -65,7 +64,7 @@
6564
<arguments>
6665
<argument name="tasks" xsi:type="array">
6766
<item name="cronMagento" xsi:type="array">
68-
<item name="command" xsi:type="string">{magentoRoot}bin/magento cron:run | grep -v "Ran jobs by schedule" >> {magentoLog}magento.cron.log</item>
67+
<item name="command" xsi:type="string">{magentoRoot}bin/magento cron:run >> {magentoLog}magento.cron.log 2>&amp;1</item>
6968
<item name="optional" xsi:type="boolean">false</item>
7069
</item>
7170
</argument>

app/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,4 +2014,11 @@
20142014
<preference for="Magento\Framework\App\PageCache\IdentifierInterface" type="Magento\Framework\App\PageCache\Identifier"/>
20152015
<preference for="Magento\Framework\App\State\ReloadProcessorInterface" type="Magento\Framework\App\State\ReloadProcessorComposite" />
20162016
<preference for="Magento\Framework\Indexer\Config\Converter\SortingAdjustmentInterface" type="Magento\Framework\Indexer\Config\Converter\SortingAdjustment" />
2017+
2018+
<!-- @api -->
2019+
<virtualType name="shellBackground" type="Magento\Framework\Shell">
2020+
<arguments>
2021+
<argument name="commandRenderer" xsi:type="object">Magento\Framework\Shell\CommandRendererBackground</argument>
2022+
</arguments>
2023+
</virtualType>
20172024
</config>

0 commit comments

Comments
 (0)