Browse Source

Handle hangup signal

rpm
J. King 3 years ago
parent
commit
37c58e186a
  1. 3
      arsse.php
  2. 6
      lib/Arsse.php
  3. 3
      lib/CLI.php
  4. 21
      lib/Service.php

3
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();

6
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;

3
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;
}

21
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;
}
}

Loading…
Cancel
Save