MDL-87107 core: Improve upgrade running detection

This commit is contained in:
Andrew Nicols
2025-11-07 17:19:20 +08:00
parent 5cfff76727
commit 39a08965a2
5 changed files with 69 additions and 24 deletions
@@ -0,0 +1,16 @@
issueNumber: MDL-87107
notes:
core:
- message: >
The `upgrade_ensure_not_running()` function has been deprecated and
replaced
with:
- `\core\setup::warn_if_upgrade_is_running()`;
- `\core\setup::ensure_upgrade_is_not_running()`; and
- `\core\setup::is_upgrade_running()`.
type: improved
+1 -13
View File
@@ -699,7 +699,7 @@ final class manager implements
return false;
}
if ($this->is_upgrade_running()) {
if (\core\setup::is_upgrade_running()) {
// Do not use the cache during upgrade.
return false;
}
@@ -747,16 +747,4 @@ final class manager implements
rename($tmppath, $cachepath);
clearstatcache(true, $cachepath);
}
/**
* Check whether upgrade is currently running.
*
* @return bool
*/
protected function is_upgrade_running(): bool {
global $CFG;
// Note: This mimics the test in lib/setuplib.php during upgrade.
return !empty($CFG->upgraderunning);
}
}
+40
View File
@@ -80,4 +80,44 @@ class setup {
return str_ends_with($CFG->wwwroot, '/');
}
/**
* Check whether an upgrade is currently running.
*
* @return bool
*/
public static function is_upgrade_running(): bool {
global $CFG;
return !empty($CFG->upgraderunning);
}
/**
* Ensure that an upgrade is not running, emitting an exception if it is.
*
* @throws moodle_exception
* @return bool false if no upgrade is running
*/
public static function ensure_upgrade_is_not_running(): bool {
if (self::is_upgrade_running()) {
throw new moodle_exception('cannotexecduringupgrade');
}
return false;
}
/**
* Warn if an upgrade is currently running.
*
* @return bool true if an upgrade is running, false if no upgrade is running
*/
public static function warn_if_upgrade_is_running(): bool {
if (self::is_upgrade_running()) {
debugging(get_string('cannotexecduringupgrade', 'error'), DEBUG_DEVELOPER);
return true;
}
return false;
}
}
+2 -2
View File
@@ -66,7 +66,7 @@ function get_fast_modinfo($courseorid, $userid = 0, $resetonly = false) {
// Function get_fast_modinfo() can never be called during upgrade unless it is used for clearing cache only.
if (!$resetonly) {
upgrade_ensure_not_running();
\core\setup::ensure_upgrade_is_not_running();
}
// Function is called with $reset = true.
@@ -269,7 +269,7 @@ function rebuild_course_cache(int $courseid = 0, bool $clearonly = false, bool $
}
// Function rebuild_course_cache() can not be called during upgrade unless it's clear only.
if (!$clearonly && !upgrade_ensure_not_running(true)) {
if (!$clearonly && \core\setup::warn_if_upgrade_is_running()) {
$clearonly = true;
}
+10 -9
View File
@@ -1202,17 +1202,18 @@ function redirect_if_major_upgrade_required() {
* @param bool $warningonly if true displays a warning instead of throwing an exception
* @return bool true if executed from outside of upgrade process, false if from inside upgrade process and function is used for warning only
*/
#[\core\attribute\deprecated(
replacement: 'Use \core\setup::ensure_upgrade_is_not_running() or \core\setup::warn_if_upgrade_is_running() instead.',
mdl: 'MDL-87107',
since: '5.2',
)]
function upgrade_ensure_not_running($warningonly = false) {
global $CFG;
if (!empty($CFG->upgraderunning)) {
if (!$warningonly) {
throw new moodle_exception('cannotexecduringupgrade');
} else {
debugging(get_string('cannotexecduringupgrade', 'error'), DEBUG_DEVELOPER);
return false;
}
\core\deprecation::emit_deprecation(__FUNCTION__);
if ($warningonly) {
return !\core\setup::warn_if_upgrade_is_running();
} else {
return !\core\setup::ensure_upgrade_is_not_running();
}
return true;
}
/**