@@ -233,6 +233,8 @@ function magentoDeployAssetsSplit(string $area)
233233 throw new ConfigurationException ("\$area must be either 'frontend' or 'backend', ' $ area' given " );
234234 }
235235
236+ $ maxProcesses = get ('static_content_jobs ' );
237+
236238 $ isFrontend = $ area === 'frontend ' ;
237239 $ suffix = $ isFrontend
238240 ? ''
@@ -251,18 +253,31 @@ function magentoDeployAssetsSplit(string $area)
251253 ? 'frontend '
252254 : 'adminhtml ' ;
253255
256+ // group themes by their languages to minimize number of static content deploy commands, and allow parallel jobs
257+ $ themeGroups = [];
254258 if ($ useDefaultLanguages ) {
255- $ themes = '-t ' . implode (' -t ' , $ themes );
256-
257- run ("{{bin/php}} {{bin/magento}} setup:static-content:deploy -f --area= $ staticContentArea --content-version={{content_version}} {{static_deploy_options}} $ defaultLanguages $ themes -j {{static_content_jobs}} " );
258- return ;
259+ $ themeGroups [$ defaultLanguages ] = $ themes ;
260+ } else {
261+ foreach ($ themes as $ theme ) {
262+ $ locales = array_unique (array_filter (explode (' ' , parse ($ themesConfig [$ theme ] ?? $ defaultLanguages ))));
263+ sort ($ locales ); // sort locales to ensure consistent grouping
264+ $ localesSorted = implode (' ' , $ locales );
265+ $ themeGroups [$ localesSorted ][] = $ theme ;
266+ }
259267 }
260268
261- foreach ($ themes as $ theme ) {
262- $ languages = parse ($ themesConfig [$ theme ] ?? $ defaultLanguages );
263-
264- run ("{{bin/php}} {{bin/magento}} setup:static-content:deploy -f --area= $ staticContentArea --content-version={{content_version}} {{static_deploy_options}} $ languages -t $ theme -j {{static_content_jobs}} " );
269+ foreach ($ themeGroups as $ locales => $ themes ) {
270+ $ localeCount = substr_count ($ locales , ' ' ) + 1 ;
271+ // how many themes can we process in parallel?
272+ $ maxConcurrentThemes = max (1 , floor ($ maxProcesses / $ localeCount ));
273+ // WARNING: when static_content_jobs>1, and it's doing more than 1 theme-locale per process, it can get stuck - so we do 1batch at a time
274+ do {
275+ $ chunk = array_splice ($ themes , 0 , $ maxConcurrentThemes );
276+ $ themeArgs = '-t ' . implode (' -t ' , $ chunk );
277+ run ("{{bin/php}} {{bin/magento}} setup:static-content:deploy -f --area= $ staticContentArea --content-version={{content_version}} {{static_deploy_options}} $ locales $ themeArgs -j {{static_content_jobs}} " );
278+ } while (!empty ($ themes ));
265279 }
280+
266281}
267282
268283desc ('Syncs content version ' );
0 commit comments