Skip to content

Commit a322b06

Browse files
author
Bohdan Korablov
authored
MAGECLOUD-1485: Deploy static content in compact strategy shouldn't use process-per-locale for pre-start hook (#139)
1 parent 25d6cc5 commit a322b06

File tree

6 files changed

+28
-453
lines changed

6 files changed

+28
-453
lines changed

src/Process/Prestart/DeployStaticContent/Generate.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\MagentoCloud\Process\ProcessInterface;
1212
use Magento\MagentoCloud\Shell\ShellInterface;
1313
use Magento\MagentoCloud\StaticContent\CommandFactory;
14-
use Magento\MagentoCloud\StaticContent\Prestart\Option;
14+
use Magento\MagentoCloud\StaticContent\Deploy\Option;
1515
use Psr\Log\LoggerInterface;
1616

1717
/**
@@ -52,7 +52,7 @@ class Generate implements ProcessInterface
5252
/**
5353
* @var Option
5454
*/
55-
private $prestartOption;
55+
private $deployOption;
5656

5757
/**
5858
* @param ShellInterface $shell
@@ -61,7 +61,7 @@ class Generate implements ProcessInterface
6161
* @param File $file
6262
* @param DirectoryList $directoryList
6363
* @param CommandFactory $commandFactory
64-
* @param Option $prestartOption
64+
* @param Option $deployOption
6565
*/
6666
public function __construct(
6767
ShellInterface $shell,
@@ -70,15 +70,15 @@ public function __construct(
7070
File $file,
7171
DirectoryList $directoryList,
7272
CommandFactory $commandFactory,
73-
Option $prestartOption
73+
Option $deployOption
7474
) {
7575
$this->shell = $shell;
7676
$this->logger = $logger;
7777
$this->environment = $environment;
7878
$this->file = $file;
7979
$this->directoryList = $directoryList;
8080
$this->commandFactory = $commandFactory;
81-
$this->prestartOption = $prestartOption;
81+
$this->deployOption = $deployOption;
8282
}
8383

8484
/**
@@ -89,20 +89,14 @@ public function execute()
8989
$this->file->touch($this->directoryList->getMagentoRoot() . '/pub/static/deployed_version.txt');
9090
$this->logger->info('Extracting locales');
9191

92-
$locales = $this->prestartOption->getLocales();
92+
$locales = $this->deployOption->getLocales();
9393
$logMessage = count($locales) ?
9494
'Generating static content for locales: ' . implode(' ', $locales) :
9595
'Generating static content';
9696

9797
$this->logger->info($logMessage);
9898

99-
$threadCount= $this->prestartOption->getThreadCount();
100-
$parallelCommands = $this->commandFactory->createParallel($this->prestartOption);
101-
102-
$this->shell->execute(sprintf(
103-
"printf '%s' | xargs -I CMD -P %d bash -c CMD",
104-
$parallelCommands,
105-
$threadCount
106-
));
99+
$command = $this->commandFactory->create($this->deployOption);
100+
$this->shell->execute($command);
107101
}
108102
}

src/StaticContent/CommandFactory.php

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,6 @@ class CommandFactory
1717
* @return string
1818
*/
1919
public function create(OptionInterface $option): string
20-
{
21-
$command = $this->createBaseCommand($option);
22-
23-
$locales = $option->getLocales();
24-
if (count($locales)) {
25-
$command .= ' ' . implode(' ', $locales);
26-
}
27-
28-
$treadCount = $option->getThreadCount();
29-
if ($treadCount) {
30-
$command .= ' --jobs=' . $treadCount;
31-
}
32-
33-
return $command;
34-
}
35-
36-
/**
37-
* Creates static deploy command for running in parallel for xargs command
38-
*
39-
* @param OptionInterface $option
40-
* @return string
41-
* @deprecated This method should be removed after replacing xargs with --jobs parameter in build phase
42-
*/
43-
public function createParallel(OptionInterface $option): string
44-
{
45-
$command = $this->createBaseCommand($option);
46-
47-
$parallelCommands = '';
48-
foreach ($option->getLocales() as $locale) {
49-
$parallelCommands .= $command . ' ' . $locale . PHP_EOL;
50-
}
51-
52-
return $parallelCommands;
53-
}
54-
55-
/**
56-
* @param OptionInterface $option
57-
* @return string
58-
*/
59-
private function createBaseCommand(OptionInterface $option): string
6020
{
6121
$command = 'php ./bin/magento setup:static-content:deploy';
6222

@@ -81,6 +41,16 @@ private function createBaseCommand(OptionInterface $option): string
8141
$command .= ' ' . $verbosityLevel;
8242
}
8343

44+
$locales = $option->getLocales();
45+
if (count($locales)) {
46+
$command .= ' ' . implode(' ', $locales);
47+
}
48+
49+
$treadCount = $option->getThreadCount();
50+
if ($treadCount) {
51+
$command .= ' --jobs=' . $treadCount;
52+
}
53+
8454
return $command;
8555
}
8656
}

