|
|
|
@ -7,7 +7,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
import Foundation |
|
|
|
import Foundation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protocol SomeCallCollection { |
|
|
|
protocol SomeCallCollection { |
|
|
|
|
|
|
|
|
|
|
|
func findCallById(_ id: String) async -> (any SomeCall)? |
|
|
|
func findCallById(_ id: String) async -> (any SomeCall)? |
|
|
|
@ -32,7 +31,7 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
fileprivate var _attemptLoops: Int = 0 |
|
|
|
fileprivate var _attemptLoops: Int = 0 |
|
|
|
|
|
|
|
|
|
|
|
/// Indicates if the collection is currently retrying ApiCalls |
|
|
|
/// Indicates if the collection is currently retrying ApiCalls |
|
|
|
fileprivate var _isRetryingCalls: Bool = false |
|
|
|
fileprivate var _isRescheduling: Bool = false |
|
|
|
|
|
|
|
|
|
|
|
/// The task of waiting and executing ApiCalls |
|
|
|
/// The task of waiting and executing ApiCalls |
|
|
|
fileprivate var _reschedulingTask: Task<Void, any Error>? = nil |
|
|
|
fileprivate var _reschedulingTask: Task<Void, any Error>? = nil |
|
|
|
@ -67,7 +66,7 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
if FileManager.default.fileExists(atPath: fileURL.path()) { |
|
|
|
if FileManager.default.fileExists(atPath: fileURL.path()) { |
|
|
|
let jsonString: String = try FileUtils.readFile(fileURL: fileURL) |
|
|
|
let jsonString: String = try FileUtils.readFile(fileURL: fileURL) |
|
|
|
let decoded: [ApiCall<T>] = try jsonString.decodeArray() ?? [] |
|
|
|
let decoded: [ApiCall<T>] = try jsonString.decodeArray() ?? [] |
|
|
|
// Logger.log("loaded \(fileURL.lastPathComponent) with \(decoded.count) items") |
|
|
|
// Logger.log("loaded \(fileURL.lastPathComponent) with \(decoded.count) items") |
|
|
|
self.items = decoded |
|
|
|
self.items = decoded |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -76,14 +75,14 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
fileprivate func _write() { |
|
|
|
fileprivate func _write() { |
|
|
|
let fileName = ApiCall<T>.fileName() |
|
|
|
let fileName = ApiCall<T>.fileName() |
|
|
|
DispatchQueue(label: "lestorage.queue.write", qos: .utility).asyncAndWait { |
|
|
|
DispatchQueue(label: "lestorage.queue.write", qos: .utility).asyncAndWait { |
|
|
|
// Logger.log("Start write to \(fileName)...") |
|
|
|
// Logger.log("Start write to \(fileName)...") |
|
|
|
do { |
|
|
|
do { |
|
|
|
let jsonString: String = try self.items.jsonString() |
|
|
|
let jsonString: String = try self.items.jsonString() |
|
|
|
try T.writeToStorageDirectory(content: jsonString, fileName: fileName) |
|
|
|
try T.writeToStorageDirectory(content: jsonString, fileName: fileName) |
|
|
|
} catch { |
|
|
|
} catch { |
|
|
|
Logger.error(error) |
|
|
|
Logger.error(error) |
|
|
|
} |
|
|
|
} |
|
|
|
// Logger.log("End write") |
|
|
|
// Logger.log("End write") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -100,6 +99,7 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
/// Deletes an API call by [id] |
|
|
|
/// Deletes an API call by [id] |
|
|
|
func deleteById(_ id: String) { |
|
|
|
func deleteById(_ id: String) { |
|
|
|
self.items.removeAll(where: { $0.id == id }) |
|
|
|
self.items.removeAll(where: { $0.id == id }) |
|
|
|
|
|
|
|
Logger.log("\(T.resourceName()) > Delete by id, count after deletion = \(self.items.count)") |
|
|
|
self._hasChanged = true |
|
|
|
self._hasChanged = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -136,25 +136,41 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _wait() async { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let delay = pow(2, self._attemptLoops) |
|
|
|
|
|
|
|
let seconds = NSDecimalNumber(decimal: delay).intValue |
|
|
|
|
|
|
|
Logger.log("\(T.resourceName()): wait for \(seconds) sec") |
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
try await Task.sleep(until: .now + .seconds(seconds)) |
|
|
|
|
|
|
|
} catch { |
|
|
|
|
|
|
|
Logger.error(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Reschedule API calls if necessary |
|
|
|
|
|
|
|
func rescheduleApiCallsIfNecessary() { |
|
|
|
|
|
|
|
Task { |
|
|
|
|
|
|
|
await self._rescheduleApiCalls() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Reschedule the execution of API calls |
|
|
|
/// Reschedule the execution of API calls |
|
|
|
fileprivate func _rescheduleApiCalls() { |
|
|
|
fileprivate func _rescheduleApiCalls() async { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
guard !self._isRescheduling else { return } |
|
|
|
|
|
|
|
self._isRescheduling = true |
|
|
|
|
|
|
|
|
|
|
|
guard self.items.isNotEmpty else { |
|
|
|
guard self.items.isNotEmpty else { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self._isRetryingCalls = true |
|
|
|
|
|
|
|
self._attemptLoops += 1 |
|
|
|
self._attemptLoops += 1 |
|
|
|
|
|
|
|
|
|
|
|
self._reschedulingTask = Task { |
|
|
|
await self._wait() |
|
|
|
|
|
|
|
|
|
|
|
let delay = pow(2, self._attemptLoops) |
|
|
|
|
|
|
|
let seconds = NSDecimalNumber(decimal: delay).intValue |
|
|
|
|
|
|
|
Logger.log("\(T.resourceName()): wait for \(seconds) sec") |
|
|
|
|
|
|
|
try await Task.sleep(until: .now + .seconds(seconds)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let apiCallsCopy = self.items |
|
|
|
let apiCallsCopy = self.items |
|
|
|
for apiCall in apiCallsCopy { |
|
|
|
for (index, apiCall) in apiCallsCopy.enumerated() { |
|
|
|
apiCall.attemptsCount += 1 |
|
|
|
apiCall.attemptsCount += 1 |
|
|
|
apiCall.lastAttemptDate = Date() |
|
|
|
apiCall.lastAttemptDate = Date() |
|
|
|
|
|
|
|
|
|
|
|
@ -165,16 +181,12 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self._hasChanged = true |
|
|
|
self._isRescheduling = false |
|
|
|
|
|
|
|
if self.items.isNotEmpty { |
|
|
|
if self.items.isEmpty { |
|
|
|
await self._rescheduleApiCalls() |
|
|
|
self._isRetryingCalls = false |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
self._rescheduleApiCalls() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Synchronization |
|
|
|
// MARK: - Synchronization |
|
|
|
|
|
|
|
|
|
|
|
@ -213,13 +225,6 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
self.addOrUpdate(apiCall) |
|
|
|
self.addOrUpdate(apiCall) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Reschedule API calls if necessary |
|
|
|
|
|
|
|
func rescheduleApiCallsIfNecessary() { |
|
|
|
|
|
|
|
if !self._isRetryingCalls { |
|
|
|
|
|
|
|
self._rescheduleApiCalls() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Sends an insert api call for the provided [instance] |
|
|
|
/// Sends an insert api call for the provided [instance] |
|
|
|
func sendInsertion(_ instance: T) async throws -> T? { |
|
|
|
func sendInsertion(_ instance: T) async throws -> T? { |
|
|
|
do { |
|
|
|
do { |
|
|
|
|