Fix: Auto-delete logs did not clear heap

This commit is contained in:
relikd
2020-08-24 00:56:14 +02:00
parent 908a909c87
commit 8855ae754a
2 changed files with 15 additions and 7 deletions

View File

@@ -48,11 +48,16 @@ extension SQLiteDatabase {
/// - Returns: `true` if at least one row was deleted.
@discardableResult func dnsLogsDeleteOlderThan(days: Int) throws -> Bool {
guard days > 0 else { return false }
return try self.run(sql: "DELETE FROM cache WHERE ts < strftime('%s', 'now', ?);",
bind: [BindText("-\(days) days")]) {
try ifStep($0, SQLITE_DONE)
return numberOfChanges > 0
func delFrom(_ table: String) throws -> Bool {
return try self.run(sql: "DELETE FROM \(table) WHERE ts < strftime('%s', 'now', ?);",
bind: [BindText("-\(days) days")]) {
try ifStep($0, SQLITE_DONE)
return numberOfChanges > 0
}
}
let didDelHeap = try delFrom("heap")
let didDelCache = try delFrom("cache")
return didDelHeap || didDelCache
}
}

View File

@@ -34,10 +34,13 @@ struct TheGreatDestroyer {
DispatchQueue.global().async {
defer { sync.continue() }
QLog.Info("Auto-delete logs")
guard let success = try? AppDB?.dnsLogsDeleteOlderThan(days: days), success else {
return // nothing changed
do {
if try AppDB!.dnsLogsDeleteOlderThan(days: days) {
sync.needsReloadDB()
}
} catch {
QLog.Warning("Couldn't auto-delete logs, \(error)")
}
sync.needsReloadDB()
}
}
}