MDL-59506 core_lock: use a null lock during install
[moodle.git] / lib / classes / lock / lock_config.php
CommitLineData
9843e5ec
DW
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
26namespace core\lock;
27
28defined('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 */
38class lock_config {
39
40 /**
41 * Get an instance of the currently configured locking subclass.
42 *
43 * @param string $type - Unique namespace for the locks generated by this factory. e.g. core_cron
44 * @return \core\lock\lock_factory
34027f15 45 * @throws \coding_exception
9843e5ec
DW
46 */
47 public static function get_lock_factory($type) {
48 global $CFG, $DB;
49 $lockfactory = null;
50
5bc4c797
AN
51 if (during_initial_install()) {
52 $lockfactory = new \core\lock\installation_lock_factory($type);
53 } else if (isset($CFG->lock_factory) && $CFG->lock_factory != 'auto') {
9843e5ec
DW
54 if (!class_exists($CFG->lock_factory)) {
55 // In this case I guess it is not safe to continue. Different cluster nodes could end up using different locking
56 // types because of an installation error.
57 throw new \coding_exception('Lock factory set in $CFG does not exist: ' . $CFG->lock_factory);
58 }
59 $lockfactoryclass = $CFG->lock_factory;
60 $lockfactory = new $lockfactoryclass($type);
61 } else {
62 $dbtype = clean_param($DB->get_dbfamily(), PARAM_ALPHA);
63
34027f15 64 // DB Specific lock factory is preferred - should support auto-release.
9843e5ec
DW
65 $lockfactoryclass = "\\core\\lock\\${dbtype}_lock_factory";
66 if (!class_exists($lockfactoryclass)) {
d5511125 67 $lockfactoryclass = '\core\lock\file_lock_factory';
9843e5ec 68 }
34027f15 69 /* @var lock_factory $lockfactory */
9843e5ec 70 $lockfactory = new $lockfactoryclass($type);
d5511125
DW
71 if (!$lockfactory->is_available()) {
72 // Final fallback - DB row locking.
73 $lockfactory = new \core\lock\db_record_lock_factory($type);
74 }
9843e5ec
DW
75 }
76
77 return $lockfactory;
78 }
79
80}