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. /// - Returns: `true` if at least one row was deleted.
@discardableResult func dnsLogsDeleteOlderThan(days: Int) throws -> Bool { @discardableResult func dnsLogsDeleteOlderThan(days: Int) throws -> Bool {
guard days > 0 else { return false } guard days > 0 else { return false }
return try self.run(sql: "DELETE FROM cache WHERE ts < strftime('%s', 'now', ?);", func delFrom(_ table: String) throws -> Bool {
bind: [BindText("-\(days) days")]) { return try self.run(sql: "DELETE FROM \(table) WHERE ts < strftime('%s', 'now', ?);",
try ifStep($0, SQLITE_DONE) bind: [BindText("-\(days) days")]) {
return numberOfChanges > 0 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 { DispatchQueue.global().async {
defer { sync.continue() } defer { sync.continue() }
QLog.Info("Auto-delete logs") QLog.Info("Auto-delete logs")
guard let success = try? AppDB?.dnsLogsDeleteOlderThan(days: days), success else { do {
return // nothing changed if try AppDB!.dnsLogsDeleteOlderThan(days: days) {
sync.needsReloadDB()
}
} catch {
QLog.Warning("Couldn't auto-delete logs, \(error)")
} }
sync.needsReloadDB()
} }
} }
} }