Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 39 and 311]

   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 factory for use during installation.
  19   *
  20   * @package    core
  21   * @category   lock
  22   * @copyright  Andrew Nicols <andrew@nicols.co.uk>
  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 factory for use during installation.
  32   *
  33   * @package   core
  34   * @category  lock
  35   * @copyright  Andrew Nicols <andrew@nicols.co.uk>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class installation_lock_factory implements lock_factory {
  39  
  40      /**
  41       * Create this lock factory.
  42       *
  43       * @param string $type - The type, e.g. cron, cache, session
  44       */
  45      public function __construct($type) {
  46      }
  47  
  48      /**
  49       * Return information about the blocking behaviour of the lock type on this platform.
  50       *
  51       * @return boolean - False if attempting to get a lock will block indefinitely.
  52       */
  53      public function supports_timeout() {
  54          return true;
  55      }
  56  
  57      /**
  58       * This lock type will be automatically released when a process ends.
  59       *
  60       * @return boolean - True
  61       */
  62      public function supports_auto_release() {
  63          return true;
  64      }
  65  
  66      /**
  67       * This lock factory is only available during the initial installation.
  68       * To use it at any other time would be potentially dangerous.
  69       *
  70       * @return boolean
  71       */
  72      public function is_available() {
  73          return during_initial_install();
  74      }
  75  
  76      /**
  77       * Multiple locks for the same resource cannot be held from a single process.
  78       *
  79       * @deprecated since Moodle 3.10.
  80       * @return boolean - False
  81       */
  82      public function supports_recursion() {
  83          debugging('The function supports_recursion() is deprecated, please do not use it anymore.',
  84              DEBUG_DEVELOPER);
  85          return false;
  86      }
  87  
  88      /**
  89       * Get some info that might be useful for debugging.
  90       * @return boolean - string
  91       */
  92      protected function get_debug_info() {
  93          return 'host:' . php_uname('n') . ', pid:' . getmypid() . ', time:' . time();
  94      }
  95  
  96      /**
  97       * Get a lock within the specified timeout or return false.
  98       *
  99       * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
 100       * @param int $timeout - The number of seconds to wait for a lock before giving up.
 101       * @param int $maxlifetime - Unused by this lock type.
 102       * @return boolean - true if a lock was obtained.
 103       */
 104      public function get_lock($resource, $timeout, $maxlifetime = 86400) {
 105          return new lock($resource, $this);
 106      }
 107  
 108      /**
 109       * Release a lock that was previously obtained with @lock.
 110       *
 111       * @param lock $lock - A lock obtained from this factory.
 112       * @return boolean - true if the lock is no longer held (including if it was never held).
 113       */
 114      public function release_lock(lock $lock) {
 115          return true;
 116      }
 117  
 118      /**
 119       * Extend a lock that was previously obtained with @lock.
 120       *
 121       * @deprecated since Moodle 3.10.
 122       * @param lock $lock - not used
 123       * @param int $maxlifetime - not used
 124       * @return boolean - true if the lock was extended.
 125       */
 126      public function extend_lock(lock $lock, $maxlifetime = 86400) {
 127          debugging('The function extend_lock() is deprecated, please do not use it anymore.',
 128              DEBUG_DEVELOPER);
 129          // Not supported by this factory.
 130          return false;
 131      }
 132  
 133  }