Skip to content

Commit 5a67062

Browse files
committed
ACP2E-4132: [Cloud] Deactivate the old sitemap generation
1 parent 1c84f2f commit 5a67062

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

app/code/Magento/Cron/Model/Config/Backend/Sitemap.php

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Magento\Cron\Model\Config\Backend;
1313

1414
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Sitemap\Model\Config\Source\GenerationMethod;
1516

1617
/**
1718
* Sitemap configuration
@@ -79,6 +80,9 @@ public function afterSave()
7980
$this->_config->getValue('sitemap/generate/time', $this->getScope(), $this->getScopeId()) ?: '0,0,0'
8081
);
8182
$frequency = $this->getValue();
83+
$generationMethod = $this->getData('groups/generate/fields/generation_method/value') ?:
84+
$this->_config->getValue('sitemap/generate/generation_method', $this->getScope(), $this->getScopeId())
85+
?: GenerationMethod::STANDARD;
8286

8387
$cronExprArray = [
8488
(int)($time[1] ?? 0), //Minute
@@ -91,25 +95,60 @@ public function afterSave()
9195
$cronExprString = join(' ', $cronExprArray);
9296

9397
try {
94-
$this->_configValueFactory->create()->load(
95-
self::CRON_STRING_PATH,
96-
'path'
97-
)->setValue(
98-
$cronExprString
99-
)->setPath(
100-
self::CRON_STRING_PATH
101-
)->save();
102-
$this->_configValueFactory->create()->load(
103-
self::CRON_MODEL_PATH,
104-
'path'
105-
)->setValue(
106-
$this->_runModelPath
107-
)->setPath(
108-
self::CRON_MODEL_PATH
109-
)->save();
98+
// Clear old cron configurations first to prevent conflicts
99+
$this->clearCronConfiguration(self::CRON_STRING_PATH);
100+
$this->clearCronConfiguration(self::CRON_MODEL_PATH);
101+
102+
// Configure the selected generation method with correct model class
103+
if ($generationMethod === GenerationMethod::BATCH) {
104+
$this->setCronConfiguration(self::CRON_STRING_PATH, $cronExprString);
105+
$this->setCronConfiguration(
106+
self::CRON_MODEL_PATH,
107+
'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps'
108+
);
109+
} else {
110+
$this->setCronConfiguration(self::CRON_STRING_PATH, $cronExprString);
111+
$this->setCronConfiguration(
112+
self::CRON_MODEL_PATH,
113+
'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps'
114+
);
115+
}
110116
} catch (\Exception $e) {
111117
throw new LocalizedException(__('We can\'t save the cron expression.'));
112118
}
113119
return parent::afterSave();
114120
}
121+
122+
/**
123+
* Set cron configuration value
124+
*
125+
* @param string $path
126+
* @param string $value
127+
* @return void
128+
*/
129+
private function setCronConfiguration(string $path, string $value): void
130+
{
131+
$this->_configValueFactory->create()->load(
132+
$path,
133+
'path'
134+
)->setValue(
135+
$value
136+
)->setPath(
137+
$path
138+
)->save();
139+
}
140+
141+
/**
142+
* Clear cron configuration value
143+
*
144+
* @param string $path
145+
* @return void
146+
*/
147+
private function clearCronConfiguration(string $path): void
148+
{
149+
$configValue = $this->_configValueFactory->create()->load($path, 'path');
150+
if ($configValue->getId()) {
151+
$configValue->delete();
152+
}
153+
}
115154
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Sitemap\Model\Batch;
99

1010
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
11+
use Magento\Framework\App\Filesystem\DirectoryList;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\App\RequestInterface;
1314
use Magento\Framework\Data\Collection\AbstractDb;
@@ -141,6 +142,7 @@ public function __construct(
141142
ObjectManager::getInstance()->get(ProductConfigReader::class);
142143
$this->sitemapItemFactory = $sitemapItemFactory ??
143144
ObjectManager::getInstance()->get(SitemapItemInterfaceFactory::class);
145+
$this->tmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
144146

145147
parent::__construct(
146148
$context,
@@ -310,4 +312,18 @@ private function processSitemapItem($item): void
310312
$this->_lineCount++;
311313
$this->_fileSize += strlen($xml);
312314
}
315+
316+
/**
317+
* Get path to sitemap file
318+
*
319+
* @param string $fileName
320+
* @return string
321+
*/
322+
private function getFilePath(string $fileName): string
323+
{
324+
$path = $this->getSitemapPath() !== null ? rtrim($this->getSitemapPath(), '/') : '';
325+
$path .= '/' . $fileName;
326+
327+
return $path;
328+
}
313329
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sitemap\Model\Config\Source;
9+
10+
use Magento\Framework\Data\OptionSourceInterface;
11+
12+
/**
13+
* Source model for sitemap generation method configuration
14+
*/
15+
class GenerationMethod implements OptionSourceInterface
16+
{
17+
/**
18+
* Standard generation method constant
19+
*/
20+
public const STANDARD = 'standard';
21+
22+
/**
23+
* Batch generation method constant
24+
*/
25+
public const BATCH = 'batch';
26+
27+
/**
28+
* Return array of options as value-label pairs
29+
*
30+
* @return array
31+
*/
32+
public function toOptionArray(): array
33+
{
34+
return [
35+
['value' => self::STANDARD, 'label' => __('Standard')],
36+
['value' => self::BATCH, 'label' => __('Batch (Memory Optimized)')],
37+
];
38+
}
39+
40+
/**
41+
* Get options in "key-value" format
42+
*
43+
* @return array
44+
*/
45+
public function toArray(): array
46+
{
47+
return [
48+
self::STANDARD => __('Standard'),
49+
self::BATCH => __('Batch (Memory Optimized)'),
50+
];
51+
}
52+
}

app/code/Magento/Sitemap/etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@
6969
<label>Enabled</label>
7070
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
7171
</field>
72+
<field id="generation_method" translate="label comment" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
73+
<label>Generation Method</label>
74+
<comment>Standard method processes all data in memory. Batch method uses memory-optimized processing for large catalogs.</comment>
75+
<source_model>Magento\Sitemap\Model\Config\Source\GenerationMethod</source_model>
76+
<depends>
77+
<field id="enabled">1</field>
78+
</depends>
79+
</field>
7280
<field id="error_email" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
7381
<label>Error Email Recipient</label>
7482
<validate>validate-email</validate>

app/code/Magento/Sitemap/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
</store>
3232
<generate>
3333
<enabled>0</enabled>
34+
<generation_method>standard</generation_method>
3435
<error_email />
3536
<error_email_template>sitemap_generate_error_email_template</error_email_template>
3637
<error_email_identity>general</error_email_identity>

app/code/Magento/Sitemap/etc/crontab.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
99
<group id="default">
1010
<job name="sitemap_generate" instance="Magento\Sitemap\Model\Observer" method="scheduledGenerateSitemaps" />
11-
<job name="sitemap_generate_batch" instance="Magento\Sitemap\Model\Batch\Observer" method="scheduledGenerateSitemaps" />
1211
</group>
1312
</config>

0 commit comments

Comments
 (0)