2017-07-15 20:44:06 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse;
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
2017-07-15 20:44:06 +00:00
|
|
|
|
|
|
|
class Service {
|
|
|
|
|
2017-07-17 11:47:57 +00:00
|
|
|
/** @var Service\Driver */
|
2017-07-15 20:44:06 +00:00
|
|
|
protected $drv;
|
2017-07-17 11:47:57 +00:00
|
|
|
/** @var \DateInterval */
|
2017-07-15 20:44:06 +00:00
|
|
|
protected $interval;
|
2017-07-18 20:38:23 +00:00
|
|
|
|
|
|
|
static public function driverList(): array {
|
|
|
|
$sep = \DIRECTORY_SEPARATOR;
|
|
|
|
$path = __DIR__.$sep."Service".$sep;
|
|
|
|
$classes = [];
|
|
|
|
foreach(glob($path."*".$sep."Driver.php") as $file) {
|
|
|
|
$name = basename(dirname($file));
|
|
|
|
$class = NS_BASE."User\\$name\\Driver";
|
|
|
|
$classes[$class] = $class::driverName();
|
|
|
|
}
|
|
|
|
return $classes;
|
|
|
|
}
|
2017-07-15 20:44:06 +00:00
|
|
|
|
2017-07-19 22:07:36 +00:00
|
|
|
public static function interval(): \DateInterval {
|
|
|
|
try{
|
|
|
|
return new \DateInterval(Arsse::$conf->serviceFrequency);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
return new \DateInterval("PT2M");
|
|
|
|
}
|
2017-07-16 18:55:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 20:44:06 +00:00
|
|
|
function __construct() {
|
2017-07-17 11:47:57 +00:00
|
|
|
$driver = Arsse::$conf->serviceDriver;
|
2017-07-15 20:44:06 +00:00
|
|
|
$this->drv = new $driver();
|
2017-07-16 18:55:37 +00:00
|
|
|
$this->interval = static::interval();
|
2017-07-15 20:44:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 22:07:36 +00:00
|
|
|
function watch(bool $loop = true): \DateTimeInterface {
|
2017-07-16 18:55:37 +00:00
|
|
|
$t = new \DateTime();
|
|
|
|
do {
|
|
|
|
$this->checkIn();
|
|
|
|
static::cleanupPre();
|
2017-07-17 11:47:57 +00:00
|
|
|
$list = Arsse::$db->feedListStale();
|
2017-07-15 20:44:06 +00:00
|
|
|
if($list) {
|
|
|
|
$this->drv->queue(...$list);
|
|
|
|
$this->drv->exec();
|
|
|
|
$this->drv->clean();
|
2017-07-16 18:55:37 +00:00
|
|
|
static::cleanupPost();
|
2017-07-18 20:38:23 +00:00
|
|
|
unset($list);
|
2017-07-15 20:44:06 +00:00
|
|
|
}
|
|
|
|
$t->add($this->interval);
|
2017-07-19 22:07:36 +00:00
|
|
|
if($loop) {
|
|
|
|
do {
|
|
|
|
@time_sleep_until($t->getTimestamp());
|
|
|
|
} while($t->getTimestamp() > time());
|
|
|
|
}
|
2017-07-16 18:55:37 +00:00
|
|
|
} while($loop);
|
2017-07-19 22:07:36 +00:00
|
|
|
return $t;
|
2017-07-16 18:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkIn(): bool {
|
2017-07-17 11:47:57 +00:00
|
|
|
return Arsse::$db->metaSet("service_last_checkin", time(), "datetime");
|
2017-07-16 18:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static function hasCheckedIn(): bool {
|
2017-07-17 11:47:57 +00:00
|
|
|
$checkin = Arsse::$db->metaGet("service_last_checkin");
|
2017-07-16 18:55:37 +00:00
|
|
|
// if the service has never checked in, return false
|
|
|
|
if(!$checkin) return false;
|
|
|
|
// convert the check-in timestamp to a DateTime instance
|
2017-07-17 11:47:57 +00:00
|
|
|
$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);
|
2017-07-19 22:07:36 +00:00
|
|
|
// return whether the check-in time is within the acceptable limit
|
|
|
|
return ($checkin >= $limit);
|
2017-07-16 18:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static function cleanupPre(): bool {
|
|
|
|
// TODO: stub
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function cleanupPost():bool {
|
|
|
|
// TODO: stub
|
|
|
|
return true;
|
2017-07-15 20:44:06 +00:00
|
|
|
}
|
|
|
|
}
|