Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove semaphore from PersistentHitQueue #1042

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions AEPServices/Sources/utility/hitprocessor/PersistentHitQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PersistentHitQueue: HitQueuing {

private var suspended = true
private var isTaskScheduled = false
private let queue = DispatchQueue(label: "com.adobe.mobile.persistenthitqueue")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it looks like we are using the mobile identifier in a few other places. Do we want to remove it from all of those as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will update them separately.

private let queue = DispatchQueue(label: "com.adobe.persistentHitQueue")

/// Creates a new `HitQueue` with the underlying `DataQueue` which is used to persist hits
/// - Parameter dataQueue: a `DataQueue` used to persist hits
Expand Down Expand Up @@ -62,44 +62,34 @@ public class PersistentHitQueue: HitQueuing {
queue.async {
guard !self.suspended, !self.isTaskScheduled else { return }

self.isTaskScheduled = true

guard let hit = self.dataQueue.peek() else {
self.isTaskScheduled = false
return
} // nothing left in the queue, stop processing

let semaphore = DispatchSemaphore(value: 0)
self.isTaskScheduled = true
self.processor.processHit(entity: hit, completion: { [weak self] success in

guard let self = self else {
semaphore.signal()
return
}

guard let self = self else { return }

if success {
// successful processing of hit
// attempt to remove it from the queue and process next hit if successful
if self.dataQueue.remove() {
self.isTaskScheduled = false
self.processNextHit()
} else {
// deleting the hit from the database failed
// need to delete the database to try and recover
Log.warning(label: "PersistentHitQueue", "An unexpected error occurred while attempting to delete a record from the database. Data processing will be paused.")
self.queue.async {
// successful processing of hit
if self.dataQueue.remove() {
self.isTaskScheduled = false
self.processNextHit()
} else {
// deleting the hit from the database failed
Log.warning(label: "PersistentHitQueue", "An unexpected error occurred while attempting to delete a record from the database. Data processing will be paused.")
}
}
} else {
// processing hit failed, leave it in the queue, retry after the retry interval
self.queue.asyncAfter(deadline: .now() + self.processor.retryInterval(for: hit)) { [weak self] in
guard let self = self else { return }
self.isTaskScheduled = false
self.processNextHit()
}
// processing hit failed, retry after the retry interval
self.queue.asyncAfter(deadline: .now() + self.processor.retryInterval(for: hit)) { [weak self] in
guard let self = self else { return }
self.isTaskScheduled = false
self.processNextHit()
}
}

semaphore.signal()
})
semaphore.wait()
}
}
}