Commit 87c42e261ac0c9146b9a1fb6f26048b30a8035ef
1 parent
1123e921
KTS-3764
"Items in admin section registered more than once." Fixed. Added a lock around the plugin registration (reread) to prevent a race condition. Committed by: Megan Watson Reviewed by: Conrad Vermeulen git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@9474 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
58 additions
and
1 deletions
lib/plugins/pluginutil.inc.php
| @@ -475,6 +475,64 @@ class KTPluginUtil { | @@ -475,6 +475,64 @@ class KTPluginUtil { | ||
| 475 | * This is called by the 'Re-read plugins' action in the web interface. | 475 | * This is called by the 'Re-read plugins' action in the web interface. |
| 476 | */ | 476 | */ |
| 477 | function registerPlugins () { | 477 | function registerPlugins () { |
| 478 | + global $default; | ||
| 479 | + | ||
| 480 | + // Path to lock file | ||
| 481 | + $cacheDir = $default->cacheDirectory . DIRECTORY_SEPARATOR; | ||
| 482 | + $lockFile = $cacheDir.'plugin_register.lock'; | ||
| 483 | + | ||
| 484 | + // Check if the lock file exists | ||
| 485 | + if(KTPluginUtil::doCheck($lockFile)){ | ||
| 486 | + return true; | ||
| 487 | + } | ||
| 488 | + | ||
| 489 | + // Create the lock file, run through the plugin registration and then delete the lock file | ||
| 490 | + touch($lockFile); | ||
| 491 | + KTPluginUtil::doPluginRegistration(); | ||
| 492 | + @unlink($lockFile); | ||
| 493 | + } | ||
| 494 | + | ||
| 495 | + /** | ||
| 496 | + * Check the lockfile | ||
| 497 | + */ | ||
| 498 | + function doCheck($lockFile) | ||
| 499 | + { | ||
| 500 | + if(file_exists($lockFile)){ | ||
| 501 | + // If it does exist, do a stat on it to check when it was created. | ||
| 502 | + // if it was accessed more than 5 minutes ago then delete it and proceed with the plugin registration | ||
| 503 | + // otherwise wait till lock file is deleted signalling that the registration is complete and return. | ||
| 504 | + | ||
| 505 | + $stat = stat($lockFile); | ||
| 506 | + | ||
| 507 | + $time = time() - (60 * 5); | ||
| 508 | + if($stat['mtime'] > $time){ | ||
| 509 | + | ||
| 510 | + $cnt = 0; | ||
| 511 | + | ||
| 512 | + while(file_exists($lockFile)){ | ||
| 513 | + $cnt++; | ||
| 514 | + sleep(2); | ||
| 515 | + | ||
| 516 | + // if we've been waiting too long - typically it should only take a few seconds so 2 mins is too much time. | ||
| 517 | + if($cnt > 60){ | ||
| 518 | + @unlink($lockFile); | ||
| 519 | + return false; | ||
| 520 | + } | ||
| 521 | + } | ||
| 522 | + return true; | ||
| 523 | + } | ||
| 524 | + @unlink($lockFile); | ||
| 525 | + } | ||
| 526 | + return false; | ||
| 527 | + } | ||
| 528 | + | ||
| 529 | + /** | ||
| 530 | + * Read the plugins directory and register all plugins in the database. | ||
| 531 | + */ | ||
| 532 | + function doPluginRegistration() | ||
| 533 | + { | ||
| 534 | + global $default; | ||
| 535 | + | ||
| 478 | KTPluginUtil::_deleteSmartyFiles(); | 536 | KTPluginUtil::_deleteSmartyFiles(); |
| 479 | require_once(KT_LIB_DIR . '/cache/cache.inc.php'); | 537 | require_once(KT_LIB_DIR . '/cache/cache.inc.php'); |
| 480 | $oCache =& KTCache::getSingleton(); | 538 | $oCache =& KTCache::getSingleton(); |
| @@ -493,7 +551,6 @@ class KTPluginUtil { | @@ -493,7 +551,6 @@ class KTPluginUtil { | ||
| 493 | } | 551 | } |
| 494 | } | 552 | } |
| 495 | 553 | ||
| 496 | - global $default; | ||
| 497 | $oRegistry =& KTPluginRegistry::getSingleton(); | 554 | $oRegistry =& KTPluginRegistry::getSingleton(); |
| 498 | $aRegistryList = $oRegistry->getPlugins(); | 555 | $aRegistryList = $oRegistry->getPlugins(); |
| 499 | foreach ($aRegistryList as $oPlugin) { | 556 | foreach ($aRegistryList as $oPlugin) { |