|
@ -3,6 +3,7 @@ declare(strict_types=1); |
|
|
namespace JKingWeb\Arsse; |
|
|
namespace JKingWeb\Arsse; |
|
|
|
|
|
|
|
|
class Service { |
|
|
class Service { |
|
|
|
|
|
use Misc\DateFormatter; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @var Service\Driver |
|
|
* @var Service\Driver |
|
@ -13,30 +14,63 @@ class Service { |
|
|
*/ |
|
|
*/ |
|
|
protected $interval; |
|
|
protected $interval; |
|
|
|
|
|
|
|
|
|
|
|
protected static function interval(): \DateInterval { |
|
|
|
|
|
return new \DateInterval(Data::$conf->serviceFrequency); // FIXME: this needs to fall back in case of incorrect input |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function __construct() { |
|
|
function __construct() { |
|
|
$driver = Data::$conf->serviceDriver; |
|
|
$driver = Data::$conf->serviceDriver; |
|
|
$this->drv = new $driver(); |
|
|
$this->drv = new $driver(); |
|
|
$this->interval = new \DateInterval(Data::$conf->serviceFrequency); // FIXME: this needs to fall back in case of incorrect input |
|
|
$this->interval = static::interval(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function watch() { |
|
|
function watch(bool $loop = true) { |
|
|
while(true) { |
|
|
$t = new \DateTime(); |
|
|
$t = new \DateTime(); |
|
|
do { |
|
|
|
|
|
$this->checkIn(); |
|
|
|
|
|
static::cleanupPre(); |
|
|
$list = Data::$db->feedListStale(); |
|
|
$list = Data::$db->feedListStale(); |
|
|
if($list) { |
|
|
if($list) { |
|
|
echo date("H:i:s")." Updating feeds ".json_encode($list)."\n"; |
|
|
echo date("H:i:s")." Updating feeds ".json_encode($list)."\n"; |
|
|
// TODO: pre-cleanup |
|
|
|
|
|
$this->drv->queue(...$list); |
|
|
$this->drv->queue(...$list); |
|
|
$this->drv->exec(); |
|
|
$this->drv->exec(); |
|
|
$this->drv->clean(); |
|
|
$this->drv->clean(); |
|
|
// TODO: post-cleanup |
|
|
static::cleanupPost(); |
|
|
} else { |
|
|
|
|
|
echo date("H:i:s")." No feeds to update; sleeping\n"; |
|
|
|
|
|
} |
|
|
} |
|
|
$t->add($this->interval); |
|
|
$t->add($this->interval); |
|
|
do { |
|
|
do { |
|
|
@time_sleep_until($t->getTimestamp()); |
|
|
@time_sleep_until($t->getTimestamp()); |
|
|
} while($t->getTimestamp() > time()); |
|
|
} while($t->getTimestamp() > time()); |
|
|
} |
|
|
} while($loop); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function checkIn(): bool { |
|
|
|
|
|
return Data::$db->metaSet("service_last_checkin", time(), "datetime"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static function hasCheckedIn(): bool { |
|
|
|
|
|
$checkin = Data::$db->metaGet("service_last_checkin"); |
|
|
|
|
|
// if the service has never checked in, return false |
|
|
|
|
|
if(!$checkin) return false; |
|
|
|
|
|
// convert the check-in timestamp to a DateTime instance |
|
|
|
|
|
$checkin = static::dateNormalize($checkin, "sql"); |
|
|
|
|
|
// 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 less than the acceptable limit |
|
|
|
|
|
return ($checkin < $limit); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static function cleanupPre(): bool { |
|
|
|
|
|
// TODO: stub |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static function cleanupPost():bool { |
|
|
|
|
|
// TODO: stub |
|
|
|
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |