mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-06 05:07:36 +02:00
SignalHandler cleanups
This commit is contained in:
@@ -24,49 +24,45 @@ class SignalHandler
|
|||||||
{
|
{
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
private $previousSignalHandler = array();
|
private $previousSignalHandler = [];
|
||||||
private $signalLevelMap = array();
|
private $signalLevelMap = [];
|
||||||
private $signalRestartSyscalls = array();
|
private $signalRestartSyscalls = [];
|
||||||
|
|
||||||
public function __construct(LoggerInterface $logger)
|
public function __construct(LoggerInterface $logger)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, bool $callPrevious = true, bool $restartSyscalls = true, bool $async = true): self
|
public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, bool $callPrevious = true, bool $restartSyscalls = true, ?bool $async = true): self
|
||||||
{
|
{
|
||||||
if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) {
|
if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($callPrevious) {
|
if ($callPrevious) {
|
||||||
if (function_exists('pcntl_signal_get_handler')) {
|
$handler = pcntl_signal_get_handler($signo);
|
||||||
$handler = pcntl_signal_get_handler($signo);
|
if ($handler === false) {
|
||||||
if ($handler === false) {
|
return $this;
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
$this->previousSignalHandler[$signo] = $handler;
|
|
||||||
} else {
|
|
||||||
$this->previousSignalHandler[$signo] = true;
|
|
||||||
}
|
}
|
||||||
|
$this->previousSignalHandler[$signo] = $handler;
|
||||||
} else {
|
} else {
|
||||||
unset($this->previousSignalHandler[$signo]);
|
unset($this->previousSignalHandler[$signo]);
|
||||||
}
|
}
|
||||||
$this->signalLevelMap[$signo] = $level;
|
$this->signalLevelMap[$signo] = $level;
|
||||||
$this->signalRestartSyscalls[$signo] = $restartSyscalls;
|
$this->signalRestartSyscalls[$signo] = $restartSyscalls;
|
||||||
|
|
||||||
if (function_exists('pcntl_async_signals') && $async !== null) {
|
if ($async !== null) {
|
||||||
pcntl_async_signals($async);
|
pcntl_async_signals($async);
|
||||||
}
|
}
|
||||||
|
|
||||||
pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
|
pcntl_signal($signo, [$this, 'handleSignal'], $restartSyscalls);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleSignal($signo, array $siginfo = null)
|
public function handleSignal($signo, array $siginfo = null)
|
||||||
{
|
{
|
||||||
static $signals = array();
|
static $signals = [];
|
||||||
|
|
||||||
if (!$signals && extension_loaded('pcntl')) {
|
if (!$signals && extension_loaded('pcntl')) {
|
||||||
$pcntl = new ReflectionExtension('pcntl');
|
$pcntl = new ReflectionExtension('pcntl');
|
||||||
@@ -86,7 +82,7 @@ class SignalHandler
|
|||||||
|
|
||||||
$level = isset($this->signalLevelMap[$signo]) ? $this->signalLevelMap[$signo] : LogLevel::CRITICAL;
|
$level = isset($this->signalLevelMap[$signo]) ? $this->signalLevelMap[$signo] : LogLevel::CRITICAL;
|
||||||
$signal = isset($signals[$signo]) ? $signals[$signo] : $signo;
|
$signal = isset($signals[$signo]) ? $signals[$signo] : $signo;
|
||||||
$context = isset($siginfo) ? $siginfo : array();
|
$context = isset($siginfo) ? $siginfo : [];
|
||||||
$this->logger->log($level, sprintf('Program received signal %s', $signal), $context);
|
$this->logger->log($level, sprintf('Program received signal %s', $signal), $context);
|
||||||
|
|
||||||
if (!isset($this->previousSignalHandler[$signo])) {
|
if (!isset($this->previousSignalHandler[$signo])) {
|
||||||
@@ -95,21 +91,18 @@ class SignalHandler
|
|||||||
|
|
||||||
if ($this->previousSignalHandler[$signo] === true || $this->previousSignalHandler[$signo] === SIG_DFL) {
|
if ($this->previousSignalHandler[$signo] === true || $this->previousSignalHandler[$signo] === SIG_DFL) {
|
||||||
if (extension_loaded('pcntl') && function_exists('pcntl_signal') && function_exists('pcntl_sigprocmask') && function_exists('pcntl_signal_dispatch')
|
if (extension_loaded('pcntl') && function_exists('pcntl_signal') && function_exists('pcntl_sigprocmask') && function_exists('pcntl_signal_dispatch')
|
||||||
&& extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')) {
|
&& extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')
|
||||||
$restartSyscalls = isset($this->restartSyscalls[$signo]) ? $this->restartSyscalls[$signo] : true;
|
) {
|
||||||
pcntl_signal($signo, SIG_DFL, $restartSyscalls);
|
$restartSyscalls = isset($this->restartSyscalls[$signo]) ? $this->restartSyscalls[$signo] : true;
|
||||||
pcntl_sigprocmask(SIG_UNBLOCK, array($signo), $oldset);
|
pcntl_signal($signo, SIG_DFL, $restartSyscalls);
|
||||||
posix_kill(posix_getpid(), $signo);
|
pcntl_sigprocmask(SIG_UNBLOCK, [$signo], $oldset);
|
||||||
pcntl_signal_dispatch();
|
posix_kill(posix_getpid(), $signo);
|
||||||
pcntl_sigprocmask(SIG_SETMASK, $oldset);
|
pcntl_signal_dispatch();
|
||||||
pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
|
pcntl_sigprocmask(SIG_SETMASK, $oldset);
|
||||||
}
|
pcntl_signal($signo, [$this, 'handleSignal'], $restartSyscalls);
|
||||||
} elseif (is_callable($this->previousSignalHandler[$signo])) {
|
|
||||||
if (PHP_VERSION_ID >= 70100) {
|
|
||||||
$this->previousSignalHandler[$signo]($signo, $siginfo);
|
|
||||||
} else {
|
|
||||||
$this->previousSignalHandler[$signo]($signo);
|
|
||||||
}
|
}
|
||||||
|
} elseif (is_callable($this->previousSignalHandler[$signo])) {
|
||||||
|
$this->previousSignalHandler[$signo]($signo, $siginfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user