Stop incremental migrator when backgrounding app

This commit is contained in:
Harry 2024-08-14 11:58:18 -07:00 committed by GitHub
parent 258826b0ff
commit c3fb18bf78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View File

@ -100,6 +100,12 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
// Ensure that all windows have the correct frame.
WindowManager.shared.updateWindowFrames()
AppReadiness.runNowOrWhenMainAppDidBecomeReadyAsync {
self.incrementalMessageTSAttachmentMigrator?.runInMainAppBackgroundIfNeeded(
databaseStorage: self.databaseStorage
)
}
}
private let flushQueue = DispatchQueue(label: "org.signal.flush", qos: .utility)

View File

@ -125,6 +125,11 @@ extension IncrementalMessageTSAttachmentMigrator {
return
case .started:
// Don't _start_ in the main app, but continue making progress if we already started.
if isRunningInMainApp.get() {
return
}
Logger.info("Continuing migration in main app")
Task {
await runInMainAppBackground()
@ -135,8 +140,19 @@ extension IncrementalMessageTSAttachmentMigrator {
private func runInMainAppBackground() async {
var batchCount = 0
var didFinish = false
isRunningInMainApp.set(true)
defer {
isRunningInMainApp.set(false)
}
while !didFinish {
do {
guard CurrentAppContext().isMainAppAndActive else {
// If the main app goes into the background, we shouldn't be
// grabbing the sql write lock. Stop.
Logger.info("Stopping when backgrounding app")
return
}
// Add a small delay between each batch to avoid locking the db write queue.
try await Task.sleep(nanoseconds: 300 * NSEC_PER_MSEC)

View File

@ -12,6 +12,8 @@ public protocol IncrementalMessageTSAttachmentMigrator {
// Returns true if done.
func runNextBatch() async throws -> Bool
var isRunningInMainApp: AtomicBool { get }
}
public class IncrementalMessageTSAttachmentMigratorImpl: IncrementalMessageTSAttachmentMigrator {
@ -20,6 +22,8 @@ public class IncrementalMessageTSAttachmentMigratorImpl: IncrementalMessageTSAtt
private let databaseStorage: SDSDatabaseStorage
public let isRunningInMainApp = AtomicBool(false, lock: .init())
public init(databaseStorage: SDSDatabaseStorage) {
self.databaseStorage = databaseStorage
@ -100,6 +104,8 @@ public class IncrementalMessageTSAttachmentMigratorImpl: IncrementalMessageTSAtt
public class NoOpIncrementalMessageTSAttachmentMigrator: IncrementalMessageTSAttachmentMigrator {
public init() {}
public let isRunningInMainApp = AtomicBool(false, lock: .init())
public func runUntilFinished() async {}
// Returns true if done.
@ -114,6 +120,8 @@ public class IncrementalMessageTSAttachmentMigratorMock: IncrementalMessageTSAtt
public init() {}
public let isRunningInMainApp = AtomicBool(false, lock: .init())
public func runUntilFinished() async {}
// Returns true if done.