1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Handle hangup signal

This commit is contained in:
J. King 2021-07-04 22:04:05 -04:00
parent 88fe3e76cb
commit 37c58e186a
4 changed files with 28 additions and 5 deletions

View file

@ -25,8 +25,7 @@ if (\PHP_SAPI === "cli") {
exit($exitStatus); exit($exitStatus);
} else { } else {
// load configuration // load configuration
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf; Arsse::bootstrap();
Arsse::load($conf);
// handle Web requests // handle Web requests
$emitter = new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter; $emitter = new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
$response = (new REST)->dispatch(); $response = (new REST)->dispatch();

View file

@ -29,6 +29,12 @@ class Arsse {
/** @var User */ /** @var User */
public static $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 { public static function load(Conf $conf): void {
static::$obj = static::$obj ?? new Factory; static::$obj = static::$obj ?? new Factory;
static::$lang = static::$lang ?? new Lang; static::$lang = static::$lang ?? new Lang;

View file

@ -61,8 +61,7 @@ USAGE_TEXT;
/** @codeCoverageIgnore */ /** @codeCoverageIgnore */
protected function loadConf(): bool { protected function loadConf(): bool {
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf; Arsse::bootstrap();
Arsse::load($conf);
return true; return true;
} }

View file

@ -17,6 +17,7 @@ class Service {
/** @var Service\Driver */ /** @var Service\Driver */
protected $drv; protected $drv;
protected $loop = false; protected $loop = false;
protected $reload = false;
public function __construct() { public function __construct() {
$driver = Arsse::$conf->serviceDriver; $driver = Arsse::$conf->serviceDriver;
@ -44,6 +45,9 @@ class Service {
do { do {
sleep((int) max(0, $t->getTimestamp() - time())); sleep((int) max(0, $t->getTimestamp() - time()));
pcntl_signal_dispatch(); pcntl_signal_dispatch();
if ($this->hangup) {
$this->reload();
}
} while ($this->loop && $t->getTimestamp() > time()); } while ($this->loop && $t->getTimestamp() > time());
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
@ -51,6 +55,13 @@ class Service {
return $t; 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 { public function checkIn(): bool {
return Arsse::$db->metaSet("service_last_checkin", time(), "datetime"); return Arsse::$db->metaSet("service_last_checkin", time(), "datetime");
} }
@ -100,6 +111,7 @@ class Service {
foreach ([\SIGABRT, \SIGINT, \SIGTERM] as $sig) { foreach ([\SIGABRT, \SIGINT, \SIGTERM] as $sig) {
pcntl_signal($sig, [$this, "sigTerm"]); pcntl_signal($sig, [$this, "sigTerm"]);
} }
pcntl_signal(\SIGHUP, [$this, "sigHup"]);
} }
} }
@ -109,4 +121,11 @@ class Service {
protected function sigTerm(int $signo): void { protected function sigTerm(int $signo): void {
$this->loop = false; $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;
}
} }