mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 14:27:22 +01:00
MDL-59594 core: Allow custom signal handlers
This commit is contained in:
parent
77d1c41502
commit
29e3a223f5
@ -33,7 +33,9 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*/
|
||||
class core_shutdown_manager {
|
||||
/** @var array list of custom callbacks */
|
||||
protected static $callbacks = array();
|
||||
protected static $callbacks = [];
|
||||
/** @var array list of custom signal callbacks */
|
||||
protected static $signalcallbacks = [];
|
||||
/** @var bool is this manager already registered? */
|
||||
protected static $registered = false;
|
||||
|
||||
@ -66,7 +68,7 @@ class core_shutdown_manager {
|
||||
*
|
||||
* @param int $signo The signal being handled
|
||||
*/
|
||||
public static function signal_handler($signo) {
|
||||
public static function signal_handler(int $signo) {
|
||||
// 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.
|
||||
@ -92,17 +94,57 @@ class core_shutdown_manager {
|
||||
$exitcode = 1;
|
||||
}
|
||||
|
||||
// Normally we should exit unless a callback tells us to wait.
|
||||
$shouldexit = true;
|
||||
foreach (self::$signalcallbacks as $data) {
|
||||
list($callback, $params) = $data;
|
||||
try {
|
||||
array_unshift($params, $signo);
|
||||
$shouldexit = call_user_func_array($callback, $params) && $shouldexit;
|
||||
} catch (Throwable $e) {
|
||||
// @codingStandardsIgnoreStart
|
||||
error_log('Exception ignored in signal function ' . get_callable_name($callback) . ': ' . $e->getMessage());
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
||||
if ($shouldexit) {
|
||||
exit ($exitcode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register custom signal handler function.
|
||||
*
|
||||
* If a handler returns false the signal will be ignored.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
public static function register_signal_handler($callback, array $params = null): void {
|
||||
if (!is_callable($callback)) {
|
||||
// @codingStandardsIgnoreStart
|
||||
error_log('Invalid custom signal function detected ' . var_export($callback, true));
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
self::$signalcallbacks[] = [$callback, $params ?? []];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register custom shutdown function.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
public static function register_function($callback, array $params = null) {
|
||||
self::$callbacks[] = array($callback, $params);
|
||||
public static function register_function($callback, array $params = null): void {
|
||||
if (!is_callable($callback)) {
|
||||
// @codingStandardsIgnoreStart
|
||||
error_log('Invalid custom shutdown function detected '.var_export($callback, true));
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
self::$callbacks[] = [$callback, $params ?? []];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,20 +157,11 @@ class core_shutdown_manager {
|
||||
foreach (self::$callbacks as $data) {
|
||||
list($callback, $params) = $data;
|
||||
try {
|
||||
if (!is_callable($callback)) {
|
||||
error_log('Invalid custom shutdown function detected '.var_export($callback, true));
|
||||
continue;
|
||||
}
|
||||
if ($params === null) {
|
||||
call_user_func($callback);
|
||||
} else {
|
||||
call_user_func_array($callback, $params);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log('Exception ignored in shutdown function '.get_callable_name($callback).': '.$e->getMessage());
|
||||
} catch (Throwable $e) {
|
||||
// Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
|
||||
// @codingStandardsIgnoreStart
|
||||
error_log('Exception ignored in shutdown function '.get_callable_name($callback).': '.$e->getMessage());
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user