Browse Source

Improvements to Service

microsub
J. King 7 years ago
parent
commit
9f90756ede
  1. 10
      lib/Database.php
  2. 52
      lib/Service.php

10
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;
}

52
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;
}
}
Loading…
Cancel
Save