From 87a7eed5dc8e101a0d1c3f0b6e83d8b7d637cfcb Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Thu, 7 Sep 2023 10:23:22 -0500 Subject: [PATCH] fix: reduce memory and cpu usage while running background queue (#379) --- .../Common/Background Queue/QueueRunner.swift | 98 +++++++++---- .../DeletePushNotificationQueueTaskData.swift | 16 +++ .../IdentifyProfileQueueTaskData.swift | 17 +++ ...egisterPushNotificationQueueTaskData.swift | 16 +++ .../Task Data/TrackEventQueueTaskData.swift | 17 +++ .../Background Queue/Type/QueueTaskType.swift | 7 +- .../Service/Request/MetricRequest.swift | 17 ++- .../AutoMockable.generated.swift | 132 ------------------ Sources/Common/hooks/HooksManager.swift | 5 - Sources/Common/hooks/ModuleHook.swift | 8 -- .../MessagingInApp/hooks/HookProvider.swift | 4 - .../Background Queue/QueueRunner.swift | 97 ------------- .../DeletePushNotificationQueueTaskData.swift | 11 -- .../IdentifyProfileQueueTaskData.swift | 12 -- ...egisterPushNotificationQueueTaskData.swift | 11 -- .../Task Data/TrackEventQueueTaskData.swift | 12 -- .../Background Queue/Type/QueueTaskType.swift | 10 -- Sources/Tracking/Service/Request/Device.swift | 17 ++- .../Request/RegisterDeviceRequest.swift | 8 +- .../AutoDependencyInjection.generated.swift | 13 -- .../hooks/TrackingModuleHookProvider.swift | 4 - .../extension/QueueStorageExtension.swift | 2 +- 22 files changed, 173 insertions(+), 361 deletions(-) create mode 100644 Sources/Common/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift create mode 100644 Sources/Common/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift create mode 100644 Sources/Common/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift create mode 100644 Sources/Common/Background Queue/Task Data/TrackEventQueueTaskData.swift rename Sources/{Tracking => Common}/Service/Request/MetricRequest.swift (53%) delete mode 100644 Sources/Tracking/Background Queue/QueueRunner.swift delete mode 100644 Sources/Tracking/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift delete mode 100644 Sources/Tracking/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift delete mode 100644 Sources/Tracking/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift delete mode 100644 Sources/Tracking/Background Queue/Task Data/TrackEventQueueTaskData.swift delete mode 100644 Sources/Tracking/Background Queue/Type/QueueTaskType.swift diff --git a/Sources/Common/Background Queue/QueueRunner.swift b/Sources/Common/Background Queue/QueueRunner.swift index 5990ad97e..8bdd27962 100644 --- a/Sources/Common/Background Queue/QueueRunner.swift +++ b/Sources/Common/Background Queue/QueueRunner.swift @@ -13,11 +13,6 @@ public protocol QueueRunner: AutoMockable { // sourcery: InjectRegister = "QueueRunner" public class CioQueueRunner: ApiSyncQueueRunner, QueueRunner { - private let hooks: HooksManager - // store currently running queue hook in memory so it doesn't get garbage collected. - // hook instance needs to call completion handler so hold strong reference - private var currentlyRunningHook: QueueRunnerHook? - init( jsonAdapter: JsonAdapter, logger: Logger, @@ -25,8 +20,6 @@ public class CioQueueRunner: ApiSyncQueueRunner, QueueRunner { hooksManager: HooksManager, sdkConfig: SdkConfig ) { - self.hooks = hooksManager - super.init( jsonAdapter: jsonAdapter, logger: logger, @@ -36,30 +29,20 @@ public class CioQueueRunner: ApiSyncQueueRunner, QueueRunner { } public func runTask(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { - if let queueTaskType = QueueTaskType(rawValue: task.type) { - switch queueTaskType { - case .trackDeliveryMetric: trackDeliveryMetric(task, onComplete: onComplete) - } - - return - } + guard let queueTaskType = QueueTaskType(rawValue: task.type) else { + // not being able to compose a QueueTaskType is unexpected. All types are expected to be handled by this runner. Log an error so we get notified of this event. + logger.error("task \(task.type) not handled by the queue runner.") - var hookHandled = false - - hooks.queueRunnerHooks.forEach { hook in - if hook.runTask(task, onComplete: { result in - self.currentlyRunningHook = nil - onComplete(result) - }) { - self.currentlyRunningHook = hook - hookHandled = true - } + return onComplete(.failure(.noRequestMade(nil))) } - if !hookHandled { - logger.error("task \(task.type) not handled by any module") - - onComplete(.failure(.noRequestMade(nil))) + switch queueTaskType { + case .trackDeliveryMetric: trackDeliveryMetric(task, onComplete: onComplete) + case .identifyProfile: identify(task, onComplete: onComplete) + case .trackEvent: track(task, onComplete: onComplete) + case .registerPushToken: registerPushToken(task, onComplete: onComplete) + case .deletePushToken: deletePushToken(task, onComplete: onComplete) + case .trackPushMetric: trackPushMetric(task, onComplete: onComplete) } } } @@ -79,4 +62,63 @@ private extension CioQueueRunner { performHttpRequest(endpoint: .trackDeliveryMetrics, requestBody: bodyData, onComplete: onComplete) } + + private func identify(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { + guard let taskData = getTaskData(task, type: IdentifyProfileQueueTaskData.self) else { + return onComplete(failureIfDontDecodeTaskData) + } + + performHttpRequest( + endpoint: .identifyCustomer(identifier: taskData.identifier), + requestBody: taskData.attributesJsonString?.data, + onComplete: onComplete + ) + } + + private func track(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { + guard let taskData = getTaskData(task, type: TrackEventQueueTaskData.self) else { + return onComplete(failureIfDontDecodeTaskData) + } + + performHttpRequest( + endpoint: .trackCustomerEvent(identifier: taskData.identifier), + requestBody: taskData.attributesJsonString.data, + onComplete: onComplete + ) + } + + private func registerPushToken(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { + guard let taskData = getTaskData(task, type: RegisterPushNotificationQueueTaskData.self) else { + return onComplete(failureIfDontDecodeTaskData) + } + + performHttpRequest( + endpoint: .registerDevice(identifier: taskData.profileIdentifier), + requestBody: taskData.attributesJsonString?.data, + onComplete: onComplete + ) + } + + private func deletePushToken(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { + guard let taskData = getTaskData(task, type: DeletePushNotificationQueueTaskData.self) else { + return onComplete(failureIfDontDecodeTaskData) + } + + performHttpRequest(endpoint: .deleteDevice( + identifier: taskData.profileIdentifier, + deviceToken: taskData.deviceToken + ), requestBody: nil, onComplete: onComplete) + } + + private func trackPushMetric(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { + guard let taskData = getTaskData(task, type: MetricRequest.self) else { + return onComplete(failureIfDontDecodeTaskData) + } + + guard let bodyData = jsonAdapter.toJson(taskData) else { + return + } + + performHttpRequest(endpoint: .pushMetrics, requestBody: bodyData, onComplete: onComplete) + } } diff --git a/Sources/Common/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift b/Sources/Common/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift new file mode 100644 index 000000000..235b0f137 --- /dev/null +++ b/Sources/Common/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift @@ -0,0 +1,16 @@ +import Foundation + +public struct DeletePushNotificationQueueTaskData: Codable { + public let profileIdentifier: String + public let deviceToken: String + + public init(profileIdentifier: String, deviceToken: String) { + self.profileIdentifier = profileIdentifier + self.deviceToken = deviceToken + } + + enum CodingKeys: String, CodingKey { + case profileIdentifier = "profile_identifier" + case deviceToken = "device_token" + } +} diff --git a/Sources/Common/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift b/Sources/Common/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift new file mode 100644 index 000000000..be1372c22 --- /dev/null +++ b/Sources/Common/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift @@ -0,0 +1,17 @@ +import Foundation + +public struct IdentifyProfileQueueTaskData: Codable { + public let identifier: String + /// JSON string: '{"foo": "bar"}' + public let attributesJsonString: String? + + public init(identifier: String, attributesJsonString: String?) { + self.identifier = identifier + self.attributesJsonString = attributesJsonString + } + + enum CodingKeys: String, CodingKey { + case identifier + case attributesJsonString = "attributes_json_string" + } +} diff --git a/Sources/Common/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift b/Sources/Common/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift new file mode 100644 index 000000000..4d0d936f7 --- /dev/null +++ b/Sources/Common/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift @@ -0,0 +1,16 @@ +import Foundation + +public struct RegisterPushNotificationQueueTaskData: Codable { + public let profileIdentifier: String + public let attributesJsonString: String? + + public init(profileIdentifier: String, attributesJsonString: String?) { + self.profileIdentifier = profileIdentifier + self.attributesJsonString = attributesJsonString + } + + enum CodingKeys: String, CodingKey { + case profileIdentifier = "profile_identifier" + case attributesJsonString = "attributes_json_string" + } +} diff --git a/Sources/Common/Background Queue/Task Data/TrackEventQueueTaskData.swift b/Sources/Common/Background Queue/Task Data/TrackEventQueueTaskData.swift new file mode 100644 index 000000000..18403d01a --- /dev/null +++ b/Sources/Common/Background Queue/Task Data/TrackEventQueueTaskData.swift @@ -0,0 +1,17 @@ +import Foundation + +public struct TrackEventQueueTaskData: Codable { + public let identifier: String + /// JSON string: '{"foo": "bar"}' + public let attributesJsonString: String + + public init(identifier: String, attributesJsonString: String) { + self.identifier = identifier + self.attributesJsonString = attributesJsonString + } + + enum CodingKeys: String, CodingKey { + case identifier + case attributesJsonString = "attributes_json_string" + } +} diff --git a/Sources/Common/Background Queue/Type/QueueTaskType.swift b/Sources/Common/Background Queue/Type/QueueTaskType.swift index 24822acc3..cb519367b 100644 --- a/Sources/Common/Background Queue/Type/QueueTaskType.swift +++ b/Sources/Common/Background Queue/Type/QueueTaskType.swift @@ -1,5 +1,10 @@ import Foundation -enum QueueTaskType: String { +public enum QueueTaskType: String { case trackDeliveryMetric + case identifyProfile + case trackEvent + case registerPushToken + case deletePushToken + case trackPushMetric } diff --git a/Sources/Tracking/Service/Request/MetricRequest.swift b/Sources/Common/Service/Request/MetricRequest.swift similarity index 53% rename from Sources/Tracking/Service/Request/MetricRequest.swift rename to Sources/Common/Service/Request/MetricRequest.swift index e323227b9..d305dd5b6 100644 --- a/Sources/Tracking/Service/Request/MetricRequest.swift +++ b/Sources/Common/Service/Request/MetricRequest.swift @@ -2,11 +2,18 @@ import CioInternalCommon import Foundation // https://customer.io/docs/api/#operation/pushMetrics -struct MetricRequest: Codable { - let deliveryId: String - let event: Metric - let deviceToken: String - let timestamp: Date +public struct MetricRequest: Codable { + public let deliveryId: String + public let event: Metric + public let deviceToken: String + public let timestamp: Date + + public init(deliveryId: String, event: Metric, deviceToken: String, timestamp: Date) { + self.deliveryId = deliveryId + self.event = event + self.deviceToken = deviceToken + self.timestamp = timestamp + } enum CodingKeys: String, CodingKey { case deliveryId = "delivery_id" diff --git a/Sources/Common/autogenerated/AutoMockable.generated.swift b/Sources/Common/autogenerated/AutoMockable.generated.swift index 400724e15..74f92a678 100644 --- a/Sources/Common/autogenerated/AutoMockable.generated.swift +++ b/Sources/Common/autogenerated/AutoMockable.generated.swift @@ -832,42 +832,6 @@ public class HooksManagerMock: HooksManager, Mock { } } - /** - When setter of the property called, the value given to setter is set here. - When the getter of the property called, the value set here will be returned. Your chance to mock the property. - */ - public var underlyingQueueRunnerHooks: [QueueRunnerHook] = [] - /// `true` if the getter or setter of property is called at least once. - public var queueRunnerHooksCalled: Bool { - queueRunnerHooksGetCalled || queueRunnerHooksSetCalled - } - - /// `true` if the getter called on the property at least once. - public var queueRunnerHooksGetCalled: Bool { - queueRunnerHooksGetCallsCount > 0 - } - - public var queueRunnerHooksGetCallsCount = 0 - /// `true` if the setter called on the property at least once. - public var queueRunnerHooksSetCalled: Bool { - queueRunnerHooksSetCallsCount > 0 - } - - public var queueRunnerHooksSetCallsCount = 0 - /// The mocked property with a getter and setter. - public var queueRunnerHooks: [QueueRunnerHook] { - get { - mockCalled = true - queueRunnerHooksGetCallsCount += 1 - return underlyingQueueRunnerHooks - } - set(value) { - mockCalled = true - queueRunnerHooksSetCallsCount += 1 - underlyingQueueRunnerHooks = value - } - } - /** When setter of the property called, the value given to setter is set here. When the getter of the property called, the value set here will be returned. Your chance to mock the property. @@ -907,8 +871,6 @@ public class HooksManagerMock: HooksManager, Mock { public func resetMock() { profileIdentifyHooksGetCallsCount = 0 profileIdentifyHooksSetCallsCount = 0 - queueRunnerHooksGetCallsCount = 0 - queueRunnerHooksSetCallsCount = 0 screenViewHooksGetCallsCount = 0 screenViewHooksSetCallsCount = 0 addCallsCount = 0 @@ -1368,42 +1330,6 @@ public class ModuleHookProviderMock: ModuleHookProvider, Mock { } } - /** - When setter of the property called, the value given to setter is set here. - When the getter of the property called, the value set here will be returned. Your chance to mock the property. - */ - public var underlyingQueueRunnerHook: QueueRunnerHook? = nil - /// `true` if the getter or setter of property is called at least once. - public var queueRunnerHookCalled: Bool { - queueRunnerHookGetCalled || queueRunnerHookSetCalled - } - - /// `true` if the getter called on the property at least once. - public var queueRunnerHookGetCalled: Bool { - queueRunnerHookGetCallsCount > 0 - } - - public var queueRunnerHookGetCallsCount = 0 - /// `true` if the setter called on the property at least once. - public var queueRunnerHookSetCalled: Bool { - queueRunnerHookSetCallsCount > 0 - } - - public var queueRunnerHookSetCallsCount = 0 - /// The mocked property with a getter and setter. - public var queueRunnerHook: QueueRunnerHook? { - get { - mockCalled = true - queueRunnerHookGetCallsCount += 1 - return underlyingQueueRunnerHook - } - set(value) { - mockCalled = true - queueRunnerHookSetCallsCount += 1 - underlyingQueueRunnerHook = value - } - } - /** When setter of the property called, the value given to setter is set here. When the getter of the property called, the value set here will be returned. Your chance to mock the property. @@ -1444,9 +1370,6 @@ public class ModuleHookProviderMock: ModuleHookProvider, Mock { profileIdentifyHook = nil profileIdentifyHookGetCallsCount = 0 profileIdentifyHookSetCallsCount = 0 - queueRunnerHook = nil - queueRunnerHookGetCallsCount = 0 - queueRunnerHookSetCallsCount = 0 screenTrackingHook = nil screenTrackingHookGetCallsCount = 0 screenTrackingHookSetCallsCount = 0 @@ -2033,61 +1956,6 @@ public class QueueRunnerMock: QueueRunner, Mock { } } -/** - Class to easily create a mocked version of the `QueueRunnerHook` class. - This class is equipped with functions and properties ready for you to mock! - - Note: This file is automatically generated. This means the mocks should always be up-to-date and has a consistent API. - See the SDK documentation to learn the basics behind using the mock classes in the SDK. - */ -public class QueueRunnerHookMock: QueueRunnerHook, Mock { - /// If *any* interactions done on mock. `true` if any method or property getter/setter called. - public var mockCalled: Bool = false // - - public init() { - Mocks.shared.add(mock: self) - } - - public func resetMock() { - runTaskCallsCount = 0 - runTaskReceivedArguments = nil - runTaskReceivedInvocations = [] - - mockCalled = false // do last as resetting properties above can make this true - } - - // MARK: - runTask - - /// Number of times the function was called. - public private(set) var runTaskCallsCount = 0 - /// `true` if the function was ever called. - public var runTaskCalled: Bool { - runTaskCallsCount > 0 - } - - /// The arguments from the *last* time the function was called. - public private(set) var runTaskReceivedArguments: (task: QueueTask, onComplete: (Result) -> Void)? - /// Arguments from *all* of the times that the function was called. - public private(set) var runTaskReceivedInvocations: [(task: QueueTask, onComplete: (Result) -> Void)] = [] - /// Value to return from the mocked function. - public var runTaskReturnValue: Bool! - /** - Set closure to get called when function gets called. Great way to test logic or return a value for the function. - The closure has first priority to return a value for the mocked function. If the closure returns `nil`, - then the mock will attempt to return the value for `runTaskReturnValue` - */ - public var runTaskClosure: ((QueueTask, @escaping (Result) -> Void) -> Bool)? - - /// Mocked function for `runTask(_ task: QueueTask, onComplete: @escaping (Result) -> Void)`. Your opportunity to return a mocked value and check result of mock in test code. - public func runTask(_ task: QueueTask, onComplete: @escaping (Result) -> Void) -> Bool { - mockCalled = true - runTaskCallsCount += 1 - runTaskReceivedArguments = (task: task, onComplete: onComplete) - runTaskReceivedInvocations.append((task: task, onComplete: onComplete)) - return runTaskClosure.map { $0(task, onComplete) } ?? runTaskReturnValue - } -} - /** Class to easily create a mocked version of the `QueueStorage` class. This class is equipped with functions and properties ready for you to mock! diff --git a/Sources/Common/hooks/HooksManager.swift b/Sources/Common/hooks/HooksManager.swift index fc8b296ba..0a6dc8d6e 100644 --- a/Sources/Common/hooks/HooksManager.swift +++ b/Sources/Common/hooks/HooksManager.swift @@ -3,7 +3,6 @@ import Foundation public protocol HooksManager: AutoMockable { func add(key: HookModule, provider: ModuleHookProvider) var profileIdentifyHooks: [ProfileIdentifyHook] { get } - var queueRunnerHooks: [QueueRunnerHook] { get } var screenViewHooks: [ScreenTrackingHook] { get } } @@ -34,10 +33,6 @@ public class CioHooksManager: HooksManager { hookProviders.filter { $0.value.profileIdentifyHook != nil }.map { $0.value.profileIdentifyHook! } } - public var queueRunnerHooks: [QueueRunnerHook] { - hookProviders.filter { $0.value.queueRunnerHook != nil }.map { $0.value.queueRunnerHook! } - } - public var screenViewHooks: [ScreenTrackingHook] { hookProviders.filter { $0.value.screenTrackingHook != nil }.map { $0.value.screenTrackingHook! } } diff --git a/Sources/Common/hooks/ModuleHook.swift b/Sources/Common/hooks/ModuleHook.swift index c3876c5ed..9e36e5e12 100644 --- a/Sources/Common/hooks/ModuleHook.swift +++ b/Sources/Common/hooks/ModuleHook.swift @@ -5,7 +5,6 @@ import Foundation /// from initializing so many classes. So, initialize instances when requested. public protocol ModuleHookProvider: AutoMockable { var profileIdentifyHook: ProfileIdentifyHook? { get } - var queueRunnerHook: QueueRunnerHook? { get } var screenTrackingHook: ScreenTrackingHook? { get } } @@ -22,13 +21,6 @@ public protocol ProfileIdentifyHook: AutoMockable { func beforeProfileStoppedBeingIdentified(oldIdentifier: String) } -// When a module wants to run background queue tasks, they implement this hook. -public protocol QueueRunnerHook: AutoMockable { - /// called from background queue in `Tracking` module. - /// return `true` if the `task` belongs to that module. - func runTask(_ task: QueueTask, onComplete: @escaping (Result) -> Void) -> Bool -} - // Hook for when a screen view track event is sent public protocol ScreenTrackingHook: AutoMockable { func screenViewed(name: String) diff --git a/Sources/MessagingInApp/hooks/HookProvider.swift b/Sources/MessagingInApp/hooks/HookProvider.swift index ee087badc..cb6ffefde 100644 --- a/Sources/MessagingInApp/hooks/HookProvider.swift +++ b/Sources/MessagingInApp/hooks/HookProvider.swift @@ -16,10 +16,6 @@ class MessagingInAppModuleHookProvider: ModuleHookProvider { return MessagingInAppImplementation(diGraph: diGraph) } - var queueRunnerHook: QueueRunnerHook? { - nil - } - var screenTrackingHook: ScreenTrackingHook? { guard let diGraph = diGraph else { return nil } diff --git a/Sources/Tracking/Background Queue/QueueRunner.swift b/Sources/Tracking/Background Queue/QueueRunner.swift deleted file mode 100644 index 4cb49b60a..000000000 --- a/Sources/Tracking/Background Queue/QueueRunner.swift +++ /dev/null @@ -1,97 +0,0 @@ -import CioInternalCommon -import Foundation - -// Queue tasks for the Tracking module. -// sourcery: InjectRegister = "QueueRunnerHook" -class TrackingQueueRunner: ApiSyncQueueRunner, QueueRunnerHook { - override init( - jsonAdapter: JsonAdapter, - logger: Logger, - httpClient: HttpClient, - sdkConfig: SdkConfig - ) { - super.init( - jsonAdapter: jsonAdapter, - logger: logger, - httpClient: httpClient, - sdkConfig: sdkConfig - ) - } - - public func runTask(_ task: QueueTask, onComplete: @escaping (Result) -> Void) -> Bool { - guard let queueTaskType = QueueTaskType(rawValue: task.type) else { - return false - } - - switch queueTaskType { - case .identifyProfile: identify(task, onComplete: onComplete) - case .trackEvent: track(task, onComplete: onComplete) - case .registerPushToken: registerPushToken(task, onComplete: onComplete) - case .deletePushToken: deletePushToken(task, onComplete: onComplete) - case .trackPushMetric: trackPushMetric(task, onComplete: onComplete) - } - - return true - } -} - -extension TrackingQueueRunner { - private func identify(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { - guard let taskData = getTaskData(task, type: IdentifyProfileQueueTaskData.self) else { - return onComplete(failureIfDontDecodeTaskData) - } - - performHttpRequest( - endpoint: .identifyCustomer(identifier: taskData.identifier), - requestBody: taskData.attributesJsonString?.data, - onComplete: onComplete - ) - } - - private func track(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { - guard let taskData = getTaskData(task, type: TrackEventQueueTaskData.self) else { - return onComplete(failureIfDontDecodeTaskData) - } - - performHttpRequest( - endpoint: .trackCustomerEvent(identifier: taskData.identifier), - requestBody: taskData.attributesJsonString.data, - onComplete: onComplete - ) - } - - private func registerPushToken(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { - guard let taskData = getTaskData(task, type: RegisterPushNotificationQueueTaskData.self) else { - return onComplete(failureIfDontDecodeTaskData) - } - - performHttpRequest( - endpoint: .registerDevice(identifier: taskData.profileIdentifier), - requestBody: taskData.attributesJsonString?.data, - onComplete: onComplete - ) - } - - private func deletePushToken(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { - guard let taskData = getTaskData(task, type: DeletePushNotificationQueueTaskData.self) else { - return onComplete(failureIfDontDecodeTaskData) - } - - performHttpRequest(endpoint: .deleteDevice( - identifier: taskData.profileIdentifier, - deviceToken: taskData.deviceToken - ), requestBody: nil, onComplete: onComplete) - } - - private func trackPushMetric(_ task: QueueTask, onComplete: @escaping (Result) -> Void) { - guard let taskData = getTaskData(task, type: MetricRequest.self) else { - return onComplete(failureIfDontDecodeTaskData) - } - - guard let bodyData = jsonAdapter.toJson(taskData) else { - return - } - - performHttpRequest(endpoint: .pushMetrics, requestBody: bodyData, onComplete: onComplete) - } -} diff --git a/Sources/Tracking/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift b/Sources/Tracking/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift deleted file mode 100644 index 1a82b5279..000000000 --- a/Sources/Tracking/Background Queue/Task Data/DeletePushNotificationQueueTaskData.swift +++ /dev/null @@ -1,11 +0,0 @@ -import Foundation - -struct DeletePushNotificationQueueTaskData: Codable { - let profileIdentifier: String - let deviceToken: String - - enum CodingKeys: String, CodingKey { - case profileIdentifier = "profile_identifier" - case deviceToken = "device_token" - } -} diff --git a/Sources/Tracking/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift b/Sources/Tracking/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift deleted file mode 100644 index 3291c084f..000000000 --- a/Sources/Tracking/Background Queue/Task Data/IdentifyProfileQueueTaskData.swift +++ /dev/null @@ -1,12 +0,0 @@ -import Foundation - -struct IdentifyProfileQueueTaskData: Codable { - let identifier: String - /// JSON string: '{"foo": "bar"}' - let attributesJsonString: String? - - enum CodingKeys: String, CodingKey { - case identifier - case attributesJsonString = "attributes_json_string" - } -} diff --git a/Sources/Tracking/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift b/Sources/Tracking/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift deleted file mode 100644 index 1125e8e28..000000000 --- a/Sources/Tracking/Background Queue/Task Data/RegisterPushNotificationQueueTaskData.swift +++ /dev/null @@ -1,11 +0,0 @@ -import Foundation - -struct RegisterPushNotificationQueueTaskData: Codable { - let profileIdentifier: String - let attributesJsonString: String? - - enum CodingKeys: String, CodingKey { - case profileIdentifier = "profile_identifier" - case attributesJsonString = "attributes_json_string" - } -} diff --git a/Sources/Tracking/Background Queue/Task Data/TrackEventQueueTaskData.swift b/Sources/Tracking/Background Queue/Task Data/TrackEventQueueTaskData.swift deleted file mode 100644 index 8f37f1a62..000000000 --- a/Sources/Tracking/Background Queue/Task Data/TrackEventQueueTaskData.swift +++ /dev/null @@ -1,12 +0,0 @@ -import Foundation - -struct TrackEventQueueTaskData: Codable { - let identifier: String - /// JSON string: '{"foo": "bar"}' - let attributesJsonString: String - - enum CodingKeys: String, CodingKey { - case identifier - case attributesJsonString = "attributes_json_string" - } -} diff --git a/Sources/Tracking/Background Queue/Type/QueueTaskType.swift b/Sources/Tracking/Background Queue/Type/QueueTaskType.swift deleted file mode 100644 index ffb80fdfe..000000000 --- a/Sources/Tracking/Background Queue/Type/QueueTaskType.swift +++ /dev/null @@ -1,10 +0,0 @@ -import Foundation - -/// All the types of tasks the `Tracking` module runs in the background queue -enum QueueTaskType: String { - case identifyProfile - case trackEvent - case registerPushToken - case deletePushToken - case trackPushMetric -} diff --git a/Sources/Tracking/Service/Request/Device.swift b/Sources/Tracking/Service/Request/Device.swift index 3956372bc..f3ed6d8e8 100644 --- a/Sources/Tracking/Service/Request/Device.swift +++ b/Sources/Tracking/Service/Request/Device.swift @@ -1,11 +1,18 @@ import Foundation // Standard device structure -struct Device: Encodable { - let token: String - let platform: String // iOS, tvOS, watchOS, etc. - let lastUsed: Date - let attributes: T? +public struct Device: Encodable { + public let token: String + public let platform: String // iOS, tvOS, watchOS, etc. + public let lastUsed: Date + public let attributes: T? + + public init(token: String, platform: String, lastUsed: Date, attributes: T?) { + self.token = token + self.platform = platform + self.lastUsed = lastUsed + self.attributes = attributes + } enum CodingKeys: String, CodingKey { case platform diff --git a/Sources/Tracking/Service/Request/RegisterDeviceRequest.swift b/Sources/Tracking/Service/Request/RegisterDeviceRequest.swift index e45a4860c..fc603c2c4 100644 --- a/Sources/Tracking/Service/Request/RegisterDeviceRequest.swift +++ b/Sources/Tracking/Service/Request/RegisterDeviceRequest.swift @@ -1,6 +1,10 @@ import Foundation // https://customer.io/docs/api/#operation/add_device -struct RegisterDeviceRequest: Encodable { - let device: Device +public struct RegisterDeviceRequest: Encodable { + public let device: Device + + public init(device: Device) { + self.device = device + } } diff --git a/Sources/Tracking/autogenerated/AutoDependencyInjection.generated.swift b/Sources/Tracking/autogenerated/AutoDependencyInjection.generated.swift index c48bb48f2..a2e50b9a9 100644 --- a/Sources/Tracking/autogenerated/AutoDependencyInjection.generated.swift +++ b/Sources/Tracking/autogenerated/AutoDependencyInjection.generated.swift @@ -60,9 +60,6 @@ extension DIGraph { _ = deviceAttributesProvider countDependenciesResolved += 1 - _ = queueRunnerHook - countDependenciesResolved += 1 - return countDependenciesResolved } @@ -85,16 +82,6 @@ extension DIGraph { private var newDeviceAttributesProvider: DeviceAttributesProvider { SdkDeviceAttributesProvider(sdkConfig: sdkConfig, deviceInfo: deviceInfo) } - - // QueueRunnerHook - var queueRunnerHook: QueueRunnerHook { - getOverriddenInstance() ?? - newQueueRunnerHook - } - - private var newQueueRunnerHook: QueueRunnerHook { - TrackingQueueRunner(jsonAdapter: jsonAdapter, logger: logger, httpClient: httpClient, sdkConfig: sdkConfig) - } } // swiftlint:enable all diff --git a/Sources/Tracking/hooks/TrackingModuleHookProvider.swift b/Sources/Tracking/hooks/TrackingModuleHookProvider.swift index abdbc3648..f0ea04f7e 100644 --- a/Sources/Tracking/hooks/TrackingModuleHookProvider.swift +++ b/Sources/Tracking/hooks/TrackingModuleHookProvider.swift @@ -12,10 +12,6 @@ class TrackingModuleHookProvider: ModuleHookProvider { nil } - var queueRunnerHook: QueueRunnerHook? { - diGraph?.queueRunnerHook - } - var screenTrackingHook: ScreenTrackingHook? { nil } diff --git a/Tests/Shared/extension/QueueStorageExtension.swift b/Tests/Shared/extension/QueueStorageExtension.swift index ee94754dc..a200ebd97 100644 --- a/Tests/Shared/extension/QueueStorageExtension.swift +++ b/Tests/Shared/extension/QueueStorageExtension.swift @@ -3,7 +3,7 @@ import Foundation public extension QueueStorage { - func filterTrackEvents(_ type: CioTracking.QueueTaskType) -> [QueueTaskMetadata] { + func filterTrackEvents(_ type: QueueTaskType) -> [QueueTaskMetadata] { getInventory().filter { $0.taskType == type.rawValue } } }