Skip to content

Commit 8172cb8

Browse files
committed
ACP2E-4132: [Cloud] Deactivate the old sitemap generation
1 parent 0ae5d26 commit 8172cb8

File tree

5 files changed

+156
-59
lines changed

5 files changed

+156
-59
lines changed

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

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

1414
use Magento\Framework\Exception\LocalizedException;
15-
use Magento\Sitemap\Model\Config\Source\GenerationMethod;
1615

1716
/**
1817
* Sitemap configuration
@@ -80,9 +79,6 @@ public function afterSave()
8079
$this->_config->getValue('sitemap/generate/time', $this->getScope(), $this->getScopeId()) ?: '0,0,0'
8180
);
8281
$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;
8682

8783
$cronExprArray = [
8884
(int)($time[1] ?? 0), //Minute
@@ -95,60 +91,25 @@ public function afterSave()
9591
$cronExprString = join(' ', $cronExprArray);
9692

9793
try {
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-
}
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();
116110
} catch (\Exception $e) {
117111
throw new LocalizedException(__('We can\'t save the cron expression.'));
118112
}
119113
return parent::afterSave();
120114
}
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-
}
154115
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sitemap\Plugin\Cron\Model\Config\Backend;
9+
10+
use Magento\Cron\Model\Config\Backend\Sitemap;
11+
use Magento\Cron\Model\Config\Source\Frequency;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Sitemap\Model\Config\Source\GenerationMethod;
14+
15+
/**
16+
* Plugin for Cron Sitemap backend model to ensure extended cron job configuration
17+
*/
18+
class SitemapPlugin
19+
{
20+
/**
21+
* Cron string path for sitemap schedule
22+
*/
23+
private const CRON_STRING_PATH = 'crontab/default/jobs/sitemap_generate/schedule/cron_expr';
24+
25+
/**
26+
* Cron model path
27+
*/
28+
private const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model';
29+
30+
/**
31+
* @var \Magento\Framework\App\Config\ValueFactory
32+
*/
33+
private $configValueFactory;
34+
35+
/**
36+
* @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
37+
*/
38+
public function __construct(
39+
\Magento\Framework\App\Config\ValueFactory $configValueFactory
40+
) {
41+
$this->configValueFactory = $configValueFactory;
42+
}
43+
44+
/**
45+
* Plugin to override the afterSave behavior to use unified cron job
46+
*
47+
* @param Sitemap $subject
48+
* @param mixed $result
49+
* @return mixed
50+
* @throws LocalizedException
51+
*/
52+
public function afterAfterSave(
53+
Sitemap $subject,
54+
mixed $result
55+
) {
56+
$time = $subject->getData('groups/generate/fields/time/value') ?:
57+
explode(
58+
',',
59+
$subject->getConfig()->getValue(
60+
'sitemap/generate/time',
61+
$subject->getScope(),
62+
$subject->getScopeId()
63+
) ?: '0,0,0'
64+
);
65+
$frequency = $subject->getValue();
66+
$generationMethod = $subject->getData('groups/generate/fields/generation_method/value') ?:
67+
$subject->getConfig()->getValue(
68+
'sitemap/generate/generation_method',
69+
$subject->getScope(),
70+
$subject->getScopeId()
71+
) ?: GenerationMethod::STANDARD;
72+
73+
$cronExprArray = [
74+
(int)($time[1] ?? 0), //Minute
75+
(int)($time[0] ?? 0), //Hour
76+
$frequency == Frequency::CRON_MONTHLY ? '1' : '*', //Day of the Month
77+
'*', //Month of the Year
78+
$frequency == Frequency::CRON_WEEKLY ? '1' : '*', //# Day of the Week
79+
];
80+
81+
$cronExprString = join(' ', $cronExprArray);
82+
83+
try {
84+
$this->clearCronConfiguration(self::CRON_STRING_PATH);
85+
$this->clearCronConfiguration(self::CRON_MODEL_PATH);
86+
87+
$observerModel = $generationMethod === GenerationMethod::BATCH
88+
? 'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps'
89+
: 'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps';
90+
91+
$this->setCronConfiguration(self::CRON_STRING_PATH, $cronExprString);
92+
$this->setCronConfiguration(self::CRON_MODEL_PATH, $observerModel);
93+
} catch (\Exception $e) {
94+
throw new LocalizedException(__('We can\'t save the cron expression.'));
95+
}
96+
97+
// Return the original result from the afterSave method
98+
return $result;
99+
}
100+
101+
/**
102+
* Set cron configuration value
103+
*
104+
* @param string $path
105+
* @param string $value
106+
* @return void
107+
*/
108+
private function setCronConfiguration(string $path, string $value): void
109+
{
110+
$this->configValueFactory->create()->load(
111+
$path,
112+
'path'
113+
)->setValue(
114+
$value
115+
)->setPath(
116+
$path
117+
)->save();
118+
}
119+
120+
/**
121+
* Clear cron configuration value
122+
*
123+
* @param string $path
124+
* @return void
125+
*/
126+
private function clearCronConfiguration(string $path): void
127+
{
128+
$configValue = $this->configValueFactory->create()->load($path, 'path');
129+
if ($configValue->getId()) {
130+
$configValue->delete();
131+
}
132+
}
133+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2012 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,7 @@
7979
<argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
8080
</arguments>
8181
</type>
82+
<type name="Magento\Cron\Model\Config\Backend\Sitemap">
83+
<plugin name="sitemap_extended_cron_config" type="Magento\Sitemap\Plugin\Cron\Model\Config\Backend\SitemapPlugin" />
84+
</type>
8285
</config>

0 commit comments

Comments
 (0)