From 87c42e261ac0c9146b9a1fb6f26048b30a8035ef Mon Sep 17 00:00:00 2001 From: Megan Watson Date: Fri, 3 Oct 2008 12:42:49 +0000 Subject: [PATCH] KTS-3764 "Items in admin section registered more than once." Fixed. Added a lock around the plugin registration (reread) to prevent a race condition. --- lib/plugins/pluginutil.inc.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/plugins/pluginutil.inc.php b/lib/plugins/pluginutil.inc.php index 477e7a4..40b95a6 100644 --- a/lib/plugins/pluginutil.inc.php +++ b/lib/plugins/pluginutil.inc.php @@ -475,6 +475,64 @@ class KTPluginUtil { * This is called by the 'Re-read plugins' action in the web interface. */ function registerPlugins () { + global $default; + + // Path to lock file + $cacheDir = $default->cacheDirectory . DIRECTORY_SEPARATOR; + $lockFile = $cacheDir.'plugin_register.lock'; + + // Check if the lock file exists + if(KTPluginUtil::doCheck($lockFile)){ + return true; + } + + // Create the lock file, run through the plugin registration and then delete the lock file + touch($lockFile); + KTPluginUtil::doPluginRegistration(); + @unlink($lockFile); + } + + /** + * Check the lockfile + */ + function doCheck($lockFile) + { + if(file_exists($lockFile)){ + // If it does exist, do a stat on it to check when it was created. + // if it was accessed more than 5 minutes ago then delete it and proceed with the plugin registration + // otherwise wait till lock file is deleted signalling that the registration is complete and return. + + $stat = stat($lockFile); + + $time = time() - (60 * 5); + if($stat['mtime'] > $time){ + + $cnt = 0; + + while(file_exists($lockFile)){ + $cnt++; + sleep(2); + + // if we've been waiting too long - typically it should only take a few seconds so 2 mins is too much time. + if($cnt > 60){ + @unlink($lockFile); + return false; + } + } + return true; + } + @unlink($lockFile); + } + return false; + } + + /** + * Read the plugins directory and register all plugins in the database. + */ + function doPluginRegistration() + { + global $default; + KTPluginUtil::_deleteSmartyFiles(); require_once(KT_LIB_DIR . '/cache/cache.inc.php'); $oCache =& KTCache::getSingleton(); @@ -493,7 +551,6 @@ class KTPluginUtil { } } - global $default; $oRegistry =& KTPluginRegistry::getSingleton(); $aRegistryList = $oRegistry->getPlugins(); foreach ($aRegistryList as $oPlugin) { -- libgit2 0.21.4