Use Cron for periodic profile refreshes

This commit is contained in:
Max Radermacher
2025-11-20 22:47:19 -06:00
committed by GitHub
parent 276aa5d8c2
commit 2cb9731a45
3 changed files with 24 additions and 14 deletions

View File

@@ -665,13 +665,19 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
}
}
appReadiness.runNowOrWhenAppDidBecomeReadyAsync {
StaleProfileFetcher(
db: DependenciesBridge.shared.db,
profileFetcher: SSKEnvironment.shared.profileFetcherRef,
tsAccountManager: DependenciesBridge.shared.tsAccountManager
).scheduleProfileFetches()
}
cron.schedulePeriodically(
uniqueKey: .fetchStaleProfiles,
approximateInterval: .day,
mustBeRegistered: true,
mustBeConnected: true,
operation: {
try await StaleProfileFetcher(
db: DependenciesBridge.shared.db,
profileFetcher: SSKEnvironment.shared.profileFetcherRef,
tsAccountManager: DependenciesBridge.shared.tsAccountManager,
).fetchSomeStaleProfiles()
},
)
let groupV2Updates = SSKEnvironment.shared.groupV2UpdatesRef
cron.schedulePeriodically(

View File

@@ -23,11 +23,8 @@ class StaleProfileFetcher {
self.tsAccountManager = tsAccountManager
}
func scheduleProfileFetches() {
func fetchSomeStaleProfiles() async throws(CancellationError) {
let staleServiceIds = db.read { tx -> [ServiceId] in
guard tsAccountManager.registrationState(tx: tx).isRegistered else {
return []
}
var staleServiceIds = [ServiceId]()
Self.enumerateMissingAndStaleUserProfiles(now: Date(), tx: tx) { userProfile in
switch userProfile.internalAddress {
@@ -42,9 +39,15 @@ class StaleProfileFetcher {
}
return staleServiceIds
}
Task { [profileFetcher] in
for serviceId in staleServiceIds.shuffled() {
_ = try? await profileFetcher.fetchProfile(for: serviceId, context: .init(isOpportunistic: true))
for serviceId in staleServiceIds.shuffled() {
do {
_ = try await profileFetcher.fetchProfile(for: serviceId, context: .init(isOpportunistic: true))
} catch let error as CancellationError {
throw error
} catch ProfileFetcherError.skippingOpportunisticFetch {
// We expect this to happen and can safely ignore it.
} catch {
Logger.warn("Couldn't fetch stale profile for \(serviceId): \(error)")
}
}
}

View File

@@ -68,6 +68,7 @@ public class Cron {
case fetchDevices
case fetchSenderCertificates
case fetchStaleGroup
case fetchStaleProfiles
case fetchSubscriptionConfig
}