Differences Between: [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 namespace core\lock; 18 19 use core\progress\base; 20 21 /** 22 * Lock utilities. 23 * 24 * @package core 25 * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net} 26 * @author Mark Johnson <mark.johnson@catalyst-eu.net> 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 class lock_utils { 30 /** 31 * Start a progress bar and attempt to get a lock, updating the bar until a lock is achieved. 32 * 33 * This will make multiple attempts at getting the lock using a short timeout set by $progressupdatetime. After 34 * each failed attempt, it will update the progress bar and try again, until $timeout is reached. 35 * 36 * @param lock_factory $lockfactory The factory to use to get the lock 37 * @param string $resource The resource key we will try to get a lock on 38 * @param base $progress The progress bar 39 * @param int $timeout The maximum time in seconds to wait for a lock 40 * @param string $message Optional message to display on the progress bar 41 * @param int $progressupdatetime The number of seconds to wait for each lock attempt before updating the progress bar. 42 * @param int $maxlifetime The maxlifetime to set on the lock, if supported. 43 * @return lock|false A lock if successful, or false if the timeout expires. 44 * @throws \coding_exception 45 */ 46 public static function wait_for_lock_with_progress( 47 lock_factory $lockfactory, 48 string $resource, 49 \core\progress\base $progress, 50 int $timeout, 51 string $message = '', 52 int $progressupdatetime = 10, 53 int $maxlifetime = DAYSECS, 54 ) { 55 if ($progressupdatetime < 1) { 56 throw new \invalid_parameter_exception('Progress bar cannot update more than once per second. ' . 57 '$progressupdate time must be at least 1.'); 58 } 59 if ($progressupdatetime > $timeout) { 60 throw new \invalid_parameter_exception('$timeout must be greater than $progressupdatetime.'); 61 } 62 $lockattempts = 0; 63 $maxattempts = $timeout / $progressupdatetime; 64 $lock = false; 65 $progress->start_progress($message); 66 while (!$lock && $lockattempts < $maxattempts) { 67 $lock = $lockfactory->get_lock($resource, $progressupdatetime, $maxlifetime); 68 if (!$lock) { 69 $progress->progress(); 70 $lockattempts++; 71 } 72 } 73 $progress->end_progress(); 74 return $lock; 75 } 76 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body