Skip to content

Commit f6d4a77

Browse files
authored
ENGCOM-4124: Direct STDERR output when listing crontab to /dev/null #20951
2 parents a746b57 + e87f504 commit f6d4a77

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

lib/internal/Magento/Framework/Crontab/CrontabManager.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
78
namespace Magento\Framework\Crontab;
89

910
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -40,31 +41,35 @@ public function __construct(
4041
}
4142

4243
/**
44+
* Build tasks block start text.
45+
*
4346
* @return string
4447
*/
4548
private function getTasksBlockStart()
4649
{
4750
$tasksBlockStart = self::TASKS_BLOCK_START;
4851
if (defined('BP')) {
49-
$tasksBlockStart .= ' ' . md5(BP);
52+
$tasksBlockStart .= ' ' . hash("sha256", BP);
5053
}
5154
return $tasksBlockStart;
5255
}
5356

5457
/**
58+
* Build tasks block end text.
59+
*
5560
* @return string
5661
*/
5762
private function getTasksBlockEnd()
5863
{
5964
$tasksBlockEnd = self::TASKS_BLOCK_END;
6065
if (defined('BP')) {
61-
$tasksBlockEnd .= ' ' . md5(BP);
66+
$tasksBlockEnd .= ' ' . hash("sha256", BP);
6267
}
6368
return $tasksBlockEnd;
6469
}
6570

6671
/**
67-
* {@inheritdoc}
72+
* @inheritdoc
6873
*/
6974
public function getTasks()
7075
{
@@ -82,7 +87,7 @@ public function getTasks()
8287
}
8388

8489
/**
85-
* {@inheritdoc}
90+
* @inheritdoc
8691
*/
8792
public function saveTasks(array $tasks)
8893
{
@@ -118,8 +123,7 @@ public function saveTasks(array $tasks)
118123
}
119124

