Cross-platform signal handler

This commit is contained in:
Dylan K. Taylor 2021-10-14 15:03:11 +01:00
parent 321345fcc8
commit 34b1392598
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 91 additions and 1 deletions

View File

@ -98,6 +98,7 @@ use pocketmine\utils\NotCloneable;
use pocketmine\utils\NotSerializable;
use pocketmine\utils\Process;
use pocketmine\utils\Promise;
use pocketmine\utils\SignalHandler;
use pocketmine\utils\Terminal;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Utils;
@ -250,6 +251,8 @@ class Server{
/** @var Player[] */
private array $playerList = [];
private SignalHandler $signalHandler;
/**
* @var CommandSender[][]
* @phpstan-var array<string, array<int, CommandSender>>
@ -743,6 +746,11 @@ class Server{
$this->autoloader = $autoloader;
$this->logger = $logger;
$this->signalHandler = new SignalHandler(function() : void{
$this->logger->info("Received signal interrupt, stopping the server");
$this->shutdown();
});
try{
foreach([
$dataPath,
@ -1326,7 +1334,10 @@ class Server{
* Shuts the server down correctly
*/
public function shutdown() : void{
$this->isRunning = false;
if($this->isRunning){
$this->isRunning = false;
$this->signalHandler->unregister();
}
}
public function forceShutdown() : void{

View File

@ -0,0 +1,79 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\utils;
use function function_exists;
use function pcntl_signal;
use function sapi_windows_set_ctrl_handler;
use const PHP_WINDOWS_EVENT_CTRL_BREAK;
use const PHP_WINDOWS_EVENT_CTRL_C;
use const SIG_DFL;
use const SIGHUP;
use const SIGINT;
use const SIGTERM;
final class SignalHandler{
/** @phpstan-var (\Closure(int) : void)|null */
private ?\Closure $interruptCallback;
/**
* @phpstan-param \Closure() : void $interruptCallback
*/
public function __construct(\Closure $interruptCallback){
$this->interruptCallback = $interruptCallback;
if(function_exists('sapi_windows_set_ctrl_handler')){
sapi_windows_set_ctrl_handler($this->interruptCallback = function(int $signo) use ($interruptCallback) : void{
if($signo === PHP_WINDOWS_EVENT_CTRL_C || $signo === PHP_WINDOWS_EVENT_CTRL_BREAK){
$interruptCallback();
}
});
}elseif(function_exists('pcntl_signal')){
foreach([
SIGTERM,
SIGINT,
SIGHUP
] as $signal){
pcntl_signal($signal, $this->interruptCallback = fn(int $signo) => $interruptCallback());
}
pcntl_async_signals(true);
}else{
//no supported signal handlers :(
}
}
public function unregister() : void{
if(function_exists('sapi_windows_set_ctrl_handler')){
sapi_windows_set_ctrl_handler($this->interruptCallback, false);
}elseif(function_exists('pcntl_signal')){
foreach([
SIGTERM,
SIGINT,
SIGHUP
] as $signal){
pcntl_signal($signal, SIG_DFL);
}
}
}
}