1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 07:24:53 +00:00
Arsse/lib/Service.php

102 lines
3.1 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 {
2018-10-26 18:58:04 +00:00
/** @var Service\Driver */
protected $drv;
/** @var \DateInterval */
protected $interval;
2017-08-29 14:50:31 +00:00
public static function driverList(): array {
$sep = \DIRECTORY_SEPARATOR;
$path = __DIR__.$sep."Service".$sep;
$classes = [];
2017-08-29 14:50:31 +00:00
foreach (glob($path."*".$sep."Driver.php") as $file) {
$name = basename(dirname($file));
$class = NS_BASE."User\\$name\\Driver";
$classes[$class] = $class::driverName();
}
return $classes;
}
2018-10-26 18:58:04 +00:00
public static function interval(): \DateInterval {
2017-08-29 14:50:31 +00:00
try {
return new \DateInterval(Arsse::$conf->serviceFrequency);
2017-08-29 14:50:31 +00:00
} catch (\Exception $e) {
return new \DateInterval("PT2M");
}
2017-07-16 18:55:37 +00:00
}
2017-08-29 14:50:31 +00:00
public function __construct() {
$driver = Arsse::$conf->serviceDriver;
$this->drv = new $driver();
2017-07-16 18:55:37 +00:00
$this->interval = static::interval();
}
2017-08-29 14:50:31 +00:00
public function watch(bool $loop = true): \DateTimeInterface {
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);
$this->drv->exec();
$this->drv->clean();
unset($list);
}
2017-08-18 02:36:15 +00:00
static::cleanupPost();
$t->add($this->interval);
2017-08-29 14:50:31 +00:00
if ($loop) {
do {
@time_sleep_until($t->getTimestamp());
2017-08-29 14:50:31 +00:00
} while ($t->getTimestamp() > time());
}
2017-08-29 14:50:31 +00:00
} while ($loop);
return $t;
2017-07-16 18:55:37 +00:00
}
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 = static::interval();
// subtract twice the checking interval from the current time to the earliest acceptable check-in time
$limit = new \DateTime();
$limit->sub($int);
$limit->sub($int);
// return whether the check-in time is within the acceptable limit
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();
// 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 {
2017-08-18 02:36:15 +00:00
// delete old articles, according to configured threasholds
return Arsse::$db->articleCleanup();
}
2017-08-29 14:50:31 +00:00
}