|
| 1 | +diff -Nuar a/vendor/magento/module-sitemap/Model/Observer.php b/vendor/magento/module-sitemap/Model/Observer.php |
| 2 | +--- a/vendor/magento/module-sitemap/Model/Observer.php |
| 3 | ++++ b/vendor/magento/module-sitemap/Model/Observer.php |
| 4 | +@@ -5,6 +5,9 @@ |
| 5 | + */ |
| 6 | + namespace Magento\Sitemap\Model; |
| 7 | + |
| 8 | ++use Magento\Framework\App\ObjectManager; |
| 9 | ++use Psr\Log\LoggerInterface; |
| 10 | ++ |
| 11 | + /** |
| 12 | + * Sitemap module observer |
| 13 | + * |
| 14 | +@@ -66,25 +69,38 @@ class Observer |
| 15 | + */ |
| 16 | + protected $inlineTranslation; |
| 17 | + |
| 18 | ++ /** |
| 19 | ++ * @var int |
| 20 | ++ */ |
| 21 | ++ private $retryInterval = 10; |
| 22 | ++ |
| 23 | ++ /** |
| 24 | ++ * @var LoggerInterface |
| 25 | ++ */ |
| 26 | ++ private $logger; |
| 27 | ++ |
| 28 | + /** |
| 29 | + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
| 30 | + * @param \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory |
| 31 | + * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
| 32 | + * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder |
| 33 | + * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation |
| 34 | ++ * @param LoggerInterface|null $logger |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 38 | + \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory $collectionFactory, |
| 39 | + \Magento\Store\Model\StoreManagerInterface $storeManager, |
| 40 | + \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, |
| 41 | +- \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation |
| 42 | ++ \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, |
| 43 | ++ LoggerInterface $logger = null |
| 44 | + ) { |
| 45 | + $this->_scopeConfig = $scopeConfig; |
| 46 | + $this->_collectionFactory = $collectionFactory; |
| 47 | + $this->_storeManager = $storeManager; |
| 48 | + $this->_transportBuilder = $transportBuilder; |
| 49 | + $this->inlineTranslation = $inlineTranslation; |
| 50 | ++ $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | +@@ -97,6 +113,7 @@ class Observer |
| 55 | + public function scheduledGenerateSitemaps() |
| 56 | + { |
| 57 | + $errors = []; |
| 58 | ++ $sitemapsWithError = []; |
| 59 | + |
| 60 | + // check if scheduled generation enabled |
| 61 | + if (!$this->_scopeConfig->isSetFlag( |
| 62 | +@@ -114,6 +131,7 @@ class Observer |
| 63 | + try { |
| 64 | + $sitemap->generateXml(); |
| 65 | + } catch (\Exception $e) { |
| 66 | ++ $sitemapsWithError[] = $sitemap; |
| 67 | + $errors[] = $e->getMessage(); |
| 68 | + } |
| 69 | + } |
| 70 | +@@ -123,35 +141,56 @@ class Observer |
| 71 | + \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 72 | + ) |
| 73 | + ) { |
| 74 | +- $this->inlineTranslation->suspend(); |
| 75 | +- |
| 76 | +- $this->_transportBuilder->setTemplateIdentifier( |
| 77 | +- $this->_scopeConfig->getValue( |
| 78 | +- self::XML_PATH_ERROR_TEMPLATE, |
| 79 | +- \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 80 | +- ) |
| 81 | +- )->setTemplateOptions( |
| 82 | +- [ |
| 83 | +- 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, |
| 84 | +- 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, |
| 85 | +- ] |
| 86 | +- )->setTemplateVars( |
| 87 | +- ['warnings' => join("\n", $errors)] |
| 88 | +- )->setFrom( |
| 89 | +- $this->_scopeConfig->getValue( |
| 90 | +- self::XML_PATH_ERROR_IDENTITY, |
| 91 | +- \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 92 | +- ) |
| 93 | +- )->addTo( |
| 94 | +- $this->_scopeConfig->getValue( |
| 95 | +- self::XML_PATH_ERROR_RECIPIENT, |
| 96 | +- \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 97 | +- ) |
| 98 | +- ); |
| 99 | +- $transport = $this->_transportBuilder->getTransport(); |
| 100 | +- $transport->sendMessage(); |
| 101 | +- |
| 102 | +- $this->inlineTranslation->resume(); |
| 103 | ++ sleep($this->retryInterval); |
| 104 | ++ $message = 'Sitemap generation errors occurred: '; |
| 105 | ++ $message .= join(';', $errors); |
| 106 | ++ $this->logger->debug($message); |
| 107 | ++ $this->logger->debug('Sitemap generation retry attempt'); |
| 108 | ++ $nonRecoverableErrors = []; |
| 109 | ++ //re-try to write sitemap again |
| 110 | ++ foreach ($sitemapsWithError as $sitemap) { |
| 111 | ++ try { |
| 112 | ++ $sitemap->generateXml(); |
| 113 | ++ } catch (\Exception $e) { |
| 114 | ++ $nonRecoverableErrors[] = $e->getMessage(); |
| 115 | ++ } |
| 116 | ++ } |
| 117 | ++ |
| 118 | ++ if (!empty($nonRecoverableErrors)) { |
| 119 | ++ $this->inlineTranslation->suspend(); |
| 120 | ++ |
| 121 | ++ $this->_transportBuilder->setTemplateIdentifier( |
| 122 | ++ $this->_scopeConfig->getValue( |
| 123 | ++ self::XML_PATH_ERROR_TEMPLATE, |
| 124 | ++ \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 125 | ++ ) |
| 126 | ++ )->setTemplateOptions( |
| 127 | ++ [ |
| 128 | ++ 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, |
| 129 | ++ 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, |
| 130 | ++ ] |
| 131 | ++ )->setTemplateVars( |
| 132 | ++ ['warnings' => join("\n", $nonRecoverableErrors)] |
| 133 | ++ )->setFrom( |
| 134 | ++ $this->_scopeConfig->getValue( |
| 135 | ++ self::XML_PATH_ERROR_IDENTITY, |
| 136 | ++ \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 137 | ++ ) |
| 138 | ++ )->addTo( |
| 139 | ++ $this->_scopeConfig->getValue( |
| 140 | ++ self::XML_PATH_ERROR_RECIPIENT, |
| 141 | ++ \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 142 | ++ ) |
| 143 | ++ ); |
| 144 | ++ $transport = $this->_transportBuilder->getTransport(); |
| 145 | ++ $transport->sendMessage(); |
| 146 | ++ |
| 147 | ++ $this->inlineTranslation->resume(); |
| 148 | ++ |
| 149 | ++ $message = 'Sitemap generation non-recoverable errors occurred: '; |
| 150 | ++ $message .= join(';', $nonRecoverableErrors); |
| 151 | ++ $this->logger->debug($message); |
| 152 | ++ } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
0 commit comments