Skip to content

Commit 333ca0d

Browse files
committed
MAGETWO-52660: Improve performance of static assets deployment
1 parent e91dfff commit 333ca0d

File tree

13 files changed

+100
-54
lines changed

13 files changed

+100
-54
lines changed

app/code/Magento/Deploy/Model/Deploy/DeployInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
interface DeployInterface
1010
{
11+
/**
12+
* Base locale option without customizations
13+
*/
14+
const DEPLOY_BASE_LOCALE = 'deploy_base_locale';
15+
1116
/**
1217
* @param string $area
1318
* @param string $themePath

app/code/Magento/Deploy/Model/Deploy/LocaleDeploy.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,34 @@
2525
*/
2626
class LocaleDeploy implements DeployInterface
2727
{
28-
/** @var int */
28+
/**
29+
* @var int
30+
*/
2931
private $count = 0;
3032

31-
/** @var int */
33+
/**
34+
* @var int
35+
*/
3236
private $errorCount = 0;
3337

3438
/**
3539
* @var OutputInterface
3640
*/
3741
private $output;
3842

39-
/** @var \Magento\Framework\View\Asset\Repository */
43+
/**
44+
* @var \Magento\Framework\View\Asset\Repository
45+
*/
4046
private $assetRepo;
4147

42-
/** @var Publisher */
48+
/**
49+
* @var Publisher
50+
*/
4351
private $assetPublisher;
4452

45-
/** @var \Magento\Framework\View\Asset\Bundle\Manager */
53+
/**
54+
* @var \Magento\Framework\View\Asset\Bundle\Manager
55+
*/
4656
private $bundleManager;
4757

4858
/**

app/code/Magento/Deploy/Model/Deploy/LocaleQuickDeploy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ public function deploy($area, $themePath, $locale)
7373

7474
$this->output->writeln("=== {$area} -> {$themePath} -> {$locale} ===");
7575

76-
if (!isset($this->options[DeployManager::DEPLOY_BASE_LOCALE])) {
76+
if (!isset($this->options[self::DEPLOY_BASE_LOCALE])) {
7777
throw new \InvalidArgumentException('Deploy base locale must be set for Quick Deploy');
7878
}
7979
$processedFiles = 0;
8080
$errorAmount = 0;
8181

82-
$baseLocale = $this->options[DeployManager::DEPLOY_BASE_LOCALE];
82+
$baseLocale = $this->options[self::DEPLOY_BASE_LOCALE];
8383
$newLocalePath = $this->getLocalePath($area, $themePath, $locale);
8484
$baseLocalePath = $this->getLocalePath($area, $themePath, $baseLocale);
8585
$baseRequireJsPath = RequireJsConfig::DIR_NAME . DIRECTORY_SEPARATOR . $baseLocalePath;

app/code/Magento/Deploy/Model/DeployManager.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@
1010
use Symfony\Component\Console\Output\OutputInterface;
1111
use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;
1212
use Magento\Deploy\Model\Deploy\TemplateMinifier;
13-
use \Magento\Framework\App\State;
13+
use Magento\Framework\App\State;
1414

1515
class DeployManager
1616
{
17-
/**
18-
* Base locale without customizations
19-
*/
20-
const DEPLOY_BASE_LOCALE = 'deploy_base_locale';
21-
2217
/**
2318
* @var array
2419
*/
@@ -45,12 +40,12 @@ class DeployManager
4540
private $deployStrategyProviderFactory;
4641

4742
/**
48-
* @var ProcessQueueManager
43+
* @var ProcessQueueManagerFactory
4944
*/
50-
private $processQueueManager;
45+
private $processQueueManagerFactory;
5146

5247
/**
53-
* @var \Magento\Deploy\Model\TemplateMinifier
48+
* @var TemplateMinifier
5449
*/
5550
private $templateMinifier;
5651

@@ -68,7 +63,7 @@ class DeployManager
6863
* @param OutputInterface $output
6964
* @param StorageInterface $versionStorage
7065
* @param DeployStrategyProviderFactory $deployStrategyProviderFactory
71-
* @param ProcessQueueManager $processQueueManager
66+
* @param ProcessQueueManagerFactory $processQueueManagerFactory
7267
* @param TemplateMinifier $templateMinifier
7368
* @param State $state
7469
* @param array $options
@@ -77,7 +72,7 @@ public function __construct(
7772
OutputInterface $output,
7873
StorageInterface $versionStorage,
7974
DeployStrategyProviderFactory $deployStrategyProviderFactory,
80-
ProcessQueueManager $processQueueManager,
75+
ProcessQueueManagerFactory $processQueueManagerFactory,
8176
TemplateMinifier $templateMinifier,
8277
State $state,
8378
array $options
@@ -86,9 +81,10 @@ public function __construct(
8681
$this->options = $options;
8782
$this->versionStorage = $versionStorage;
8883
$this->deployStrategyProviderFactory = $deployStrategyProviderFactory;
89-
$this->processQueueManager = $processQueueManager;
84+
$this->processQueueManagerFactory = $processQueueManagerFactory;
9085
$this->templateMinifier = $templateMinifier;
9186
$this->state = $state;
87+
$this->idDryRun = !empty($this->options[Options::DRY_RUN]);
9288
}
9389

9490
/**
@@ -110,7 +106,6 @@ public function addPack($area, $themePath, $locale)
110106
*/
111107
public function deploy()
112108
{
113-
$this->idDryRun = isset($this->options[Options::DRY_RUN]) && $this->options[Options::DRY_RUN];
114109
if ($this->idDryRun) {
115110
$this->output->writeln('Dry run. Nothing will be recorded to the target directory.');
116111
}
@@ -123,7 +118,7 @@ public function deploy()
123118
if ($this->isCanBeParalleled()) {
124119
$result = $this->runInParallel($strategyProvider);
125120
} else {
126-
$result = null;
121+
$result = 0;
127122
foreach ($this->packages as $package) {
128123
$locales = array_keys($package);
129124
list($area, $themePath) = current($package);
@@ -162,7 +157,9 @@ private function minifyTemplates()
162157
*/
163158
private function runInParallel(DeployStrategyProvider $strategyProvider)
164159
{
165-
$this->processQueueManager->setMaxProcessesAmount($this->getProcessesAmount());
160+
$processQueueManager = $this->processQueueManagerFactory->create(
161+
['maxProcesses' => $this->getProcessesAmount()]
162+
);
166163
foreach ($this->packages as $package) {
167164
$locales = array_keys($package);
168165
list($area, $themePath) = current($package);
@@ -179,10 +176,10 @@ private function runInParallel(DeployStrategyProvider $strategyProvider)
179176
}
180177

181178
}
182-
$this->processQueueManager->addTaskToQueue($baseStrategy, $dependentStrategy);
179+
$processQueueManager->addTaskToQueue($baseStrategy, $dependentStrategy);
183180
}
184181

185-
return $this->processQueueManager->process();
182+
return $processQueueManager->process();
186183
}
187184

188185
/**

app/code/Magento/Deploy/Model/DeployStrategyFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(ObjectManagerInterface $objectManager)
3333
* @param string $type
3434
* @param array $arguments
3535
* @return DeployInterface
36+
* @throws \InvalidArgumentException
3637
*/
3738
public function create($type, array $arguments = [])
3839
{

app/code/Magento/Deploy/Model/DeployStrategyProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Deploy\Model\Deploy\DeployInterface;
1010
use Magento\Framework\Component\ComponentRegistrar;
11+
use Magento\Framework\Module\Dir;
1112
use Magento\Framework\View\Design\Fallback\Rule\RuleInterface;
1213
use Magento\Framework\View\DesignInterface;
1314
use Magento\Framework\View\Design\Fallback\RulePool;
@@ -122,7 +123,7 @@ private function getLocaleDirectories($params)
122123
$dirs = $this->getFallbackRule()->getPatternDirs($params);
123124

124125
return array_filter($dirs, function ($dir) {
125-
return strpos($dir, 'i18n');
126+
return strpos($dir, Dir::MODULE_I18N_DIR);
126127
});
127128
}
128129

@@ -181,7 +182,7 @@ private function getDeployStrategy($type, $baseLocale = null)
181182
{
182183
$options = $this->options;
183184
if ($baseLocale) {
184-
$options[DeployManager::DEPLOY_BASE_LOCALE] = $baseLocale;
185+
$options[DeployInterface::DEPLOY_BASE_LOCALE] = $baseLocale;
185186
}
186187

187188
return $this->deployStrategyFactory->create(

app/code/Magento/Deploy/Model/Deployer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ public function __construct(
7373
}
7474
}
7575

76+
/**
77+
* @return \Magento\Deploy\Model\DeployManagerFactory
78+
*/
79+
private function getDeployManagerFactory()
80+
{
81+
if (null === $this->deployManagerFactory) {
82+
$this->deployManagerFactory = ObjectManager::getInstance()->get(DeployManagerFactory::class);
83+
}
84+
85+
return $this->deployManagerFactory;
86+
}
87+
7688
/**
7789
* Populate all static view files for specified root path and list of languages
7890
*
@@ -85,11 +97,8 @@ public function __construct(
8597
*/
8698
public function deploy(ObjectManagerFactory $omFactory, array $locales, array $deployableAreaThemeMap = [])
8799
{
88-
if (null === $this->deployManagerFactory) {
89-
$this->deployManagerFactory = ObjectManager::getInstance()->get(DeployManagerFactory::class);
90-
}
91100
/** @var DeployManager $deployerManager */
92-
$deployerManager = $this->deployManagerFactory->create(
101+
$deployerManager = $this->getDeployManagerFactory()->create(
93102
['options' => $this->options, 'output' => $this->output]
94103
);
95104

app/code/Magento/Deploy/Model/ProcessManager.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ class ProcessManager
1111
/** @var Process[] */
1212
private $processes = [];
1313

14+
/**
15+
* @var ProcessFactory
16+
*/
17+
private $processFactory;
18+
19+
/**
20+
* ProcessManager constructor.
21+
* @param ProcessFactory $processFactory
22+
*/
23+
public function __construct(ProcessFactory $processFactory)
24+
{
25+
$this->processFactory = $processFactory;
26+
}
27+
1428
/**
1529
* Forks the currently running process.
1630
*
@@ -66,11 +80,11 @@ public function delete(Process $process)
6680
*/
6781
private function createProcess(callable $handler)
6882
{
69-
return new Process($handler);
83+
return $this->processFactory->create(['handler' => $handler]);
7084
}
7185

7286
/**
73-
* Protect against zombie process
87+
* Protect against zombie process§
7488
* @return void
7589
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
7690
*/

app/code/Magento/Deploy/Model/ProcessQueueManager.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
class ProcessQueueManager
1212
{
13+
/**
14+
* Default max amount of processes
15+
*/
16+
const DEFAULT_MAX_PROCESSES_AMOUNT = 4;
17+
1318
/**
1419
* @var ProcessTask[]
1520
*/
@@ -23,7 +28,7 @@ class ProcessQueueManager
2328
/**
2429
* @var int
2530
*/
26-
private $maxProcesses = 4;
31+
private $maxProcesses;
2732

2833
/**
2934
* @var ProcessManager
@@ -36,21 +41,25 @@ class ProcessQueueManager
3641
private $resourceConnection;
3742

3843
/**
39-
* @param ProcessManager $processManager
40-
* @param ResourceConnection $resourceConnection
44+
* @var ProcessTaskFactory
4145
*/
42-
public function __construct(ProcessManager $processManager, ResourceConnection $resourceConnection)
43-
{
44-
$this->processManager = $processManager;
45-
$this->resourceConnection = $resourceConnection;
46-
}
46+
private $processTaskFactory;
4747

4848
/**
49+
* @param ProcessManager $processManager
50+
* @param ResourceConnection $resourceConnection
51+
* @param ProcessTaskFactory $processTaskFactory
4952
* @param int $maxProcesses
50-
* @return void
5153
*/
52-
public function setMaxProcessesAmount($maxProcesses)
53-
{
54+
public function __construct(
55+
ProcessManager $processManager,
56+
ResourceConnection $resourceConnection,
57+
ProcessTaskFactory $processTaskFactory,
58+
$maxProcesses = self::DEFAULT_MAX_PROCESSES_AMOUNT
59+
) {
60+
$this->processManager = $processManager;
61+
$this->resourceConnection = $resourceConnection;
62+
$this->processTaskFactory = $processTaskFactory;
5463
$this->maxProcesses = $maxProcesses;
5564
}
5665

@@ -78,7 +87,7 @@ public function process()
7887
$processQueue = [];
7988
$this->internalQueueProcess($this->tasksQueue, $processQueue);
8089

81-
$returnStatus = null;
90+
$returnStatus = 0;
8291
while (count($this->processManager->getProcesses()) > 0) {
8392
foreach ($this->processManager->getProcesses() as $process) {
8493
if ($process->isCompleted()) {
@@ -131,7 +140,7 @@ private function internalQueueProcess($taskQueue, &$processQueue)
131140
*/
132141
private function createTask($handler, $dependentTasks = [])
133142
{
134-
return new ProcessTask($handler, $dependentTasks);
143+
return $this->processTaskFactory->create(['handler' => $handler, 'dependentTasks' => $dependentTasks]);
135144
}
136145

137146
/**

app/code/Magento/Deploy/Model/ProcessTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ProcessTask
2727
* @param callable $handler
2828
* @param array $dependentTasks
2929
*/
30-
public function __construct($handler, $dependentTasks = [])
30+
public function __construct($handler, array $dependentTasks = [])
3131
{
3232
$this->taskId = uniqid('', true);
3333
$this->handler = $handler;

0 commit comments

Comments
 (0)