From 9f90756edecd9c86078a15b13000964f1e20a85d Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 16 Jul 2017 14:55:37 -0400 Subject: [PATCH] Improvements to Service --- lib/Database.php | 10 +++++----- lib/Service.php | 52 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index ee4a2520..f10eb550 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -85,19 +85,19 @@ class Database { return $this->db->begin(); } - public function settingGet(string $key) { + public function metaGet(string $key) { return $this->db->prepare("SELECT value from arsse_meta where key is ?", "str")->run($key)->getValue(); } - public function settingSet(string $key, string $value): bool { - $out = !$this->db->prepare("UPDATE arsse_meta set value = ? where key is ?", "str", "str")->run($value, $key)->changes(); + public function metaSet(string $key, string $value, string $type = "str"): bool { + $out = !$this->db->prepare("UPDATE arsse_meta set value = ? where key is ?", $type, "str")->run($value, $key)->changes(); if(!$out) { - $out = $this->db->prepare("INSERT INTO arsse_meta(key,value)", "str", "str")->run($key, $value)->changes(); + $out = $this->db->prepare("INSERT INTO arsse_meta(key,value)", "str", $type)->run($key, $value)->changes(); } return (bool) $out; } - public function settingRemove(string $key): bool { + public function metaRemove(string $key): bool { $this->db->prepare("DELETE from arsse_meta where key is ?", "str")->run($key); return true; } diff --git a/lib/Service.php b/lib/Service.php index 3cb838f5..f3fd0567 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse; class Service { + use Misc\DateFormatter; /** * @var Service\Driver @@ -13,30 +14,63 @@ class Service { */ 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() { $driver = Data::$conf->serviceDriver; $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() { - while(true) { - $t = new \DateTime(); + function watch(bool $loop = true) { + $t = new \DateTime(); + do { + $this->checkIn(); + static::cleanupPre(); $list = Data::$db->feedListStale(); if($list) { echo date("H:i:s")." Updating feeds ".json_encode($list)."\n"; - // TODO: pre-cleanup $this->drv->queue(...$list); $this->drv->exec(); $this->drv->clean(); - // TODO: post-cleanup - } else { - echo date("H:i:s")." No feeds to update; sleeping\n"; + static::cleanupPost(); } $t->add($this->interval); do { @time_sleep_until($t->getTimestamp()); } 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; } } \ No newline at end of file