Skip to content

Commit c919148

Browse files
ENGCOM-4438: Remove Environment emulation for better performance on sitemap generation #21542
- Merge Pull Request #21542 from Nazar65/magento2:refactoring-sitemap-generation - Merged commits: 1. 6c820d3
2 parents 3a4ba66 + 6c820d3 commit c919148

File tree

4 files changed

+6
-39
lines changed

4 files changed

+6
-39
lines changed

app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Store\Model\App\Emulation;
1111
use Magento\Framework\App\ObjectManager;
1212

13+
/**
14+
* Generate sitemap file
15+
*/
1316
class Generate extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
1417
{
1518
/** @var \Magento\Store\Model\App\Emulation $appEmulation */
@@ -44,23 +47,14 @@ public function execute()
4447
// if sitemap record exists
4548
if ($sitemap->getId()) {
4649
try {
47-
//We need to emulate to get the correct frontend URL for the product images
48-
$this->appEmulation->startEnvironmentEmulation(
49-
$sitemap->getStoreId(),
50-
\Magento\Framework\App\Area::AREA_FRONTEND,
51-
true
52-
);
5350
$sitemap->generateXml();
54-
5551
$this->messageManager->addSuccessMessage(
5652
__('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename())
5753
);
5854
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5955
$this->messageManager->addErrorMessage($e->getMessage());
6056
} catch (\Exception $e) {
6157
$this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.'));
62-
} finally {
63-
$this->appEmulation->stopEnvironmentEmulation();
6458
}
6559
} else {
6660
$this->messageManager->addErrorMessage(__('We can\'t find a sitemap to generate.'));

app/code/Magento/Sitemap/Model/Observer.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Sitemap\Model;
77

8-
use Magento\Store\Model\App\Emulation;
98
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
109
use Magento\Framework\App\Config\ScopeConfigInterface;
1110
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
@@ -57,11 +56,6 @@ class Observer
5756
*/
5857
private $collectionFactory;
5958

60-
/**
61-
* @var Emulation
62-
*/
63-
private $appEmulation;
64-
6559
/**
6660
* @var $emailNotification
6761
*/
@@ -72,17 +66,14 @@ class Observer
7266
* @param ScopeConfigInterface $scopeConfig
7367
* @param CollectionFactory $collectionFactory
7468
* @param EmailNotification $emailNotification
75-
* @param Emulation $appEmulation
7669
*/
7770
public function __construct(
7871
ScopeConfigInterface $scopeConfig,
7972
CollectionFactory $collectionFactory,
80-
SitemapEmail $emailNotification,
81-
Emulation $appEmulation
73+
SitemapEmail $emailNotification
8274
) {
8375
$this->scopeConfig = $scopeConfig;
8476
$this->collectionFactory = $collectionFactory;
85-
$this->appEmulation = $appEmulation;
8677
$this->emailNotification = $emailNotification;
8778
}
8879

@@ -114,17 +105,9 @@ public function scheduledGenerateSitemaps()
114105
foreach ($collection as $sitemap) {
115106
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */
116107
try {
117-
$this->appEmulation->startEnvironmentEmulation(
118-
$sitemap->getStoreId(),
119-
\Magento\Framework\App\Area::AREA_FRONTEND,
120-
true
121-
);
122-
123108
$sitemap->generateXml();
124109
} catch (\Exception $e) {
125110
$errors[] = $e->getMessage();
126-
} finally {
127-
$this->appEmulation->stopEnvironmentEmulation();
128111
}
129112
}
130113
if ($errors && $recipient) {

app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ protected function _prepareProduct(array $productRow, $storeId)
389389
*/
390390
protected function _loadProductImages($product, $storeId)
391391
{
392+
$this->_storeManager->setCurrentStore($storeId);
392393
/** @var $helper \Magento\Sitemap\Helper\Data */
393394
$helper = $this->_sitemapData;
394395
$imageIncludePolicy = $helper->getProductImageIncludePolicy($storeId);

app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail()
118118
->method('getStoreId')
119119
->willReturn($storeId);
120120

121-
$this->sitemapMock->expects($this->at(1))
121+
$this->sitemapMock->expects($this->once())
122122
->method('generateXml')
123123
->willThrowException(new \Exception($exception));
124124

@@ -130,17 +130,6 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail()
130130
)
131131
->willReturn('[email protected]');
132132

133-
$this->appEmulationMock->expects($this->at(0))
134-
->method('startEnvironmentEmulation')
135-
->with(
136-
$storeId,
137-
Area::AREA_FRONTEND,
138-
true
139-
);
140-
141-
$this->appEmulationMock->expects($this->at(1))
142-
->method('stopEnvironmentEmulation');
143-
144133
$this->observer->scheduledGenerateSitemaps();
145134
}
146135
}

0 commit comments

Comments
 (0)