MDL-87046 core: Update namespace of shutdown manager

This commit is contained in:
Andrew Nicols
2025-11-21 08:52:30 +08:00
parent 8e0788fd04
commit 36488dcab8
19 changed files with 43 additions and 46 deletions
+1 -1
View File
@@ -160,7 +160,7 @@ function tool_dbtransfer_get_drivers() {
function tool_dbtransfer_create_maintenance_file() {
global $CFG;
core_shutdown_manager::register_function('tool_dbtransfer_maintenance_callback');
\core\shutdown_manager::register_function('tool_dbtransfer_maintenance_callback');
$options = new stdClass();
$options->trusted = false;
@@ -50,7 +50,7 @@ class manager implements \core\log\manager {
$this->writers = array();
// Register shutdown handler - this may be useful for buffering, file handle closing, etc.
\core_shutdown_manager::register_function(array($this, 'dispose'));
\core\shutdown_manager::register_function([$this, 'dispose']);
$plugins = get_config('tool_log', 'enabled_stores');
if (empty($plugins)) {
@@ -78,7 +78,7 @@ class eventobservers {
if (empty(self::$instance)) {
self::$instance = new static();
// Register shutdown handler - this is useful for buffering, processing events, etc.
\core_shutdown_manager::register_function(array(self::$instance, 'process_buffer'));
\core\shutdown_manager::register_function([self::$instance, 'process_buffer']);
}
self::$instance->buffer_event($event);
@@ -20,7 +20,7 @@ class cssparser {
public function __construct($html = true) {
// Register "destructor"
core_shutdown_manager::register_function(array(&$this, "finalize"));
\core\shutdown_manager::register_function([&$this, "finalize"]);
$this->html = ($html != false);
$this->Clear();
}
+2 -1
View File
@@ -22,6 +22,7 @@ use core_cache\searchable_cache_interface;
use core_cache\store;
use core\clock;
use core\di;
use core\shutdown_manager;
/**
* Redis Cache Store
@@ -689,7 +690,7 @@ class cachestore_redis extends store implements
// If we haven't got it already, better register a shutdown function.
if ($this->currentlocks === null) {
core_shutdown_manager::register_function([$this, 'shutdown_release_locks']);
shutdown_manager::register_function([$this, 'shutdown_release_locks']);
$this->currentlocks = [];
}
@@ -117,7 +117,7 @@ class converter implements \core_files\converter_interface {
// Copy the file to the tmp dir.
$uniqdir = make_unique_writable_directory(make_temp_directory('core_file/conversions'));
\core_shutdown_manager::register_function('remove_dir', array($uniqdir));
\core\shutdown_manager::register_function('remove_dir', [$uniqdir]);
$localfilename = $file->get_id() . '.' . $fromformat;
$filename = $uniqdir . '/' . $localfilename;
+1 -14
View File
@@ -14,18 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* CLI script shutdown helper class.
*
* @package core
* @copyright 2019 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\local\cli;
defined('MOODLE_INTERNAL') || die();
/**
* CLI script shutdown helper class.
*
@@ -34,7 +24,6 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class shutdown {
/** @var bool Should we exit gracefully at the next opportunity? */
protected static $cligracefulexit = false;
@@ -44,7 +33,7 @@ class shutdown {
* @return void
*/
public static function script_supports_graceful_exit(): void {
\core_shutdown_manager::register_signal_handler('\core\local\cli\shutdown::signal_handler');
\core\shutdown_manager::register_signal_handler('\core\local\cli\shutdown::signal_handler');
}
/**
@@ -66,7 +55,6 @@ class shutdown {
* @return bool true if we should exit
*/
public static function signal_handler(int $signo): bool {
if (self::$cligracefulexit) {
cli_heading(get_string('cliexitnow', 'admin'));
return true;
@@ -78,4 +66,3 @@ class shutdown {
}
}
@@ -60,7 +60,7 @@ class db_record_lock_factory implements lock_factory {
// Save a reference to the global $DB so it will not be released while we still have open locks.
$this->db = $DB;
\core_shutdown_manager::register_function(array($this, 'auto_release'));
\core\shutdown_manager::register_function([$this, 'auto_release']);
}
/**
@@ -63,7 +63,7 @@ class mysql_lock_factory implements lock_factory {
// Save a reference to the global $DB so it will not be released while we still have open locks.
$this->db = $DB;
\core_shutdown_manager::register_function([$this, 'auto_release']);
\core\shutdown_manager::register_function([$this, 'auto_release']);
}
/**
@@ -85,7 +85,7 @@ class postgres_lock_factory implements lock_factory {
// Save a reference to the global $DB so it will not be released while we still have open locks.
$this->db = $DB;
\core_shutdown_manager::register_function(array($this, 'auto_release'));
\core\shutdown_manager::register_function([$this, 'auto_release']);
}
/**
@@ -18,7 +18,7 @@ namespace core\navigation;
use core_cache\cache;
use core_cache\session_cache;
use core_shutdown_manager;
use core\shutdown_manager;
/**
* The navigation_cache class is used for global and settings navigation data.
@@ -175,7 +175,7 @@ class navigation_cache {
public function volatile($setting = true) {
if (self::$volatilecaches === null) {
self::$volatilecaches = [];
core_shutdown_manager::register_function(['navigation_cache', 'destroy_volatile_caches']);
shutdown_manager::register_function(['navigation_cache', 'destroy_volatile_caches']);
}
if ($setting) {
+1 -1
View File
@@ -722,7 +722,7 @@ class manager {
// If debugging, take a snapshot of session at close and compare on shutdown to detect any accidental mutations.
if (debugging()) {
self::$sessionatclose = (array) $_SESSION['SESSION'];
\core_shutdown_manager::register_function('\core\session\manager::check_mutated_closed_session');
\core\shutdown_manager::register_function('\core\session\manager::check_mutated_closed_session');
}
if (!$requireslock || !self::$requireslockdebug) {
+20 -11
View File
@@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core;
use Throwable;
/**
* Shutdown management class.
*
@@ -21,13 +25,13 @@
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_shutdown_manager {
class shutdown_manager {
/** @var array list of custom callbacks */
protected static $callbacks = [];
protected static array $callbacks = [];
/** @var array list of custom signal callbacks */
protected static $signalcallbacks = [];
protected static array $signalcallbacks = [];
/** @var bool is this manager already registered? */
protected static $registered = false;
protected static bool $registered = false;
/** @var array A list of pcntl handlers */
protected static array $pcntlhandlers = [];
@@ -37,7 +41,7 @@ class core_shutdown_manager {
*
* Note: This method should _only_ be called from lib/setup.php.
*/
public static function initialize() {
public static function initialize(): void {
if (self::$registered) {
debugging('Shutdown manager is already initialised!');
return;
@@ -77,7 +81,7 @@ class core_shutdown_manager {
*
* @param int $signo The signal being handled
*/
public static function signal_handler(int $signo) {
public static function signal_handler(int $signo): void {
// Note: There is no need to manually call the shutdown handler.
// The fact that we are calling exit() in this script means that the standard shutdown handling is performed
// anyway.
@@ -99,7 +103,7 @@ class core_shutdown_manager {
default:
// The signal handler was called with a signal it was not expecting.
// We should exit and complain.
echo "Warning: \core_shutdown_manager::signal_handler() was called with an unexpected signal ({$signo}).\n";
echo "Warning: \core\shutdown_manager::signal_handler() was called with an unexpected signal ({$signo}).\n";
$exitcode = 1;
}
@@ -134,7 +138,7 @@ class core_shutdown_manager {
* @param array $params
* @return void
*/
public static function register_signal_handler($callback, ?array $params = null): void {
public static function register_signal_handler(callable $callback, ?array $params = null): void {
if (!is_callable($callback)) {
// phpcs:ignore moodle.PHP.ForbiddenFunctions.FoundWithAlternative
error_log('Invalid custom signal function detected ' . var_export($callback, true)); // phpcs:ignore
@@ -149,7 +153,7 @@ class core_shutdown_manager {
* @param array $params
* @return void
*/
public static function register_function($callback, ?array $params = null): void {
public static function register_function(callable $callback, ?array $params = null): void {
if (!is_callable($callback)) {
// phpcs:ignore moodle.PHP.ForbiddenFunctions.FoundWithAlternative
error_log('Invalid custom shutdown function detected '.var_export($callback, true)); // phpcs:ignore
@@ -162,7 +166,7 @@ class core_shutdown_manager {
*
* Note: DO NOT call this method directly. It will be called automatically on shutdown.
*/
public static function shutdown_handler() {
public static function shutdown_handler(): void {
global $DB;
// In case we caught an out of memory shutdown we increase memory limit to unlimited, so we can gracefully shut down.
@@ -219,7 +223,7 @@ class core_shutdown_manager {
/**
* Standard shutdown sequence.
*/
protected static function request_shutdown() {
protected static function request_shutdown(): void {
global $CFG, $OUTPUT, $PERF;
// Help apache server if possible.
@@ -281,3 +285,8 @@ class core_shutdown_manager {
}
}
}
// Alias this class to the old name.
// This file will be autoloaded by the legacyclasses autoload system.
// In future all uses of this class will be corrected and the legacy references will be removed.
class_alias(shutdown_manager::class, \core_shutdown_manager::class);
+1 -1
View File
@@ -96,7 +96,7 @@ class logmanager {
// We register a shutdown handler to ensure that logs causing any failures are correctly disposed of.
// Note: This must happen before the per-request directory is requested because the shutdown handler deletes the logfile.
if (!self::$tasklogregistered) {
\core_shutdown_manager::register_function(function() {
\core\shutdown_manager::register_function(function (): void {
// These will only actually do anything if capturing is current active when the thread ended, which
// constitutes a failure.
\core\task\logmanager::finalise_log(true);
+2 -2
View File
@@ -26,7 +26,7 @@ namespace core\task;
use core\lock\lock;
use core\lock\lock_factory;
use core_shutdown_manager;
use core\shutdown_manager;
define('CORE_TASK_TASKS_FILENAME', 'db/tasks.php');
/**
@@ -1120,7 +1120,7 @@ class manager {
// Add \core\task\manager::fail_running_task to shutdown manager, so we can ensure running tasks fail on shutdown.
if (!self::$registeredshutdownhandler) {
core_shutdown_manager::register_function('\core\task\manager::fail_running_task');
shutdown_manager::register_function('\core\task\manager::fail_running_task');
self::$registeredshutdownhandler = true;
}
+1 -1
View File
@@ -2363,7 +2363,7 @@ function send_temp_file($path, $filename, $pathisstring=false) {
throw new \moodle_exception('filenotfound', 'error', $CFG->wwwroot.'/');
}
// executed after normal finish or abort
core_shutdown_manager::register_function('send_temp_file_finished', array($path));
\core\shutdown_manager::register_function('send_temp_file_finished', [$path]);
}
// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
+2 -2
View File
@@ -779,7 +779,7 @@ if (!isset($CFG->debugdisplay)) {
}
// Register our shutdown manager, do NOT use register_shutdown_function().
core_shutdown_manager::initialize();
\core\shutdown_manager::initialize();
// Verify upgrade is not running unless we are in a script that needs to execute in any case
if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
@@ -1187,7 +1187,7 @@ if (isset($CFG->maintenance_later) and $CFG->maintenance_later <= time()) {
// Add behat_shutdown_function to shutdown manager, so we can capture php errors,
// but not necessary for behat CLI command as it's being captured by behat process.
if (defined('BEHAT_SITE_RUNNING') && !defined('BEHAT_TEST')) {
core_shutdown_manager::register_function('behat_shutdown_function');
\core\shutdown_manager::register_function('behat_shutdown_function');
}
// note: we can not block non utf-8 installations here, because empty mysql database
+1 -1
View File
@@ -1416,7 +1416,7 @@ function get_request_storage_directory($exceptiononerror = true, bool $forcecrea
if ($dir = make_unique_writable_directory($basedir, $exceptiononerror)) {
// Register a shutdown handler to remove the directory.
\core_shutdown_manager::register_function('remove_dir', [$dir]);
\core\shutdown_manager::register_function('remove_dir', [$dir]);
}
$requestdir = $dir;
+2 -2
View File
@@ -260,7 +260,7 @@ final class manager_test extends \advanced_testcase {
self::assertEmpty(manager::get_failed_adhoc_tasks());
// Trigger shutdown handler.
\core_shutdown_manager::shutdown_handler();
\core\shutdown_manager::shutdown_handler();
$failedtasks = manager::get_failed_adhoc_tasks();
@@ -292,7 +292,7 @@ final class manager_test extends \advanced_testcase {
$this->assertCount(1, $running);
// Trigger shutdown handler.
\core_shutdown_manager::shutdown_handler();
\core\shutdown_manager::shutdown_handler();
$running = manager::get_running_tasks();
$this->assertCount(0, $running);