Skip to content

Commit fc15c93

Browse files
colinmollenhourfballianokiatng
authored
Reinit fresh config before flushing cache and immediately save config… (#3533)
Co-authored-by: Fabrizio Balliano <[email protected]> Co-authored-by: Ng Kiat Siong <[email protected]>
1 parent d0f919b commit fc15c93

File tree

7 files changed

+102
-57
lines changed

7 files changed

+102
-57
lines changed

app/code/core/Mage/Adminhtml/Block/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct()
3030
parent::__construct();
3131
$this->_removeButton('add');
3232
$this->_addButton('flush_magento', [
33-
'label' => Mage::helper('core')->__('Flush OpenMage Cache'),
33+
'label' => Mage::helper('core')->__('Flush & Apply Updates'),
3434
'onclick' => Mage::helper('core/js')->getSetLocationJs($this->getFlushSystemUrl()),
3535
'class' => 'delete',
3636
]);

app/code/core/Mage/Adminhtml/controllers/CacheController.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function indexAction()
5252
*/
5353
public function flushAllAction()
5454
{
55-
Mage::dispatchEvent('adminhtml_cache_flush_all');
5655
Mage::app()->getCacheInstance()->flush();
56+
Mage::dispatchEvent('adminhtml_cache_flush_all');
5757
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The cache storage has been flushed."));
5858
$this->_redirect('*/*');
5959
}
@@ -63,9 +63,20 @@ public function flushAllAction()
6363
*/
6464
public function flushSystemAction()
6565
{
66-
Mage::app()->cleanCache();
66+
Mage::app()->getCacheInstance()->banUse('config');
67+
Mage::getConfig()->reinit();
68+
Mage::getConfig()->getCacheSaveLock(30, true);
69+
try {
70+
Mage::app()->cleanCache();
71+
Mage_Core_Model_Resource_Setup::applyAllUpdates();
72+
Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
73+
Mage::app()->getCacheInstance()->unbanUse('config');
74+
Mage::getConfig()->saveCache();
75+
} finally {
76+
Mage::getConfig()->releaseCacheSaveLock();
77+
}
6778
Mage::dispatchEvent('adminhtml_cache_flush_system');
68-
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The OpenMage cache storage has been flushed."));
79+
$this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The OpenMage cache has been flushed and updates applied."));
6980
$this->_redirect('*/*');
7081
}
7182

app/code/core/Mage/Core/Model/App.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,21 @@ protected function _initCache(array $cacheInitOptions = [])
432432
protected function _initModules()
433433
{
434434
if (!$this->_config->loadModulesCache()) {
435-
$this->_config->loadModules();
436-
if ($this->_config->isLocalConfigLoaded() && !$this->_shouldSkipProcessModulesUpdates()) {
437-
Varien_Profiler::start('mage::app::init::apply_db_schema_updates');
438-
Mage_Core_Model_Resource_Setup::applyAllUpdates();
439-
Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
435+
try {
436+
$this->_config->getCacheSaveLock();
437+
if (!$this->_config->loadModulesCache()) {
438+
$this->_config->loadModules();
439+
if ($this->_config->isLocalConfigLoaded() && !$this->_shouldSkipProcessModulesUpdates()) {
440+
Varien_Profiler::start('mage::app::init::apply_db_schema_updates');
441+
Mage_Core_Model_Resource_Setup::applyAllUpdates();
442+
Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
443+
}
444+
$this->_config->loadDb();
445+
$this->_config->saveCache();
446+
}
447+
} finally {
448+
$this->_config->releaseCacheSaveLock();
440449
}
441-
$this->_config->loadDb();
442-
$this->_config->saveCache();
443450
}
444451
return $this;
445452
}

app/code/core/Mage/Core/Model/Cache.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,18 @@ public function banUse($typeCode)
532532
return $this;
533533
}
534534

535+
/**
536+
* Enable cache usage for specific data type
537+
*
538+
* @param string $typeCode
539+
* @return $this
540+
*/
541+
public function unbanUse($typeCode)
542+
{
543+
$this->_allowedCacheOptions[$typeCode] = true;
544+
return $this;
545+
}
546+
535547
/**
536548
* Get cache tags by cache type from configuration
537549
*

app/code/core/Mage/Core/Model/Config.php

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,19 @@ public function init($options = [])
313313
$this->setOptions($options);
314314
$this->loadBase();
315315

316-
$cacheLoad = $this->loadModulesCache();
317-
if ($cacheLoad) {
318-
return $this;
316+
if (!$this->loadModulesCache()) {
317+
try {
318+
$this->getCacheSaveLock();
319+
if (!$this->loadModulesCache()) {
320+
$this->_useCache = false;
321+
$this->loadModules();
322+
$this->loadDb();
323+
$this->saveCache();
324+
}
325+
} finally {
326+
$this->releaseCacheSaveLock();
327+
}
319328
}
320-
321-
$this->_useCache = false;
322-
323-
$this->loadModules();
324-
$this->loadDb();
325-
$this->saveCache();
326329
return $this;
327330
}
328331

@@ -354,15 +357,13 @@ public function loadBase()
354357
*/
355358
public function loadModulesCache()
356359
{
357-
if (Mage::isInstalled(['etc_dir' => $this->getOptions()->getEtcDir()])) {
358-
if ($this->_canUseCacheForInit()) {
359-
Varien_Profiler::start('mage::app::init::config::load_cache');
360-
$loaded = $this->loadCache();
361-
Varien_Profiler::stop('mage::app::init::config::load_cache');
362-
if ($loaded) {
363-
$this->_useCache = true;
364-
return true;
365-
}
360+
if ($this->_canUseCacheForInit()) {
361+
Varien_Profiler::start('mage::app::init::config::load_cache');
362+
$loaded = $this->loadCache();
363+
Varien_Profiler::stop('mage::app::init::config::load_cache');
364+
if ($loaded) {
365+
$this->_useCache = true;
366+
return true;
366367
}
367368
}
368369
return false;
@@ -474,20 +475,9 @@ protected function _canUseLocalModules()
474475
*/
475476
protected function _canUseCacheForInit()
476477
{
477-
if (Mage::app()->useCache('config') && $this->_allowCacheForInit) {
478-
$retries = 10;
479-
do {
480-
if ($this->_loadCache($this->_getCacheLockId())) {
481-
if ($retries) {
482-
usleep(500000); // 0.5 seconds
483-
}
484-
} else {
485-
return true;
486-
}
487-
} while ($retries--);
488-
}
489-
490-
return false;
478+
return $this->_allowCacheForInit
479+
&& Mage::isInstalled(['etc_dir' => $this->getOptions()->getEtcDir()])
480+
&& Mage::app()->useCache('config');
491481
}
492482

493483
/**
@@ -501,13 +491,46 @@ public function getCache()
501491
}
502492

503493
/**
504-
* Get lock flag cache identifier
494+
* Call before building and saving cache to ensure only one process can save the cache
505495
*
506-
* @return string
496+
* If failed to get cache lock:
497+
* - CLI: throws exception
498+
* - Other: 503 error
499+
*
500+
* @return void
501+
* @throws Exception
507502
*/
508-
protected function _getCacheLockId()
503+
public function getCacheSaveLock($waitTime = null, $ignoreFailure = false)
509504
{
510-
return $this->getCacheId() . '.lock';
505+
if (! Mage::app()->useCache('config')) {
506+
return;
507+
}
508+
$waitTime = $waitTime ?: (PHP_SAPI === 'cli' ? 60 : 3);
509+
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
510+
if (!$connection->fetchOne("SELECT GET_LOCK('core_config_cache_save_lock', ?)", [$waitTime])) {
511+
if ($ignoreFailure) {
512+
return;
513+
} elseif (PHP_SAPI === 'cli') {
514+
throw new Exception('Could not get lock on cache save operation.');
515+
} else {
516+
require_once Mage::getBaseDir() . DS . 'errors' . DS . '503.php';
517+
die();
518+
}
519+
}
520+
}
521+
522+
/**
523+
* Release the cache saving lock after it is saved or no longer needed
524+
*
525+
* @return void
526+
*/
527+
public function releaseCacheSaveLock()
528+
{
529+
if (! Mage::app()->useCache('config')) {
530+
return;
531+
}
532+
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
533+
$connection->fetchOne("SELECT RELEASE_LOCK('core_config_cache_save_lock')");
511534
}
512535

513536
/**
@@ -524,12 +547,6 @@ public function saveCache($tags = [])
524547
if (!in_array(self::CACHE_TAG, $tags)) {
525548
$tags[] = self::CACHE_TAG;
526549
}
527-
$cacheLockId = $this->_getCacheLockId();
528-
if ($this->_loadCache($cacheLockId)) {
529-
return $this;
530-
}
531-
532-
$this->_saveCache(time(), $cacheLockId, [], 60);
533550

534551
if (!empty($this->_cacheSections)) {
535552
$xml = clone $this->_xml;
@@ -540,15 +557,13 @@ public function saveCache($tags = [])
540557
$this->_cachePartsForSave[$this->getCacheId()] = $xml->asNiceXml('', false);
541558
} else {
542559
parent::saveCache($tags);
543-
$this->_removeCache($cacheLockId);
544560
return $this;
545561
}
546562

547563
foreach ($this->_cachePartsForSave as $cacheId => $cacheData) {
548564
$this->_saveCache($cacheData, $cacheId, $tags, $this->getCacheLifetime());
549565
}
550566
unset($this->_cachePartsForSave);
551-
$this->_removeCache($cacheLockId);
552567

553568
return $this;
554569
}

app/locale/en_US/Mage_Adminhtml.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@
10311031
"The Layered Navigation indexing queue has been canceled.","The Layered Navigation indexing queue has been canceled."
10321032
"The Layered Navigation indices were refreshed.","The Layered Navigation indices were refreshed."
10331033
"The Layered Navigation process has been queued to be killed.","The Layered Navigation process has been queued to be killed."
1034-
"The OpenMage cache storage has been flushed.","The OpenMage cache storage has been flushed."
1034+
"The OpenMage cache has been flushed and updates applied.":"The OpenMage cache has been flushed and updates applied."
10351035
"The Special Price is active only when lower than the Actual Price.","The Special Price is active only when lower than the Actual Price."
10361036
"The URL Rewrite has been deleted.","The URL Rewrite has been deleted."
10371037
"The URL Rewrite has been saved.","The URL Rewrite has been saved."

app/locale/en_US/Mage_Core.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"File with an extension ""%value%"" is protected and cannot be uploaded","File with an extension ""%value%"" is protected and cannot be uploaded"
148148
"First Day of Week","First Day of Week"
149149
"Flush Cache Storage","Flush Cache Storage"
150-
"Flush OpenMage Cache","Flush OpenMage Cache"
150+
"Flush & Apply Updates","Flush & Apply Updates"
151151
"Footer","Footer"
152152
"Forgot Password Email Sender","Forgot Password Email Sender"
153153
"Forgot Password Email Template","Forgot Password Email Template"

0 commit comments

Comments
 (0)