Skip to content

Commit

Permalink
ENGCOM-4438: Remove Environment emulation for better performance on s…
Browse files Browse the repository at this point in the history
…itemap generation #21542
  • Loading branch information
sidolov authored Mar 13, 2019
2 parents 112c93f + 786ac3a commit 5f30aeb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 40 deletions.
16 changes: 6 additions & 10 deletions app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;

use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Sitemap\Controller\Adminhtml\Sitemap;
use Magento\Store\Model\App\Emulation;
use Magento\Framework\App\ObjectManager;

class Generate extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
/**
* Generate sitemap file
*/
class Generate extends Sitemap implements HttpGetActionInterface
{
/** @var \Magento\Store\Model\App\Emulation $appEmulation */
private $appEmulation;
Expand Down Expand Up @@ -44,23 +49,14 @@ public function execute()
// if sitemap record exists
if ($sitemap->getId()) {
try {
//We need to emulate to get the correct frontend URL for the product images
$this->appEmulation->startEnvironmentEmulation(
$sitemap->getStoreId(),
\Magento\Framework\App\Area::AREA_FRONTEND,
true
);
$sitemap->generateXml();

$this->messageManager->addSuccessMessage(
__('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename())
);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.'));
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}
} else {
$this->messageManager->addErrorMessage(__('We can\'t find a sitemap to generate.'));
Expand Down
19 changes: 1 addition & 18 deletions app/code/Magento/Sitemap/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
namespace Magento\Sitemap\Model;

use Magento\Store\Model\App\Emulation;
use Magento\Sitemap\Model\EmailNotification as SitemapEmail;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
Expand Down Expand Up @@ -57,11 +56,6 @@ class Observer
*/
private $collectionFactory;

/**
* @var Emulation
*/
private $appEmulation;

/**
* @var $emailNotification
*/
Expand All @@ -72,17 +66,14 @@ class Observer
* @param ScopeConfigInterface $scopeConfig
* @param CollectionFactory $collectionFactory
* @param EmailNotification $emailNotification
* @param Emulation $appEmulation
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
CollectionFactory $collectionFactory,
SitemapEmail $emailNotification,
Emulation $appEmulation
SitemapEmail $emailNotification
) {
$this->scopeConfig = $scopeConfig;
$this->collectionFactory = $collectionFactory;
$this->appEmulation = $appEmulation;
$this->emailNotification = $emailNotification;
}

Expand Down Expand Up @@ -114,17 +105,9 @@ public function scheduledGenerateSitemaps()
foreach ($collection as $sitemap) {
/* @var $sitemap \Magento\Sitemap\Model\Sitemap */
try {
$this->appEmulation->startEnvironmentEmulation(
$sitemap->getStoreId(),
\Magento\Framework\App\Area::AREA_FRONTEND,
true
);

$sitemap->generateXml();
} catch (\Exception $e) {
$errors[] = $e->getMessage();
} finally {
$this->appEmulation->stopEnvironmentEmulation();
}
}
if ($errors && $recipient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ protected function _prepareProduct(array $productRow, $storeId)
*/
protected function _loadProductImages($product, $storeId)
{
$this->_storeManager->setCurrentStore($storeId);
/** @var $helper \Magento\Sitemap\Helper\Data */
$helper = $this->_sitemapData;
$imageIncludePolicy = $helper->getProductImageIncludePolicy($storeId);
Expand Down
13 changes: 1 addition & 12 deletions app/code/Magento/Sitemap/Test/Unit/Model/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail()
->method('getStoreId')
->willReturn($storeId);

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

Expand All @@ -130,17 +130,6 @@ public function testScheduledGenerateSitemapsSendsExceptionEmail()
)
->willReturn('error-recipient@example.com');

$this->appEmulationMock->expects($this->at(0))
->method('startEnvironmentEmulation')
->with(
$storeId,
Area::AREA_FRONTEND,
true
);

$this->appEmulationMock->expects($this->at(1))
->method('stopEnvironmentEmulation');

$this->observer->scheduledGenerateSitemaps();
}
}

0 comments on commit 5f30aeb

Please sign in to comment.