Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Lock configuration class, used to get an instance of the currently configured lock factory.
  19   *
  20   * @package    core
  21   * @category   lock
  22   * @copyright  Damyon Wiese 2013
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\lock;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Lock configuration class, used to get an instance of the currently configured lock factory.
  32   *
  33   * @package   core
  34   * @category  lock
  35   * @copyright Damyon Wiese 2013
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class lock_config {
  39  
  40      /**
  41       * Get the currently configured locking subclass.
  42       *
  43       * @return string class name
  44       * @throws \coding_exception
  45       */
  46      public static function get_lock_factory_class(): string {
  47  
  48          global $CFG, $DB;
  49  
  50          if (during_initial_install()) {
  51              $lockfactoryclass = '\core\lock\installation_lock_factory';
  52          } else if (isset($CFG->lock_factory) && $CFG->lock_factory != 'auto') {
  53              if (!class_exists($CFG->lock_factory)) {
  54                  // In this case I guess it is not safe to continue. Different cluster nodes could end up using different locking
  55                  // types because of an installation error.
  56                  throw new \coding_exception('Lock factory set in $CFG does not exist: ' . $CFG->lock_factory);
  57              }
  58              $lockfactoryclass = $CFG->lock_factory;
  59          } else {
  60              $dbtype = clean_param($DB->get_dbfamily(), PARAM_ALPHA);
  61  
  62              // DB Specific lock factory is preferred - should support auto-release.
  63              $lockfactoryclass = "\\core\\lock\\$dbtype}_lock_factory";
  64              if (!class_exists($lockfactoryclass)) {
  65                  $lockfactoryclass = '\core\lock\file_lock_factory';
  66              }
  67  
  68              // Test if the auto chosen lock factory is available.
  69              $lockfactory = new $lockfactoryclass('test');
  70              if (!$lockfactory->is_available()) {
  71                  // Final fallback - DB row locking.
  72                  $lockfactoryclass = '\core\lock\db_record_lock_factory';
  73              }
  74          }
  75  
  76          return $lockfactoryclass;
  77      }
  78  
  79      /**
  80       * Get an instance of the currently configured locking subclass.
  81       *
  82       * @param string $type - Unique namespace for the locks generated by this factory. e.g. core_cron
  83       * @return \core\lock\lock_factory
  84       * @throws \coding_exception
  85       */
  86      public static function get_lock_factory(string $type): \core\lock\lock_factory {
  87          global $CFG;
  88  
  89          $lockfactoryclass = self::get_lock_factory_class();
  90          $lockfactory = new $lockfactoryclass($type);
  91          if (!$lockfactory->is_available()) {
  92              throw new \coding_exception("Lock factory class $lockfactoryclass is not available.");
  93          }
  94  
  95          // If tracking performance, insert a timing wrapper to keep track of lock delays.
  96          if ((defined('MDL_PERF') && MDL_PERF) || (!empty($CFG->perfdebug) && $CFG->perfdebug > 7)) {
  97              $wrapper = new timing_wrapper_lock_factory($type, $lockfactory);
  98              $lockfactory = $wrapper;
  99          }
 100  
 101          return $lockfactory;
 102      }
 103  
 104  }