Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 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       * @return boolean - False
  80       */
  81      public function supports_recursion() {
  82          return false;
  83      }
  84  
  85      /**
  86       * Get some info that might be useful for debugging.
  87       * @return boolean - string
  88       */
  89      protected function get_debug_info() {
  90          return 'host:' . php_uname('n') . ', pid:' . getmypid() . ', time:' . time();
  91      }
  92  
  93      /**
  94       * Get a lock within the specified timeout or return false.
  95       *
  96       * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
  97       * @param int $timeout - The number of seconds to wait for a lock before giving up.
  98       * @param int $maxlifetime - Unused by this lock type.
  99       * @return boolean - true if a lock was obtained.
 100       */
 101      public function get_lock($resource, $timeout, $maxlifetime = 86400) {
 102          return new lock($resource, $this);
 103      }
 104  
 105      /**
 106       * Release a lock that was previously obtained with @lock.
 107       *
 108       * @param lock $lock - A lock obtained from this factory.
 109       * @return boolean - true if the lock is no longer held (including if it was never held).
 110       */
 111      public function release_lock(lock $lock) {
 112          return true;
 113      }
 114  
 115      /**
 116       * Extend a lock that was previously obtained with @lock.
 117       *
 118       * @param lock $lock - not used
 119       * @param int $maxlifetime - not used
 120       * @return boolean - true if the lock was extended.
 121       */
 122      public function extend_lock(lock $lock, $maxlifetime = 86400) {
 123          // Not supported by this factory.
 124          return false;
 125      }
 126  
 127  }