1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00
Arsse/lib/Service.php

132 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse;
2017-08-29 14:50:31 +00:00
use JKingWeb\Arsse\Misc\Date;
class Service {
2020-03-01 23:32:01 +00:00
public const DRIVER_NAMES = [
'serial' => \JKingWeb\Arsse\Service\Serial\Driver::class,
'subprocess' => \JKingWeb\Arsse\Service\Subprocess\Driver::class,
];
2018-10-26 18:58:04 +00:00
/** @var Service\Driver */
protected $drv;
protected $loop = false;
2021-07-05 02:04:05 +00:00
protected $reload = false;
2017-08-29 14:50:31 +00:00
public function __construct() {
$driver = Arsse::$conf->serviceDriver;
$this->drv = new $driver();
}
2017-08-29 14:50:31 +00:00
public function watch(bool $loop = true): \DateTimeInterface {
$this->loop = $loop;
$this->signalInit();
2017-07-16 18:55:37 +00:00
$t = new \DateTime();
do {
$this->checkIn();
static::cleanupPre();
$list = Arsse::$db->feedListStale();
2017-08-29 14:50:31 +00:00
if ($list) {
$this->drv->queue(...$list);
2019-10-19 22:51:01 +00:00
unset($list);
$this->drv->exec();
$this->drv->clean();
}
2017-08-18 02:36:15 +00:00
static::cleanupPost();
2019-10-19 22:51:01 +00:00
$t->add(Arsse::$conf->serviceFrequency);
2019-10-19 16:14:13 +00:00
// @codeCoverageIgnoreStart
if ($this->loop) {
do {
sleep((int) max(0, $t->getTimestamp() - time()));
2021-07-05 02:04:05 +00:00
pcntl_signal_dispatch();
if ($this->hangup) {
$this->reload();
}
} while ($this->loop && $t->getTimestamp() > time());
}
2019-10-19 16:14:13 +00:00
// @codeCoverageIgnoreEnd
} while ($this->loop);
return $t;
2017-07-16 18:55:37 +00:00
}
2021-07-05 02:04:05 +00:00
public function reload(): void {
$this->reload = false;
unset(Arsse::$user, Arsse::$db, Arsse::$conf, Arsse::$lang, Arsse::$obj, $this->drv);
Arsse::bootstrap();
$this->__construct();
}
2017-08-29 14:50:31 +00:00
public function checkIn(): bool {
return Arsse::$db->metaSet("service_last_checkin", time(), "datetime");
2017-07-16 18:55:37 +00:00
}
2017-08-29 14:50:31 +00:00
public static function hasCheckedIn(): bool {
$checkin = Arsse::$db->metaGet("service_last_checkin");
2017-07-16 18:55:37 +00:00
// if the service has never checked in, return false
2017-08-29 14:50:31 +00:00
if (!$checkin) {
2017-07-21 02:40:09 +00:00
return false;
}
2017-07-16 18:55:37 +00:00
// convert the check-in timestamp to a DateTime instance
$checkin = Date::normalize($checkin, "sql");
2017-07-16 18:55:37 +00:00
// get the checking interval
$int = Arsse::$conf->serviceFrequency;
// subtract twice the checking interval from the current time to yield the earliest acceptable check-in time
2017-07-16 18:55:37 +00:00
$limit = new \DateTime();
$limit->sub($int);
$limit->sub($int);
// return whether the check-in time is within the acceptable limit
2020-03-01 20:16:50 +00:00
return $checkin >= $limit;
2017-07-16 18:55:37 +00:00
}
2017-08-29 14:50:31 +00:00
public static function cleanupPre(): bool {
// mark unsubscribed feeds as orphaned and delete orphaned feeds that are beyond their retention period
Arsse::$db->feedCleanup();
2020-11-06 22:06:01 +00:00
// do the same for icons
Arsse::$db->iconCleanup();
// delete expired log-in sessions
Arsse::$db->sessionCleanup();
return true;
2017-07-16 18:55:37 +00:00
}
2017-08-29 14:50:31 +00:00
public static function cleanupPost(): bool {
2019-07-26 13:37:51 +00:00
// delete old articles, according to configured thresholds
$deleted = Arsse::$db->articleCleanup();
// if any articles were deleted, perform database maintenance
if ($deleted) {
Arsse::$db->driverMaintenance();
}
return true;
}
protected function signalInit(): void {
if (function_exists("pcntl_async_signals") && function_exists("pcntl_signal")) {
// receive asynchronous signals if supported
pcntl_async_signals(true);
foreach ([\SIGABRT, \SIGINT, \SIGTERM] as $sig) {
pcntl_signal($sig, [$this, "sigTerm"]);
}
2021-07-05 02:04:05 +00:00
pcntl_signal(\SIGHUP, [$this, "sigHup"]);
}
}
2021-06-06 23:44:36 +00:00
/** Changes the condition for the service loop upon receiving a termination signal
*
* @codeCoverageIgnore */
protected function sigTerm(int $signo): void {
$this->loop = false;
}
2021-07-05 02:04:05 +00:00
/** Changes the condition for the service loop upon receiving a hangup signal
*
* @codeCoverageIgnore */
protected function sigHup(int $signo): void {
$this->hangup = true;
}
2017-08-29 14:50:31 +00:00
}