Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinit fresh config before flushing cache and immediately save config… #3533

Merged
merged 9 commits into from
Oct 3, 2023
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct()
parent::__construct();
$this->_removeButton('add');
$this->_addButton('flush_magento', [
'label' => Mage::helper('core')->__('Flush OpenMage Cache'),
'label' => Mage::helper('core')->__('Flush & Apply Updates'),
fballiano marked this conversation as resolved.
Show resolved Hide resolved
'onclick' => Mage::helper('core/js')->getSetLocationJs($this->getFlushSystemUrl()),
'class' => 'delete',
]);
Expand Down
17 changes: 14 additions & 3 deletions app/code/core/Mage/Adminhtml/controllers/CacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function indexAction()
*/
public function flushAllAction()
{
Mage::dispatchEvent('adminhtml_cache_flush_all');
Mage::app()->getCacheInstance()->flush();
Mage::dispatchEvent('adminhtml_cache_flush_all');
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The cache storage has been flushed."));
$this->_redirect('*/*');
}
Expand All @@ -63,9 +63,20 @@ public function flushAllAction()
*/
public function flushSystemAction()
{
Mage::app()->cleanCache();
Mage::app()->getCacheInstance()->banUse('config');
Mage::getConfig()->reinit();
Mage::getConfig()->getCacheSaveLock(30, true);
try {
Mage::app()->cleanCache();
Mage_Core_Model_Resource_Setup::applyAllUpdates();
Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
Mage::app()->getCacheInstance()->unbanUse('config');
Mage::getConfig()->saveCache();
} finally {
Mage::getConfig()->releaseCacheSaveLock();
}
Mage::dispatchEvent('adminhtml_cache_flush_system');
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The OpenMage cache storage has been flushed."));
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The OpenMage cache has been flushed and updates applied."));
$this->_redirect('*/*');
colinmollenhour marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
21 changes: 14 additions & 7 deletions app/code/core/Mage/Core/Model/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,21 @@ protected function _initCache(array $cacheInitOptions = [])
protected function _initModules()
{
if (!$this->_config->loadModulesCache()) {
$this->_config->loadModules();
if ($this->_config->isLocalConfigLoaded() && !$this->_shouldSkipProcessModulesUpdates()) {
Varien_Profiler::start('mage::app::init::apply_db_schema_updates');
Mage_Core_Model_Resource_Setup::applyAllUpdates();
Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
try {
$this->_config->getCacheSaveLock();
if (!$this->_config->loadModulesCache()) {
$this->_config->loadModules();
if ($this->_config->isLocalConfigLoaded() && !$this->_shouldSkipProcessModulesUpdates()) {
Varien_Profiler::start('mage::app::init::apply_db_schema_updates');
Mage_Core_Model_Resource_Setup::applyAllUpdates();
Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
}
$this->_config->loadDb();
$this->_config->saveCache();
}
} finally {
$this->_config->releaseCacheSaveLock();
}
$this->_config->loadDb();
$this->_config->saveCache();
}
return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions app/code/core/Mage/Core/Model/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,18 @@ public function banUse($typeCode)
return $this;
}

/**
* Enable cache usage for specific data type
*
* @param string $typeCode
* @return $this
*/
public function unbanUse($typeCode)
{
$this->_allowedCacheOptions[$typeCode] = true;
return $this;
}

/**
* Get cache tags by cache type from configuration
*
Expand Down
103 changes: 59 additions & 44 deletions app/code/core/Mage/Core/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,19 @@ public function init($options = [])
$this->setOptions($options);
$this->loadBase();

$cacheLoad = $this->loadModulesCache();
if ($cacheLoad) {
return $this;
if (!$this->loadModulesCache()) {
try {
$this->getCacheSaveLock();
if (!$this->loadModulesCache()) {
$this->_useCache = false;
$this->loadModules();
$this->loadDb();
$this->saveCache();
}
} finally {
$this->releaseCacheSaveLock();
}
}

$this->_useCache = false;

$this->loadModules();
$this->loadDb();
$this->saveCache();
return $this;
}

Expand Down Expand Up @@ -354,15 +357,13 @@ public function loadBase()
*/
public function loadModulesCache()
{
if (Mage::isInstalled(['etc_dir' => $this->getOptions()->getEtcDir()])) {
if ($this->_canUseCacheForInit()) {
Varien_Profiler::start('mage::app::init::config::load_cache');
$loaded = $this->loadCache();
Varien_Profiler::stop('mage::app::init::config::load_cache');
if ($loaded) {
$this->_useCache = true;
return true;
}
if ($this->_canUseCacheForInit()) {
Varien_Profiler::start('mage::app::init::config::load_cache');
$loaded = $this->loadCache();
Varien_Profiler::stop('mage::app::init::config::load_cache');
if ($loaded) {
$this->_useCache = true;
return true;
}
}
return false;
Expand Down Expand Up @@ -474,20 +475,9 @@ protected function _canUseLocalModules()
*/
protected function _canUseCacheForInit()
{
if (Mage::app()->useCache('config') && $this->_allowCacheForInit) {
$retries = 10;
do {
if ($this->_loadCache($this->_getCacheLockId())) {
if ($retries) {
usleep(500000); // 0.5 seconds
}
} else {
return true;
}
} while ($retries--);
}

return false;
return $this->_allowCacheForInit
&& Mage::isInstalled(['etc_dir' => $this->getOptions()->getEtcDir()])
&& Mage::app()->useCache('config');
fballiano marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -501,13 +491,46 @@ public function getCache()
}

/**
* Get lock flag cache identifier
* Call before building and saving cache to ensure only one process can save the cache
*
* @return string
* If failed to get cache lock:
* - CLI: throws exception
* - Other: 503 error
*
* @return void
* @throws Exception
*/
protected function _getCacheLockId()
public function getCacheSaveLock($waitTime = null, $ignoreFailure = false)
{
return $this->getCacheId() . '.lock';
if (! Mage::app()->useCache('config')) {
return;
}
$waitTime = $waitTime ?: (PHP_SAPI === 'cli' ? 60 : 3);
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
if (!$connection->fetchOne("SELECT GET_LOCK('core_config_cache_save_lock', ?)", [$waitTime])) {
if ($ignoreFailure) {
return;
} elseif (PHP_SAPI === 'cli') {
throw new Exception('Could not get lock on cache save operation.');
} else {
require_once Mage::getBaseDir() . DS . 'errors' . DS . '503.php';
die();
}
}
}

/**
* Release the cache saving lock after it is saved or no longer needed
*
* @return void
*/
public function releaseCacheSaveLock()
{
if (! Mage::app()->useCache('config')) {
return;
}
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$connection->fetchOne("SELECT RELEASE_LOCK('core_config_cache_save_lock')");
}

/**
Expand All @@ -524,12 +547,6 @@ public function saveCache($tags = [])
if (!in_array(self::CACHE_TAG, $tags)) {
$tags[] = self::CACHE_TAG;
}
$cacheLockId = $this->_getCacheLockId();
if ($this->_loadCache($cacheLockId)) {
return $this;
}

$this->_saveCache(time(), $cacheLockId, [], 60);

if (!empty($this->_cacheSections)) {
$xml = clone $this->_xml;
Expand All @@ -540,15 +557,13 @@ public function saveCache($tags = [])
$this->_cachePartsForSave[$this->getCacheId()] = $xml->asNiceXml('', false);
} else {
parent::saveCache($tags);
$this->_removeCache($cacheLockId);
return $this;
}

foreach ($this->_cachePartsForSave as $cacheId => $cacheData) {
$this->_saveCache($cacheData, $cacheId, $tags, $this->getCacheLifetime());
}
unset($this->_cachePartsForSave);
$this->_removeCache($cacheLockId);

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion app/locale/en_US/Mage_Adminhtml.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@
"The Layered Navigation indexing queue has been canceled.","The Layered Navigation indexing queue has been canceled."
"The Layered Navigation indices were refreshed.","The Layered Navigation indices were refreshed."
"The Layered Navigation process has been queued to be killed.","The Layered Navigation process has been queued to be killed."
"The OpenMage cache storage has been flushed.","The OpenMage cache storage has been flushed."
"The OpenMage cache has been flushed and updates applied.":"The OpenMage cache has been flushed and updates applied."
"The Special Price is active only when lower than the Actual Price.","The Special Price is active only when lower than the Actual Price."
"The URL Rewrite has been deleted.","The URL Rewrite has been deleted."
"The URL Rewrite has been saved.","The URL Rewrite has been saved."
Expand Down
2 changes: 1 addition & 1 deletion app/locale/en_US/Mage_Core.csv
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"File with an extension ""%value%"" is protected and cannot be uploaded","File with an extension ""%value%"" is protected and cannot be uploaded"
"First Day of Week","First Day of Week"
"Flush Cache Storage","Flush Cache Storage"
"Flush OpenMage Cache","Flush OpenMage Cache"
"Flush & Apply Updates","Flush & Apply Updates"
"Footer","Footer"
"Forgot Password Email Sender","Forgot Password Email Sender"
"Forgot Password Email Template","Forgot Password Email Template"
Expand Down
Loading