src/StaticContent/Prestart/Option.php

Lines changed: 0 additions & 138 deletions
This file was deleted.

src/Test/Unit/Process/Prestart/DeployStaticContent/GenerateTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\MagentoCloud\Filesystem\Driver\File;
1111
use Magento\MagentoCloud\Process\Prestart\DeployStaticContent\Generate;
1212
use Magento\MagentoCloud\Shell\ShellInterface;
13-
use Magento\MagentoCloud\StaticContent\Prestart\Option;
13+
use Magento\MagentoCloud\StaticContent\Deploy\Option;
1414
use Magento\MagentoCloud\StaticContent\CommandFactory;
1515
use PHPUnit\Framework\TestCase;
1616
use PHPUnit_Framework_MockObject_MockObject as Mock;
@@ -59,7 +59,7 @@ class GenerateTest extends TestCase
5959
/**
6060
* @var Option|Mock
6161
*/
62-
private $prestartOptionMock;
62+
private $deployOptionMock;
6363

6464
/**
6565
* @inheritdoc
@@ -74,7 +74,7 @@ protected function setUp()
7474
$this->directoryListMock = $this->createMock(DirectoryList::class);
7575
$this->environmentMock = $this->createMock(Environment::class);
7676
$this->commandFactoryMock = $this->createMock(CommandFactory::class);
77-
$this->prestartOptionMock = $this->createMock(Option::class);
77+
$this->deployOptionMock = $this->createMock(Option::class);
7878

7979
$this->process = new Generate(
8080
$this->shellMock,
@@ -83,18 +83,15 @@ protected function setUp()
8383
$this->fileMock,
8484
$this->directoryListMock,
8585
$this->commandFactoryMock,
86-
$this->prestartOptionMock
86+
$this->deployOptionMock
8787
);
8888
}
8989

9090
public function testExecute()
9191
{
92-
$this->prestartOptionMock->expects($this->once())
92+
$this->deployOptionMock->expects($this->once())
9393
->method('getLocales')
9494
->willReturn(['ua_UA', 'fr_FR', 'es_ES', 'en_US']);
95-
$this->prestartOptionMock->expects($this->once())
96-
->method('getThreadCount')
97-
->willReturn(3);
9895
$this->directoryListMock->method('getMagentoRoot')
9996
->willReturn('magento_root');
10097
$this->fileMock->expects($this->once())
@@ -106,12 +103,12 @@ public function testExecute()
106103
['Generating static content for locales: en_GB fr_FR']
107104
);
108105
$this->commandFactoryMock->expects($this->once())
109-
->method('createParallel')
110-
->with($this->prestartOptionMock)
111-
->willReturn('some parallel command');
106+
->method('create')
107+
->with($this->deployOptionMock)
108+
->willReturn('some command');
112109
$this->shellMock->expects($this->once())
113110
->method('execute')
114-
->with("printf 'some parallel command' | xargs -I CMD -P 3 bash -c CMD");
111+
->with('some command');
115112

116113
$this->process->execute();
117114
}

0 commit comments

Comments
 (0)