Signal-iOS/SignalMessaging/utils/OWSOrphanDataCleaner.swift
Evan Hahn d132e70778 Add more logging for orphan data cleaner, Swiftify
[Some users report][0] that data isn't being properly deleted. This adds
some additional logging to `OWSOrphanDataCleaner` (and converts some of
it to Swift while we're at it) which may help us fix the bug.

There are some small behavior changes here, but they should be minor:

- We now only open _one_ transaction when checking whether we should
  do a cleanup, instead of two.
- Some log messages were changed
- We now handle the case where the "last cleaned date" is unexpectedly
  missing

[0]: https://github.com/signalapp/Signal-iOS/issues/4916#issuecomment-1248234244
2022-11-30 15:40:13 -06:00

79 lines
2.8 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
extension OWSOrphanDataCleaner {
@objc
static func auditOnLaunchIfNecessary() {
AssertIsOnMainThread()
guard shouldAuditWithSneakyTransaction() else { return }
// If we want to be cautious, we can disable orphan deletion using
// flag - the cleanup will just be a dry run with logging.
let shouldCleanUp = true
auditAndCleanup(shouldCleanUp)
}
private static func shouldAuditWithSneakyTransaction() -> Bool {
guard CurrentAppContext().isMainApp else {
Logger.info("Orphan data audit skipped because we're not the main app")
return false
}
guard !CurrentAppContext().isRunningTests else {
Logger.info("Orphan data audit skipped because we're running tests")
return false
}
let kvs = keyValueStore()
let currentAppVersion = appVersion.currentAppReleaseVersion
return databaseStorage.read { transaction -> Bool in
guard TSAccountManager.shared.isRegistered(transaction: transaction) else {
Logger.info("Orphan data audit skipped because we're not registered")
return false
}
let lastCleaningVersion = kvs.getString(
OWSOrphanDataCleaner_LastCleaningVersionKey,
transaction: transaction
)
guard let lastCleaningVersion else {
Logger.info("Performing orphan data cleanup because we've never done it")
return true
}
guard lastCleaningVersion == currentAppVersion else {
Logger.info("Performing orphan data cleanup because we're on a different app version (\(currentAppVersion)")
return true
}
let lastCleaningDate = kvs.getDate(
OWSOrphanDataCleaner_LastCleaningDateKey,
transaction: transaction
)
guard let lastCleaningDate else {
owsFailDebug("We have a \"last cleaned version\". Why don't we have a last cleaned date?")
Logger.info("Performing orphan data cleanup because we've never done it")
return true
}
#if DEBUG
let hasEnoughTimePassed = DateUtil.dateIsOlderThanToday
#else
let hasEnoughTimePassed = DateUtil.dateIsOlderThanOneWeek
#endif
if hasEnoughTimePassed(lastCleaningDate) {
Logger.info("Performing orphan data cleanup because enough time has passed")
return true
}
Logger.info("Orphan data audit skipped because no other checks succeeded")
return false
}
}
}