From 37c58e186a55beaddf83b8910138f221b1ffbffd Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 4 Jul 2021 22:04:05 -0400 Subject: [PATCH] Handle hangup signal --- arsse.php | 3 +-- lib/Arsse.php | 6 ++++++ lib/CLI.php | 3 +-- lib/Service.php | 21 ++++++++++++++++++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/arsse.php b/arsse.php index 8b6d1fd..7e13cc3 100644 --- a/arsse.php +++ b/arsse.php @@ -25,8 +25,7 @@ if (\PHP_SAPI === "cli") { exit($exitStatus); } else { // load configuration - $conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf; - Arsse::load($conf); + Arsse::bootstrap(); // handle Web requests $emitter = new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter; $response = (new REST)->dispatch(); diff --git a/lib/Arsse.php b/lib/Arsse.php index 18f568f..ec0305b 100644 --- a/lib/Arsse.php +++ b/lib/Arsse.php @@ -29,6 +29,12 @@ class Arsse { /** @var User */ public static $user; + /** @codeCoverageIgnore */ + public static function bootstrap(): void { + $conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf; + static::load($conf); + } + public static function load(Conf $conf): void { static::$obj = static::$obj ?? new Factory; static::$lang = static::$lang ?? new Lang; diff --git a/lib/CLI.php b/lib/CLI.php index 3bb271e..9e9993e 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -61,8 +61,7 @@ USAGE_TEXT; /** @codeCoverageIgnore */ protected function loadConf(): bool { - $conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf; - Arsse::load($conf); + Arsse::bootstrap(); return true; } diff --git a/lib/Service.php b/lib/Service.php index af95edc..3ac3c4b 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -17,6 +17,7 @@ class Service { /** @var Service\Driver */ protected $drv; protected $loop = false; + protected $reload = false; public function __construct() { $driver = Arsse::$conf->serviceDriver; @@ -43,7 +44,10 @@ class Service { if ($this->loop) { do { sleep((int) max(0, $t->getTimestamp() - time())); - pcntl_signal_dispatch(); + pcntl_signal_dispatch(); + if ($this->hangup) { + $this->reload(); + } } while ($this->loop && $t->getTimestamp() > time()); } // @codeCoverageIgnoreEnd @@ -51,6 +55,13 @@ class Service { return $t; } + public function reload(): void { + $this->reload = false; + unset(Arsse::$user, Arsse::$db, Arsse::$conf, Arsse::$lang, Arsse::$obj, $this->drv); + Arsse::bootstrap(); + $this->__construct(); + } + public function checkIn(): bool { return Arsse::$db->metaSet("service_last_checkin", time(), "datetime"); } @@ -100,6 +111,7 @@ class Service { foreach ([\SIGABRT, \SIGINT, \SIGTERM] as $sig) { pcntl_signal($sig, [$this, "sigTerm"]); } + pcntl_signal(\SIGHUP, [$this, "sigHup"]); } } @@ -109,4 +121,11 @@ class Service { protected function sigTerm(int $signo): void { $this->loop = false; } + + /** Changes the condition for the service loop upon receiving a hangup signal + * + * @codeCoverageIgnore */ + protected function sigHup(int $signo): void { + $this->hangup = true; + } }