Просмотр исходного кода

replace timer in apsManager with background task to schedule regular database cleaning

polscm32 1 год назад
Родитель
Сommit
6ea546c69d

+ 2 - 0
FreeAPS/Resources/Info.plist

@@ -7,6 +7,7 @@
 	<key>BGTaskSchedulerPermittedIdentifiers</key>
 	<array>
 		<string>com.freeapsx.background-task.critical-event-log</string>
+		<string>com.openiaps.cleanup</string>
 	</array>
 	<key>CBBundleDisplayName</key>
 	<string>$(APP_DISPLAY_NAME)</string>
@@ -97,6 +98,7 @@
 	<array>
 		<string>bluetooth-central</string>
 		<string>bluetooth-peripheral</string>
+		<string>fetch</string>
 		<string>processing</string>
 	</array>
 	<key>UIFileSharingEnabled</key>

+ 0 - 85
FreeAPS/Sources/APS/APSManager.swift

@@ -79,10 +79,6 @@ final class BaseAPSManager: APSManager, Injectable {
         }
     }
 
-    private var cleanupTimer: Timer?
-    @Persisted(key: "lastHistoryCleanupDate") private var lastHistoryCleanupDate = Date.distantPast
-    @Persisted(key: "lastPurgeDate") private var lastPurgeDate = Date.distantPast
-
     let viewContext = CoreDataStack.shared.persistentContainer.viewContext
     let privateContext = CoreDataStack.shared.newTaskContext()
 
@@ -133,55 +129,6 @@ final class BaseAPSManager: APSManager, Injectable {
         isLooping
             .weakAssign(to: \.deviceDataManager.loopInProgress, on: self)
             .store(in: &lifetime)
-        startCleanupTimer()
-    }
-
-    private func startCleanupTimer() {
-        // Call the timer once every 12 hours to ensure that no clean gets missed
-        cleanupTimer = Timer.scheduledTimer(withTimeInterval: 12 * 60 * 60, repeats: true) { [weak self] _ in
-            self?.performCleanupIfNeeded()
-        }
-        RunLoop.current.add(cleanupTimer!, forMode: .common)
-    }
-
-    private func performCleanupIfNeeded() {
-        let now = Date()
-        let calendar = Calendar.current
-
-        // Check if last clean is longer than one day ago
-        if !calendar.isDateInToday(lastHistoryCleanupDate) {
-            // Perform daily cleanup
-            Task {
-                await CoreDataStack.shared.cleanupPersistentHistoryTokens(before: Date.oneWeekAgo)
-                // Update lastHistoryCleanupDate only if cleanup was successful
-                lastHistoryCleanupDate = now
-            }
-        }
-
-        // Check if last purge is longer than one week ago
-        if let lastPurge = calendar.date(byAdding: .day, value: 7, to: lastPurgeDate), now >= lastPurge {
-            // Perform weekly purge
-            Task {
-                do {
-                    try await purgeOldNSManagedObjects()
-                    // Update lastPurgeDate only if purge was successful
-                    lastPurgeDate = now
-                } catch {
-                    debugPrint("Failed to purge old managed objects: \(error.localizedDescription)")
-                }
-            }
-        }
-    }
-
-    private func purgeOldNSManagedObjects() async throws {
-        try await CoreDataStack.shared.batchDeleteOlderThan(GlucoseStored.self, dateKey: "date", days: 90)
-        try await CoreDataStack.shared.batchDeleteOlderThan(PumpEventStored.self, dateKey: "timestamp", days: 90)
-        try await CoreDataStack.shared.batchDeleteOlderThan(OrefDetermination.self, dateKey: "deliverAt", days: 90)
-        try await CoreDataStack.shared.batchDeleteOlderThan(OpenAPS_Battery.self, dateKey: "date", days: 90)
-        try await CoreDataStack.shared.batchDeleteOlderThan(CarbEntryStored.self, dateKey: "date", days: 90)
-        try await CoreDataStack.shared.batchDeleteOlderThan(Forecast.self, dateKey: "date", days: 90)
-
-        // TODO: - Purge Data of other (future) entities as well
     }
 
     private func subscribe() {
@@ -565,38 +512,6 @@ final class BaseAPSManager: APSManager, Injectable {
         }
     }
 
-//    func enactTempBasal(rate: Double, duration: TimeInterval) {
-//        if let error = verifyStatus() {
-//            processError(error)
-//            return
-//        }
-//
-//        guard let pump = pumpManager else { return }
-//
-//        // unable to do temp basal during manual temp basal 😁
-//        if isManualTempBasal {
-//            processError(APSError.manualBasalTemp(message: "Loop not possible during the manual basal temp"))
-//            return
-//        }
-//
-//        debug(.apsManager, "Enact temp basal \(rate) - \(duration)")
-//
-//        let roundedAmout = pump.roundToSupportedBasalRate(unitsPerHour: rate)
-//        pump.enactTempBasal(unitsPerHour: roundedAmout, for: duration) { error in
-//            if let error = error {
-//                debug(.apsManager, "Temp Basal failed with error: \(error.localizedDescription)")
-//                self.processError(APSError.pumpError(error))
-//            } else {
-//                debug(.apsManager, "Temp Basal succeeded")
-//                let temp = TempBasal(duration: Int(duration / 60), rate: Decimal(rate), temp: .absolute, timestamp: Date())
-//                self.storage.save(temp, as: OpenAPS.Monitor.tempBasal)
-//                if rate == 0, duration == 0 {
-//                    self.pumpHistoryStorage.saveCancelTempEvents()
-//                }
-//            }
-//        }
-//    }
-
     func dailyAutotune() -> AnyPublisher<Bool, Never> {
         guard settings.useAutotune else {
             return Just(false).eraseToAnyPublisher()

+ 16 - 0
FreeAPS/Sources/Application/FreeAPSApp.swift

@@ -1,4 +1,5 @@
 import ActivityKit
+import BackgroundTasks
 import CoreData
 import Foundation
 import SwiftUI
@@ -77,6 +78,21 @@ import Swinject
                 coreDataStack.save()
             }
         }
+        .backgroundTask(.appRefresh("com.openiaps.cleanup")) {
+            await scheduleDatabaseCleaning()
+            await cleanupOldData()
+        }
+    }
+
+    func scheduleDatabaseCleaning() {
+        let request = BGAppRefreshTaskRequest(identifier: "com.openiaps.cleanup")
+        request.earliestBeginDate = .now.addingTimeInterval(7 * 24 * 60 * 60) // 7 days
+        do {
+            try BGTaskScheduler.shared.submit(request)
+            debugPrint("Task scheduled successfully")
+        } catch {
+            debugPrint("Failed to schedule tasks")
+        }
     }
 
     private func cleanupOldData() {