Skip to content

Commit 7800e37

Browse files
authored
Merge pull request #389 from magento-performance/MAGETWO-52660
MAGETWO-52660: [Performance] Improve performance of static assets deployment
2 parents 152dbdc + ddbdfff commit 7800e37

39 files changed

+2506
-651
lines changed

app/code/Magento/Deploy/Console/Command/DeployStaticContentCommand.php

Lines changed: 41 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
use Magento\Framework\App\ObjectManagerFactory;
1616
use Magento\Framework\ObjectManagerInterface;
1717
use Magento\Framework\Validator\Locale;
18-
use Magento\Framework\Console\Cli;
19-
use Magento\Deploy\Model\ProcessManager;
20-
use Magento\Deploy\Model\Process;
2118
use Magento\Framework\Exception\LocalizedException;
2219
use Magento\Framework\App\State;
2320
use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;
21+
use Magento\Deploy\Model\DeployManager;
2422

2523
/**
2624
* Deploy static content command
@@ -121,109 +119,116 @@ protected function configure()
121119
Options::FORCE_RUN,
122120
'-f',
123121
InputOption::VALUE_NONE,
124-
'If specified, then run files will be deployed in any mode.'
122+
'Deploy files in any mode.'
125123
),
126124
new InputOption(
127125
Options::NO_JAVASCRIPT,
128126
null,
129127
InputOption::VALUE_NONE,
130-
'If specified, no JavaScript will be deployed.'
128+
'Do not deploy JavaScript files'
131129
),
132130
new InputOption(
133131
Options::NO_CSS,
134132
null,
135133
InputOption::VALUE_NONE,
136-
'If specified, no CSS will be deployed.'
134+
'Do not deploy CSS files.'
137135
),
138136
new InputOption(
139137
Options::NO_LESS,
140138
null,
141139
InputOption::VALUE_NONE,
142-
'If specified, no LESS will be deployed.'
140+
'Do not deploy LESS files.'
143141
),
144142
new InputOption(
145143
Options::NO_IMAGES,
146144
null,
147145
InputOption::VALUE_NONE,
148-
'If specified, no images will be deployed.'
146+
'Do not deploy images.'
149147
),
150148
new InputOption(
151149
Options::NO_FONTS,
152150
null,
153151
InputOption::VALUE_NONE,
154-
'If specified, no font files will be deployed.'
152+
'Do not deploy font files.'
155153
),
156154
new InputOption(
157155
Options::NO_HTML,
158156
null,
159157
InputOption::VALUE_NONE,
160-
'If specified, no html files will be deployed.'
158+
'Do not deploy HTML files.'
161159
),
162160
new InputOption(
163161
Options::NO_MISC,
164162
null,
165163
InputOption::VALUE_NONE,
166-
'If specified, no miscellaneous files will be deployed.'
164+
'Do not deploy other types of files (.md, .jbf, .csv, etc...).'
167165
),
168166
new InputOption(
169167
Options::NO_HTML_MINIFY,
170168
null,
171169
InputOption::VALUE_NONE,
172-
'If specified, html will not be minified.'
170+
'Do not minify HTML files.'
173171
),
174172
new InputOption(
175173
Options::THEME,
176174
'-t',
177175
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
178-
'If specified, just specific theme(s) will be actually deployed.',
176+
'Generate static view files for only the specified themes.',
179177
['all']
180178
),
181179
new InputOption(
182180
Options::EXCLUDE_THEME,
183181
null,
184182
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
185-
'If specified, exclude specific theme(s) from deployment.',
183+
'Do not generate files for the specified themes.',
186184
['none']
187185
),
188186
new InputOption(
189187
Options::LANGUAGE,
190188
'-l',
191189
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
192-
'List of languages you want the tool populate files for.',
190+
'Generate files only for the specified languages.',
193191
['all']
194192
),
195193
new InputOption(
196194
Options::EXCLUDE_LANGUAGE,
197195
null,
198196
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
199-
'List of langiages you do not want the tool populate files for.',
197+
'Do not generate files for the specified languages.',
200198
['none']
201199
),
202200
new InputOption(
203201
Options::AREA,
204202
'-a',
205203
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
206-
'List of areas you want the tool populate files for.',
204+
'Generate files only for the specified areas.',
207205
['all']
208206
),
209207
new InputOption(
210208
Options::EXCLUDE_AREA,
211209
null,
212210
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
213-
'List of areas you do not want the tool populate files for.',
211+
'Do not generate files for the specified areas.',
214212
['none']
215213
),
216214
new InputOption(
217215
Options::JOBS_AMOUNT,
218216
'-j',
219217
InputOption::VALUE_OPTIONAL,
220-
'Amount of jobs to which script can be paralleled.',
218+
'Enable parallel processing using the specified number of jobs.',
221219
self::DEFAULT_JOBS_AMOUNT
222220
),
221+
new InputOption(
222+
Options::SYMLINK_LOCALE,
223+
null,
224+
InputOption::VALUE_NONE,
225+
'Create symlinks for the files of those locales, which are passed for deployment, '
226+
. 'but have no customizations'
227+
),
223228
new InputArgument(
224229
self::LANGUAGES_ARGUMENT,
225230
InputArgument::IS_ARRAY,
226-
'List of languages you want the tool populate files for.'
231+
'Space-separated list of ISO-636 language codes for which to output static view files.'
227232
),
228233
]);
229234

@@ -374,8 +379,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
374379
if (!$input->getOption(Options::FORCE_RUN) && $this->getAppState()->getMode() !== State::MODE_PRODUCTION) {
375380
throw new LocalizedException(
376381
__(
377-
"Deploy static content is applicable only for production mode.\n"
378-
. "Please use command 'bin/magento deploy:mode:set production' for set up production mode."
382+
'NOTE: Manual static content deployment is not required in "default" and "developer" modes.'
383+
. PHP_EOL . 'In "default" and "developer" modes static contents are being deployed '
384+
. 'automatically on demand.'
385+
. PHP_EOL . 'If you still want to deploy in these modes, use -f option: '
386+
. "'bin/magento setup:static-content:deploy -f'"
379387
)
380388
);
381389
}
@@ -390,20 +398,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
390398
$output->writeln("Requested areas: " . implode(', ', array_keys($deployableAreaThemeMap)));
391399
$output->writeln("Requested themes: " . implode(', ', $requestedThemes));
392400

393-
$deployer = $this->objectManager->create(
394-
\Magento\Deploy\Model\Deployer::class,
401+
/** @var $deployManager DeployManager */
402+
$deployManager = $this->objectManager->create(
403+
DeployManager::class,
395404
[
396-
'filesUtil' => $filesUtil,
397405
'output' => $output,
398406
'options' => $this->input->getOptions(),
399407
]
400408
);
401409

