Skip to content

Commit 5f30aeb

Browse files
authored
ENGCOM-4438: Remove Environment emulation for better performance on sitemap generation #21542
2 parents 112c93f + 786ac3a commit 5f30aeb

File tree

4 files changed

+9
-40
lines changed

4 files changed

+9
-40
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;
88

99
use Magento\Backend\App\Action;
10+
use Magento\Framework\App\Action\HttpGetActionInterface;
11+
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
1012
use Magento\Store\Model\App\Emulation;
1113
use Magento\Framework\App\ObjectManager;
1214

13-
class Generate extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
15+
/**
16+
* Generate sitemap file
17+
*/
18+
class Generate extends Sitemap implements HttpGetActionInterface
1419
{
1520
/** @var \Magento\Store\Model\App\Emulation $appEmulation */
1621
private $appEmulation;
@@ -44,23 +49,14 @@ public function execute()
4449
// if sitemap record exists
4550
if ($sitemap->getId()) {
4651
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-
);
5352
$sitemap->generateXml();
54-
5553
$this->messageManager->addSuccessMessage(
5654
__('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename())
5755
);
5856
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5957
$this->messageManager->addErrorMessage($e->getMessage());
6058
} catch (\Exception $e) {
6159
$this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.'));
62-
} finally {
63-
$this->appEmulation->stopEnvironmentEmulation();
6460
}
6561
} else {
6662
$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)