Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * Shutdown management class.
19
 *
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
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Shutdown management class.
29
 *
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
33
 */
34
class core_shutdown_manager {
35
    /** @var array list of custom callbacks */
36
    protected static $callbacks = [];
37
    /** @var array list of custom signal callbacks */
38
    protected static $signalcallbacks = [];
39
    /** @var bool is this manager already registered? */
40
    protected static $registered = false;
41
 
42
    /**
43
     * Register self as main shutdown handler.
44
     *
45
     * @private to be called from lib/setup.php only!
46
     */
47
    public static function initialize() {
48
        if (self::$registered) {
49
            debugging('Shutdown manager is already initialised!');
50
        }
51
        self::$registered = true;
52
        register_shutdown_function(array('core_shutdown_manager', 'shutdown_handler'));
53
 
1441 ariadna 54
        // Signal handlers are recommended for the best possible shutdown handling.
55
        // They require the 'pcntl' extension to be loaded and the following functions to be available:
56
        // 'pcntl_async_signals'
57
        // 'pcntl_signal'
1 efrain 58
        // The 'pcntl' extension is optional and not available on Windows.
1441 ariadna 59
        if (extension_loaded('pcntl')) {
60
            if (function_exists('pcntl_async_signals')) {
61
                // We capture and handle SIGINT (Ctrl+C) and SIGTERM (termination requested).
62
                pcntl_async_signals(true);
63
            }
64
            if (function_exists('pcntl_signal')) {
65
                pcntl_signal(SIGINT, ['core_shutdown_manager', 'signal_handler']);
66
                pcntl_signal(SIGTERM, ['core_shutdown_manager', 'signal_handler']);
67
            }
1 efrain 68
        }
69
    }
70
 
71
    /**
72
     * Signal handler for SIGINT, and SIGTERM.
73
     *
74
     * @param   int     $signo The signal being handled
75
     */
76
    public static function signal_handler(int $signo) {
77
        // Note: There is no need to manually call the shutdown handler.
78
        // The fact that we are calling exit() in this script means that the standard shutdown handling is performed
79
        // anyway.
80
        switch ($signo) {
81
            case SIGTERM:
82
                // Replicate native behaviour.
83
                echo "Terminated: {$signo}\n";
84
 
85
                // The standard exit code for SIGTERM is 143.
86
                $exitcode = 143;
87
                break;
88
            case SIGINT:
89
                // Replicate native behaviour.
90
                echo "\n";
91
 
92
                // The standard exit code for SIGINT (Ctrl+C) is 130.
93
                $exitcode = 130;
94
                break;
95
            default:
96
                // The signal handler was called with a signal it was not expecting.
97
                // We should exit and complain.
98
                echo "Warning: \core_shutdown_manager::signal_handler() was called with an unexpected signal ({$signo}).\n";
99
                $exitcode = 1;
100
        }
101
 
102
        // Normally we should exit unless a callback tells us to wait.
103
        $shouldexit = true;
104
        foreach (self::$signalcallbacks as $data) {
105
            list($callback, $params) = $data;
106
            try {
107
                array_unshift($params, $signo);
108
                $shouldexit = call_user_func_array($callback, $params) && $shouldexit;
109
            } catch (Throwable $e) {
110
                // phpcs:ignore
111
                error_log('Exception ignored in signal function ' . get_callable_name($callback) . ': ' . $e->getMessage());
112
            }
113
        }
114
 
115
        if ($shouldexit) {
116
            exit ($exitcode);
117
        }
118
    }
119
 
120
    /**
121
     * Register custom signal handler function.
122
     *
123
     * If a handler returns false the signal will be ignored.
124
     *
125
     * @param callable $callback
126
     * @param array $params
127
     * @return void
128
     */
1441 ariadna 129
    public static function register_signal_handler($callback, ?array $params = null): void {
1 efrain 130
        if (!is_callable($callback)) {
131
            error_log('Invalid custom signal function detected ' . var_export($callback, true)); // phpcs:ignore
132
        }
133
        self::$signalcallbacks[] = [$callback, $params ?? []];
134
    }
135
 
136
    /**
137
     * Register custom shutdown function.
138
     *
139
     * @param callable $callback
140
     * @param array $params
141
     * @return void
142
     */
1441 ariadna 143
    public static function register_function($callback, ?array $params = null): void {
1 efrain 144
        if (!is_callable($callback)) {
145
            error_log('Invalid custom shutdown function detected '.var_export($callback, true)); // phpcs:ignore
146
        }
147
        self::$callbacks[] = [$callback, $params ? array_values($params) : []];
148
    }
149
 
150
    /**
151
     * @private - do NOT call directly.
152
     */
153
    public static function shutdown_handler() {
154
        global $DB;
155
 
156
        // In case we caught an out of memory shutdown we increase memory limit to unlimited, so we can gracefully shut down.
157
        raise_memory_limit(MEMORY_UNLIMITED);
158
 
159
        // Always ensure we know who the user is in access logs even if they
160
        // were logged in a weird way midway through the request.
161
        set_access_log_user();
162
 
163
        // Custom stuff first.
164
        foreach (self::$callbacks as $data) {
165
            list($callback, $params) = $data;
166
            try {
167
                call_user_func_array($callback, $params);
168
            } catch (Throwable $e) {
169
                // phpcs:ignore
170
                error_log('Exception ignored in shutdown function '.get_callable_name($callback).': '.$e->getMessage());
171
            }
172
        }
173
 
174
        // Handle DB transactions, session need to be written afterwards
175
        // in order to maintain consistency in all session handlers.
176
        if ($DB->is_transaction_started()) {
177
            if (!defined('PHPUNIT_TEST') or !PHPUNIT_TEST) {
178
                // This should not happen, it usually indicates wrong catching of exceptions,
179
                // because all transactions should be finished manually or in default exception handler.
180
                $backtrace = $DB->get_transaction_start_backtrace();
181
                error_log('Potential coding error - active database transaction detected during request shutdown:'."\n".format_backtrace($backtrace, true));
182
            }
183
            $DB->force_transaction_rollback();
184
        }
185
 
186
        // Close sessions - do it here to make it consistent for all session handlers.
187
        \core\session\manager::write_close();
188
 
189
        // Other cleanup.
190
        self::request_shutdown();
191
 
192
        // Stop profiling.
193
        if (function_exists('profiling_is_running')) {
194
            if (profiling_is_running()) {
195
                profiling_stop();
196
            }
197
        }
198
 
199
        // NOTE: do not dispose $DB and MUC here, they might be used from legacy shutdown functions.
200
    }
201
 
202
    /**
203
     * Standard shutdown sequence.
204
     */
205
    protected static function request_shutdown() {
206
        global $CFG, $OUTPUT, $PERF;
207
 
208
        // Help apache server if possible.
209
        $apachereleasemem = false;
210
        if (function_exists('apache_child_terminate') && function_exists('memory_get_usage') && ini_get_bool('child_terminate')) {
211
            $limit = (empty($CFG->apachemaxmem) ? 64*1024*1024 : $CFG->apachemaxmem); // 64MB default.
212
            if (memory_get_usage() > get_real_size($limit)) {
213
                $apachereleasemem = $limit;
214
                @apache_child_terminate();
215
            }
216
        }
217
 
218
        // Deal with perf logging.
219
        if (MDL_PERF || (!empty($CFG->perfdebug) && $CFG->perfdebug > 7)) {
220
            if ($apachereleasemem) {
221
                error_log('Mem usage over '.$apachereleasemem.': marking Apache child for reaping.');
222
            }
223
            if (MDL_PERFTOLOG) {
224
                $perf = get_performance_info();
225
                error_log("PERF: " . $perf['txt']);
226
            }
227
            if (!empty($PERF->perfdebugdeferred)) {
228
                $perf = get_performance_info();
229
                echo $OUTPUT->select_element_for_replace('#perfdebugfooter', $perf['html']);
230
            }
231
            if (MDL_PERFINC) {
232
                $inc = get_included_files();
233
                $ts  = 0;
234
                foreach ($inc as $f) {
235
                    if (preg_match(':^/:', $f)) {
236
                        $fs = filesize($f);
237
                        $ts += $fs;
238
                        $hfs = display_size($fs);
239
                        error_log(substr($f, strlen($CFG->dirroot)) . " size: $fs ($hfs)");
240
                    } else {
241
                        error_log($f);
242
                    }
243
                }
244
                if ($ts > 0 ) {
245
                    $hts = display_size($ts);
246
                    error_log("Total size of files included: $ts ($hts)");
247
                }
248
            }
249
        }
250
 
251
        // Close the current streaming element if any.
252
        if ($OUTPUT->has_started()) {
253
            echo $OUTPUT->close_element_for_append();
254
        }
255
 
256
        // Print any closing buffered tags.
257
        if (!empty($CFG->closingtags)) {
258
            echo $CFG->closingtags;
259
        }
260
    }
261
}