402-
if ($this->isCanBeParalleled()) {
403-
return $this->runProcessesInParallel($deployer, $deployableAreaThemeMap, $deployableLanguages);
404-
} else {
405-
return $this->deploy($deployer, $deployableLanguages, $deployableAreaThemeMap);
410+
foreach ($deployableAreaThemeMap as $area => $themes) {
411+
foreach ($deployableLanguages as $locale) {
412+
foreach ($themes as $themePath) {
413+
$deployManager->addPack($area, $themePath, $locale);
414+
}
415+
}
406416
}
417+
418+
return $deployManager->deploy();
407419
}
408420

409421
/**
@@ -464,89 +476,4 @@ private function prepareDeployableEntities($filesUtil)
464476

465477
return [$deployableLanguages, $deployableAreaThemeMap, $requestedThemes];
466478
}
467-
468-
/**
469-
* @param \Magento\Deploy\Model\Deployer $deployer
470-
* @param array $deployableLanguages
471-
* @param array $deployableAreaThemeMap
472-
* @return int
473-
*/
474-
private function deploy($deployer, $deployableLanguages, $deployableAreaThemeMap)
475-
{
476-
return $deployer->deploy(
477-
$this->objectManagerFactory,
478-
$deployableLanguages,
479-
$deployableAreaThemeMap
480-
);
481-
}
482-
483-
/**
484-
* @param \Magento\Deploy\Model\Deployer $deployer
485-
* @param array $deployableAreaThemeMap
486-
* @param array $deployableLanguages
487-
* @return int
488-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
489-
*/
490-
private function runProcessesInParallel($deployer, $deployableAreaThemeMap, $deployableLanguages)
491-
{
492-
/** @var ProcessManager $processManager */
493-
$processManager = $this->objectManager->create(ProcessManager::class);
494-
$processNumber = 0;
495-
$processQueue = [];
496-
foreach ($deployableAreaThemeMap as $area => &$themes) {
497-
foreach ($themes as $theme) {
498-
foreach ($deployableLanguages as $lang) {
499-
$deployerFunc = function (Process $process) use ($area, $theme, $lang, $deployer) {
500-
return $this->deploy($deployer, [$lang], [$area => [$theme]]);
501-
};
502-
if ($processNumber >= $this->getProcessesAmount()) {
503-
$processQueue[] = $deployerFunc;
504-
} else {
505-
$processManager->fork($deployerFunc);
506-
}
507-
$processNumber++;
508-
}
509-
}
510-
}
511-
$returnStatus = null;
512-
while (count($processManager->getProcesses()) > 0) {
513-
foreach ($processManager->getProcesses() as $process) {
514-
if ($process->isCompleted()) {
515-
$processManager->delete($process);
516-
$returnStatus |= $process->getStatus();
517-
if ($queuedProcess = array_shift($processQueue)) {
518-
$processManager->fork($queuedProcess);
519-
}
520-
if (count($processManager->getProcesses()) >= $this->getProcessesAmount()) {
521-
break 1;
522-
}
523-
}
524-
}
525-
usleep(5000);
526-
}
527-
528-
return $returnStatus === Cli::RETURN_SUCCESS ?: Cli::RETURN_FAILURE;
529-
}
530-
531-
/**
532-
* @return bool
533-
*/
534-
private function isCanBeParalleled()
535-
{
536-
return function_exists('pcntl_fork') && $this->getProcessesAmount() > 1;
537-
}
538-
539-
/**
540-
* @return int
541-
*/
542-
private function getProcessesAmount()
543-
{
544-
$jobs = (int)$this->input->getOption(Options::JOBS_AMOUNT);
545-
if ($jobs < 1) {
546-
throw new \InvalidArgumentException(
547-
Options::JOBS_AMOUNT . ' argument has invalid value. It must be greater than 0'
548-
);
549-
}
550-
return $jobs;
551-
}
552479
}

app/code/Magento/Deploy/Console/Command/DeployStaticOptionsInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,9 @@ interface DeployStaticOptionsInterface
9292
* Force run of static deploy
9393
*/
9494
const FORCE_RUN = 'force';
95+
96+
/**
97+
* Symlink locale if it not customized
98+
*/
99+
const SYMLINK_LOCALE = 'symlink-locale';
95100
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Deploy\Model\Deploy;
8+
9+
interface DeployInterface
10+
{
11+
/**
12+
* Base locale option without customizations
13+
*/
14+
const DEPLOY_BASE_LOCALE = 'deploy_base_locale';
15+
16+
/**
17+
* @param string $area
18+
* @param string $themePath
19+
* @param string $locale
20+
* @return int
21+
*/
22+
public function deploy($area, $themePath, $locale);
23+
}

0 commit comments

Comments
 (0)