2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Shutdown management class.
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();
28 * Shutdown management class.
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;
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'));
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);
64 * @private - do NOT call directly.
66 public static function shutdown_handler() {
69 // Custom stuff first.
70 foreach (self
::$callbacks as $data) {
71 list($callback, $params) = $data;
73 if (!is_callable($callback)) {
74 error_log('Invalid custom shutdown function detected '.var_export($callback, true));
77 if ($params === null) {
78 call_user_func($callback);
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();
106 self
::request_shutdown();
109 if (function_exists('profiling_is_running')) {
110 if (profiling_is_running()) {
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() {
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();
146 foreach ($inc as $f) {
147 if (preg_match(':^/:', $f)) {
150 $hfs = display_size($fs);
151 error_log(substr($f, strlen($CFG->dirroot
)) . " size: $fs ($hfs)", null, null, 0);
153 error_log($f , null, null, 0);
157 $hts = display_size($ts);
158 error_log("Total size of files included: $ts ($hts)");