Use Cron for periodic subscription config update
This commit is contained in:
parent
fcc2ca2fb1
commit
b4e7239fea
@ -132,6 +132,15 @@ public class AppEnvironment: NSObject {
|
||||
udManager: SSKEnvironment.shared.udManagerRef
|
||||
)
|
||||
|
||||
let subscriptionConfigManager = DependenciesBridge.shared.subscriptionConfigManager
|
||||
cron.schedulePeriodically(
|
||||
uniqueKey: .fetchSubscriptionConfig,
|
||||
approximateInterval: .day,
|
||||
mustBeRegistered: false,
|
||||
mustBeConnected: true,
|
||||
operation: { try await subscriptionConfigManager.refresh() },
|
||||
)
|
||||
|
||||
appReadiness.runNowOrWhenAppWillBecomeReady {
|
||||
self.badgeManager.startObservingChanges(in: DependenciesBridge.shared.databaseChangeObserver)
|
||||
self.appIconBadgeUpdater.startObserving()
|
||||
@ -159,7 +168,6 @@ public class AppEnvironment: NSObject {
|
||||
let threadStore = DependenciesBridge.shared.threadStore
|
||||
let tsAccountManager = DependenciesBridge.shared.tsAccountManager
|
||||
let storageServiceRecordIkmMigrator = DependenciesBridge.shared.storageServiceRecordIkmMigrator
|
||||
let subscriptionConfigManager = DependenciesBridge.shared.subscriptionConfigManager
|
||||
|
||||
let avatarDefaultColorStorageServiceMigrator = AvatarDefaultColorStorageServiceMigrator(
|
||||
db: db,
|
||||
@ -278,14 +286,6 @@ public class AppEnvironment: NSObject {
|
||||
await inactiveLinkedDeviceFinder.refreshLinkedDeviceStateIfNecessary()
|
||||
}
|
||||
|
||||
Task {
|
||||
do {
|
||||
try await subscriptionConfigManager.refreshIfNeeded()
|
||||
} catch {
|
||||
owsFailDebug("Failed to fetch subscription configuration in launch job! \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
Task {
|
||||
do {
|
||||
try await backupSubscriptionManager.redeemSubscriptionIfNecessary()
|
||||
|
||||
@ -66,6 +66,7 @@ public class Cron {
|
||||
case cleanUpMessageSendLog
|
||||
case cleanUpViewOnceMessages
|
||||
case fetchStaleGroup
|
||||
case fetchSubscriptionConfig
|
||||
}
|
||||
|
||||
init(
|
||||
|
||||
@ -30,21 +30,11 @@ public class SubscriptionConfigManager {
|
||||
self.networkManager = networkManager
|
||||
}
|
||||
|
||||
public func refreshIfNeeded() async throws {
|
||||
if
|
||||
let lastFetchDate = db.read(block: {
|
||||
kvStore.fetchValue(Date.self, forKey: StoreKeys.lastFetchDate, tx: $0)
|
||||
}),
|
||||
dateProvider().timeIntervalSince(lastFetchDate) < .day
|
||||
{
|
||||
// Refresh daily, although we'll use a cached value for longer.
|
||||
return
|
||||
}
|
||||
|
||||
_ = try await refresh()
|
||||
public func refresh() async throws {
|
||||
_ = try await _refresh()
|
||||
}
|
||||
|
||||
private func refresh() async throws -> SubscriptionConfig {
|
||||
private func _refresh() async throws -> SubscriptionConfig {
|
||||
var request = TSRequest(
|
||||
url: URL(string: "v1/subscription/configuration")!,
|
||||
method: "GET",
|
||||
@ -88,7 +78,7 @@ public class SubscriptionConfigManager {
|
||||
return donationConfig
|
||||
}
|
||||
|
||||
return try await refresh().donation
|
||||
return try await _refresh().donation
|
||||
}
|
||||
|
||||
// MARK: Backups
|
||||
@ -103,7 +93,7 @@ public class SubscriptionConfigManager {
|
||||
return backupConfig
|
||||
}
|
||||
|
||||
return try await refresh().backup
|
||||
return try await _refresh().backup
|
||||
}
|
||||
|
||||
/// Returns a recently-fetched-and-cached `BackupSubscriptionConfiguration`
|
||||
|
||||
Loading…
Reference in New Issue
Block a user