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