MDL-51177 core: Ignore built files in stylelint
[moodle.git] / lib / classes / shutdown_manager.php
blob2a11ae2d09029a779b2cf3c1d2c40d2123c4c726
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.
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/>.
17 /**
18 * Shutdown management class.
20 * @package core
21 * @copyright 2013 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Shutdown management class.
30 * @package core
31 * @copyright 2013 Petr Skoda {@link http://skodak.org}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_shutdown_manager {
35 /** @var array list of custom callbacks */
36 protected static $callbacks = array();
37 /** @var bool is this manager already registered? */
38 protected static $registered = false;
40 /**
41 * Register self as main shutdown handler.
43 * @private to be called from lib/setup.php only!
45 public static function initialize() {
46 if (self::$registered) {
47 debugging('Shutdown manager is already initialised!');
49 self::$registered = true;
50 register_shutdown_function(array('core_shutdown_manager', 'shutdown_handler'));
53 /**
54 * Register custom shutdown function.
56 * @param callable $callback
57 * @param array $params
59 public static function register_function($callback, array $params = null) {
60 self::$callbacks[] = array($callback, $params);
63 /**
64 * @private - do NOT call directly.
66 public static function shutdown_handler() {
67 global $DB;
69 // Custom stuff first.
70 foreach (self::$callbacks as $data) {
71 list($callback, $params) = $data;
72 try {
73 if (!is_callable($callback)) {
74 error_log('Invalid custom shutdown function detected '.var_export($callback, true));
75 continue;
77 if ($params === null) {
78 call_user_func($callback);
79 } else {
80 call_user_func_array($callback, $params);
82 } catch (Exception $e) {
83 error_log('Exception ignored in shutdown function '.var_export($callback, true).':'.$e->getMessage());
84 } catch (Throwable $e) {
85 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
86 error_log('Exception ignored in shutdown function '.var_export($callback, true).':'.$e->getMessage());
90 // Handle DB transactions, session need to be written afterwards
91 // in order to maintain consistency in all session handlers.
92 if ($DB->is_transaction_started()) {
93 if (!defined('PHPUNIT_TEST') or !PHPUNIT_TEST) {
94 // This should not happen, it usually indicates wrong catching of exceptions,
95 // because all transactions should be finished manually or in default exception handler.
96 $backtrace = $DB->get_transaction_start_backtrace();
97 error_log('Potential coding error - active database transaction detected during request shutdown:'."\n".format_backtrace($backtrace, true));
99 $DB->force_transaction_rollback();
102 // Close sessions - do it here to make it consistent for all session handlers.
103 \core\session\manager::write_close();
105 // Other cleanup.
106 self::request_shutdown();
108 // Stop profiling.
109 if (function_exists('profiling_is_running')) {
110 if (profiling_is_running()) {
111 profiling_stop();
115 // NOTE: do not dispose $DB and MUC here, they might be used from legacy shutdown functions.
119 * Standard shutdown sequence.
121 protected static function request_shutdown() {
122 global $CFG;
124 // Help apache server if possible.
125 $apachereleasemem = false;
126 if (function_exists('apache_child_terminate') && function_exists('memory_get_usage') && ini_get_bool('child_terminate')) {
127 $limit = (empty($CFG->apachemaxmem) ? 64*1024*1024 : $CFG->apachemaxmem); // 64MB default.
128 if (memory_get_usage() > get_real_size($limit)) {
129 $apachereleasemem = $limit;
130 @apache_child_terminate();
134 // Deal with perf logging.
135 if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) {
136 if ($apachereleasemem) {
137 error_log('Mem usage over '.$apachereleasemem.': marking Apache child for reaping.');
139 if (defined('MDL_PERFTOLOG')) {
140 $perf = get_performance_info();
141 error_log("PERF: " . $perf['txt']);
143 if (defined('MDL_PERFINC')) {
144 $inc = get_included_files();
145 $ts = 0;
146 foreach ($inc as $f) {
147 if (preg_match(':^/:', $f)) {
148 $fs = filesize($f);
149 $ts += $fs;
150 $hfs = display_size($fs);
151 error_log(substr($f, strlen($CFG->dirroot)) . " size: $fs ($hfs)", null, null, 0);
152 } else {
153 error_log($f , null, null, 0);
156 if ($ts > 0 ) {
157 $hts = display_size($ts);
158 error_log("Total size of files included: $ts ($hts)");