Skip to content

Commit

Permalink
Handle hangup signal
Browse files Browse the repository at this point in the history
  • Loading branch information
JKingweb committed Jul 6, 2021
1 parent 88fe3e7 commit 37c58e1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
3 changes: 1 addition & 2 deletions arsse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
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();
Expand Down
6 changes: 6 additions & 0 deletions lib/Arsse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions lib/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ protected function command($args): string {

/** @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;
}

Expand Down
21 changes: 20 additions & 1 deletion lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Service {
/** @var Service\Driver */
protected $drv;
protected $loop = false;
protected $reload = false;

public function __construct() {
$driver = Arsse::$conf->serviceDriver;
Expand All @@ -43,14 +44,24 @@ public function watch(bool $loop = true): \DateTimeInterface {
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
} while ($this->loop);
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");
}
Expand Down Expand Up @@ -100,6 +111,7 @@ protected function signalInit(): void {
foreach ([\SIGABRT, \SIGINT, \SIGTERM] as $sig) {
pcntl_signal($sig, [$this, "sigTerm"]);
}
pcntl_signal(\SIGHUP, [$this, "sigHup"]);
}
}

Expand All @@ -109,4 +121,11 @@ protected function signalInit(): void {
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;
}
}

0 comments on commit 37c58e1

Please sign in to comment.