Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion SQLiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public static function createDb(string $path = ''): PDO
v TEXT NOT NULL
);
SQL);
$pdo->exec("INSERT OR IGNORE INTO kv(k, v) VALUES ('last_synchronization', '0');");
$pdo->exec("INSERT OR IGNORE INTO kv(k, v) VALUES ('last_export', '0');");
$pdo->exec("INSERT OR IGNORE INTO kv(k, v) VALUES ('last_import', '0');");
$pdo->exec("INSERT OR IGNORE INTO kv(k, v) VALUES ('last_import_fail_date', '0');");
$pdo->exec("INSERT OR IGNORE INTO kv(k, v) VALUES ('last_key_check', '0');");
$pdo->exec("INSERT OR IGNORE INTO kv(k, v) VALUES ('sync_in_process', '0');");

Expand Down
44 changes: 39 additions & 5 deletions SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function timeSinceLastSyncRequest(): int
{
$lastSyncUnixTime = (int)(
$this->pdo
->query("SELECT v FROM kv WHERE k = 'last_synchronization'")
->query("SELECT v FROM kv WHERE k = 'last_export'")
->fetchColumn() ?? 0
);
return (time() - $lastSyncUnixTime);
Expand All @@ -69,10 +69,23 @@ public function syncData(string $apiKey): void
try {
$this->declareAppVersion();
$this->cleanOldVisitorsData();
$this->updateListsAndAgents($apiKey);
if ($this->importRecentlyFailed() === false) {
try {
$this->updateListsAndAgents($apiKey);
$this->setLastImportDate();
} catch (Exception $e) {
$this->setLastImportFailDate();
$lastImportTs = (int)(
$this->pdo
->query("SELECT v FROM kv WHERE k = 'last_import'")
->fetchColumn() ?? 0
);
error_log('AntiCrawler failed to update filtering lists. Last import date: ' . date("Y-m-d H:i:s", $lastImportTs));
}
}
$this->uploadRequestsToDB($apiKey);

$this->setLastSyncDate();
$this->setLastExportDate();
} finally {
$this->removeSyncLock();
}
Expand Down Expand Up @@ -202,9 +215,30 @@ private function uploadRequestsToDB(string $apiKey): void
$this->pdo->exec("UPDATE requests SET sync_state = 'sent' WHERE sync_state = 'sending'");
}

private function setLastSyncDate()
private function setLastExportDate()
{
$this->pdo->exec("UPDATE kv SET v = " . time() . " WHERE k = 'last_export'");
}

private function setLastImportDate()
{
$this->pdo->exec("UPDATE kv SET v = " . time() . " WHERE k = 'last_import'");
}

private function setLastImportFailDate()
{
$this->pdo->exec("UPDATE kv SET v = " . time() . " WHERE k = 'last_synchronization'");
$this->pdo->exec("UPDATE kv SET v = " . time() . " WHERE k = 'last_import_fail_date'");
}

private function importRecentlyFailed(): bool
{
$lastImportFailTs = (int)(
$this->pdo
->query("SELECT v FROM kv WHERE k = 'last_import_fail_date'")
->fetchColumn() ?? 0
);

return (time() - $lastImportFailTs) < 30;
}

public function updateListsAndAgents(string $apiKey)
Expand Down