mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 12:40:01 +01:00
MDL-25500 lock: post integration trivial cleanup
* A couple of misc comment typo's * Added missing $CFG required global in file_lock_factory::is_available * Removed unused $CFG global from file_lock_factory::get_lock * A couple of trivial phpdoc tweaks * Removed unused $DB global from postgres_lock_factory::get_index_from_key * Added global namespace \ to exceptions in get_index_from_key (tested and found they didn't resolve)
This commit is contained in:
parent
453d66b782
commit
34027f1563
@ -41,7 +41,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class db_record_lock_factory implements lock_factory {
|
||||
|
||||
/** @var moodle_database $db Hold a reference to the global $DB */
|
||||
/** @var \moodle_database $db Hold a reference to the global $DB */
|
||||
protected $db;
|
||||
|
||||
/** @var string $type Used to prefix lock keys */
|
||||
@ -99,7 +99,7 @@ class db_record_lock_factory implements lock_factory {
|
||||
|
||||
/**
|
||||
* This function generates a unique token for the lock to use.
|
||||
* It is important that this token is not soley based on time as this could lead
|
||||
* It is important that this token is not solely based on time as this could lead
|
||||
* to duplicates in a clustered environment (especially on VMs due to poor time precision).
|
||||
*/
|
||||
protected function generate_unique_token() {
|
||||
|
@ -97,6 +97,7 @@ class file_lock_factory implements lock_factory {
|
||||
* @return boolean - True if preventfilelocking is not set - or the file_lock_root is not in dataroot.
|
||||
*/
|
||||
public function is_available() {
|
||||
global $CFG;
|
||||
$preventfilelocking = !empty($CFG->preventfilelocking);
|
||||
$lockdirisdataroot = true;
|
||||
if (!empty($CFG->file_lock_root) && strpos($CFG->file_lock_root, $CFG->dataroot) !== 0) {
|
||||
@ -129,8 +130,6 @@ class file_lock_factory implements lock_factory {
|
||||
* @return boolean - true if a lock was obtained.
|
||||
*/
|
||||
public function get_lock($resource, $timeout, $maxlifetime = 86400) {
|
||||
global $CFG;
|
||||
|
||||
$giveuptime = time() + $timeout;
|
||||
|
||||
$hash = md5($this->type . '_' . $resource);
|
||||
|
@ -40,7 +40,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class lock {
|
||||
|
||||
/** @var string|int $key A uniq key representing a held lock */
|
||||
/** @var string|int $key A unique key representing a held lock */
|
||||
protected $key = '';
|
||||
|
||||
/** @var lock_factory $factory The factory that generated this lock */
|
||||
@ -51,7 +51,8 @@ class lock {
|
||||
|
||||
/**
|
||||
* Construct a lock containing the unique key required to release it.
|
||||
* @param string $key - The lock key.
|
||||
* @param mixed $key - The lock key. The type of this is up to the lock_factory being used.
|
||||
* For file locks this is a file handle. For MySQL this is a string.
|
||||
* @param lock_factory $factory - The factory that generated this lock.
|
||||
*/
|
||||
public function __construct($key, $factory) {
|
||||
|
@ -42,6 +42,7 @@ class lock_config {
|
||||
*
|
||||
* @param string $type - Unique namespace for the locks generated by this factory. e.g. core_cron
|
||||
* @return \core\lock\lock_factory
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_lock_factory($type) {
|
||||
global $CFG, $DB;
|
||||
@ -58,11 +59,12 @@ class lock_config {
|
||||
} else {
|
||||
$dbtype = clean_param($DB->get_dbfamily(), PARAM_ALPHA);
|
||||
|
||||
// DB Specific lock factory is prefered - should support auto-release.
|
||||
// DB Specific lock factory is preferred - should support auto-release.
|
||||
$lockfactoryclass = "\\core\\lock\\${dbtype}_lock_factory";
|
||||
if (!class_exists($lockfactoryclass)) {
|
||||
$lockfactoryclass = '\core\lock\file_lock_factory';
|
||||
}
|
||||
/* @var lock_factory $lockfactory */
|
||||
$lockfactory = new $lockfactoryclass($type);
|
||||
if (!$lockfactory->is_available()) {
|
||||
// Final fallback - DB row locking.
|
||||
|
@ -61,7 +61,7 @@ interface lock_factory {
|
||||
public function supports_auto_release();
|
||||
|
||||
/**
|
||||
* supports_recursion
|
||||
* Supports recursion.
|
||||
*
|
||||
* @return boolean - True if attempting to get 2 locks on the same resource will "stack"
|
||||
*/
|
||||
|
@ -39,7 +39,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class mysql_lock_factory implements lock_factory {
|
||||
|
||||
/** @var moodle_database $db Hold a reference to the global $DB */
|
||||
/** @var \moodle_database $db Hold a reference to the global $DB */
|
||||
protected $db;
|
||||
|
||||
/** @var string $type Used to prefix lock keys */
|
||||
|
@ -50,7 +50,7 @@ class postgres_lock_factory implements lock_factory {
|
||||
/** @var array $lockidcache - static cache for string -> int conversions required for pg advisory locks. */
|
||||
protected static $lockidcache = array();
|
||||
|
||||
/** @var moodle_database $db Hold a reference to the global $DB */
|
||||
/** @var \moodle_database $db Hold a reference to the global $DB */
|
||||
protected $db;
|
||||
|
||||
/** @var string $type Used to prefix lock keys */
|
||||
@ -133,10 +133,9 @@ class postgres_lock_factory implements lock_factory {
|
||||
*
|
||||
* @param string $key
|
||||
* @return int
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
protected function get_index_from_key($key) {
|
||||
global $DB;
|
||||
|
||||
if (isset(self::$lockidcache[$key])) {
|
||||
return self::$lockidcache[$key];
|
||||
}
|
||||
@ -152,7 +151,7 @@ class postgres_lock_factory implements lock_factory {
|
||||
$record->resourcekey = $key;
|
||||
try {
|
||||
$index = $this->db->insert_record('lock_db', $record);
|
||||
} catch (dml_exception $de) {
|
||||
} catch (\dml_exception $de) {
|
||||
// Race condition - never mind - now the value is guaranteed to exist.
|
||||
$record = $this->db->get_record('lock_db', array('resourcekey' => $key));
|
||||
if ($record) {
|
||||
@ -162,7 +161,7 @@ class postgres_lock_factory implements lock_factory {
|
||||
}
|
||||
|
||||
if (!$index) {
|
||||
throw new moodle_exception('Could not generate unique index for key');
|
||||
throw new \moodle_exception('Could not generate unique index for key');
|
||||
}
|
||||
|
||||
self::$lockidcache[$key] = $index;
|
||||
|
Loading…
x
Reference in New Issue
Block a user