fix remove job id concurrent issue

This commit is contained in:
ryanzhao 2021-09-30 11:20:00 +10:00
parent a856415438
commit cebda588e8
1 changed files with 9 additions and 3 deletions

View File

@ -7,6 +7,8 @@ public final class JobQueue : NSObject, JobDelegate {
internal static var currentlyExecutingJobs: Set<String> = []
private let internalQueue: DispatchQueue = DispatchQueue(label:"executingJobQueue")
@objc public static let shared = JobQueue()
@objc public func add(_ job: Job, using transaction: Any) {
@ -47,7 +49,7 @@ public final class JobQueue : NSObject, JobDelegate {
}
public func handleJobSucceeded(_ job: Job) {
given(job.id) { JobQueue.currentlyExecutingJobs.remove($0) }
given(job.id) { removeExecutingJob($0) }
SNMessagingKitConfiguration.shared.storage.write(with: { transaction in
SNMessagingKitConfiguration.shared.storage.markJobAsSucceeded(job, using: transaction)
}, completion: {
@ -56,7 +58,7 @@ public final class JobQueue : NSObject, JobDelegate {
}
public func handleJobFailed(_ job: Job, with error: Error) {
given(job.id) { JobQueue.currentlyExecutingJobs.remove($0) }
given(job.id) { removeExecutingJob($0) }
job.failureCount += 1
let storage = SNMessagingKitConfiguration.shared.storage
guard !storage.isJobCanceled(job) else { return SNLog("\(type(of: job)) canceled.") }
@ -78,7 +80,7 @@ public final class JobQueue : NSObject, JobDelegate {
}
public func handleJobFailedPermanently(_ job: Job, with error: Error) {
given(job.id) { JobQueue.currentlyExecutingJobs.remove($0) }
given(job.id) { removeExecutingJob($0) }
job.failureCount += 1
let storage = SNMessagingKitConfiguration.shared.storage
storage.write(with: { transaction in
@ -91,6 +93,10 @@ public final class JobQueue : NSObject, JobDelegate {
})
})
}
private func removeExecutingJob(_ jobID: String) {
let _ = internalQueue.sync { JobQueue.currentlyExecutingJobs.remove(jobID) }
}
private func getRetryInterval(for job: Job) -> TimeInterval {
// Arbitrary backoff factor...