120125
/**
121-
* {@inheritdoc}
122-
* @throws LocalizedException
126+
* @inheritdoc
123127
*/
124128
public function removeTasks()
125129
{
@@ -182,7 +186,7 @@ private function cleanMagentoSection($content)
182186
private function getCrontabContent()
183187
{
184188
try {
185-
$content = (string)$this->shell->execute('crontab -l');
189+
$content = (string)$this->shell->execute('crontab -l 2>/dev/null');
186190
} catch (LocalizedException $e) {
187191
return '';
188192
}
@@ -203,6 +207,7 @@ private function save($content)
203207

204208
try {
205209
$this->shell->execute('echo "' . $content . '" | crontab -');
210+
// phpcs:disable Magento2.Exceptions.ThrowCatch
206211
} catch (LocalizedException $e) {
207212
throw new LocalizedException(
208213
new Phrase('Error during saving of crontab: %1', [$e->getPrevious()->getMessage()]),

lib/internal/Magento/Framework/Crontab/Test/Unit/CrontabManagerTest.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
78
namespace Magento\Framework\Crontab\Test\Unit;
89

910
use Magento\Framework\Crontab\CrontabManager;
@@ -16,6 +17,9 @@
1617
use Magento\Framework\Filesystem\Directory\ReadInterface;
1718
use Magento\Framework\Filesystem\DriverPool;
1819

20+
/**
21+
* Tests crontab manager functionality.
22+
*/
1923
class CrontabManagerTest extends \PHPUnit\Framework\TestCase
2024
{
2125
/**
@@ -58,7 +62,7 @@ public function testGetTasksNoCrontab()
5862

5963
$this->shellMock->expects($this->once())
6064
->method('execute')
61-
->with('crontab -l', [])
65+
->with('crontab -l 2>/dev/null', [])
6266
->willThrowException($localizedException);
6367

6468
$this->assertEquals([], $this->crontabManager->getTasks());
@@ -74,7 +78,7 @@ public function testGetTasks($content, $tasks)
7478
{
7579
$this->shellMock->expects($this->once())
7680
->method('execute')
77-
->with('crontab -l', [])
81+
->with('crontab -l 2>/dev/null', [])
7882
->willReturn($content);
7983

8084
$this->assertEquals($tasks, $this->crontabManager->getTasks());
@@ -88,17 +92,17 @@ public function getTasksDataProvider()
8892
return [
8993
[
9094
'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
91-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
95+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
9296
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
93-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
97+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
9498
'tasks' => ['* * * * * /bin/php /var/www/magento/bin/magento cron:run'],
9599
],
96100
[
97101
'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
98-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
102+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
99103
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
100104
. '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run' . PHP_EOL
101-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
105+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
102106
'tasks' => [
103107
'* * * * * /bin/php /var/www/magento/bin/magento cron:run',
104108
'* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run',
@@ -127,7 +131,7 @@ public function testRemoveTasksWithException()
127131

128132
$this->shellMock->expects($this->at(0))
129133
->method('execute')
130-
->with('crontab -l', [])
134+
->with('crontab -l 2>/dev/null', [])
131135
->willReturn('');
132136

133137
$this->shellMock->expects($this->at(1))
@@ -148,7 +152,7 @@ public function testRemoveTasks($contentBefore, $contentAfter)
148152
{
149153
$this->shellMock->expects($this->at(0))
150154
->method('execute')
151-
->with('crontab -l', [])
155+
->with('crontab -l 2>/dev/null', [])
152156
->willReturn($contentBefore);
153157

154158
$this->shellMock->expects($this->at(1))
@@ -166,17 +170,17 @@ public function removeTasksDataProvider()
166170
return [
167171
[
168172
'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
169-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
173+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
170174
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
171-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
175+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
172176
'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
173177
],
174178
[
175179
'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
176-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
180+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
177181
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
178182
. '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run' . PHP_EOL
179-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
183+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
180184
'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
181185
],
182186
[
@@ -276,7 +280,7 @@ public function testSaveTasks($tasks, $content, $contentToSave)
276280

277281
$this->shellMock->expects($this->at(0))
278282
->method('execute')
279-
->with('crontab -l', [])
283+
->with('crontab -l 2>/dev/null', [])
280284
->willReturn($content);
281285

282286
$this->shellMock->expects($this->at(1))
@@ -292,9 +296,9 @@ public function testSaveTasks($tasks, $content, $contentToSave)
292296
public function saveTasksDataProvider()
293297
{
294298
$content = '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
295-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
299+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
296300
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
297-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL;
301+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL;
298302

299303
return [
300304
[
@@ -303,52 +307,52 @@ public function saveTasksDataProvider()
303307
],
304308
'content' => $content,
305309
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
306-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
310+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
307311
. '* * * * * ' . PHP_BINARY . ' run.php' . PHP_EOL
308-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
312+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
309313
],
310314
[
311315
'tasks' => [
312316
['expression' => '1 2 3 4 5', 'command' => 'run.php']
313317
],
314318
'content' => $content,
315319
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
316-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
320+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
317321
. '1 2 3 4 5 ' . PHP_BINARY . ' run.php' . PHP_EOL
318-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
322+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
319323
],
320324
[
321325
'tasks' => [
322326
['command' => '{magentoRoot}run.php >> {magentoLog}cron.log']
323327
],
324328
'content' => $content,
325329
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
326-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
330+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
327331
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php >>'
328332
. ' /var/www/magento2/var/log/cron.log' . PHP_EOL
329-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
333+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
330334
],
331335
[
332336
'tasks' => [
333337
['command' => '{magentoRoot}run.php % cron:run | grep -v "Ran \'jobs\' by schedule"']
334338
],
335339
'content' => $content,
336340
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
337-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
341+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
338342
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
339343
. ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
340-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
344+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
341345
],
342346
[
343347
'tasks' => [
344348
['command' => '{magentoRoot}run.php % cron:run | grep -v "Ran \'jobs\' by schedule"']
345349
],
346350
'content' => '* * * * * /bin/php /var/www/cron.php',
347351
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
348-
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
352+
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
349353
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
350354
. ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
351-
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
355+
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
352356
],
353357
];
354358
}

0 commit comments

Comments
 (0)