From cebda588e8d9cf8b59e4fffbb63f77771a4ebba6 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Thu, 30 Sep 2021 11:20:00 +1000 Subject: [PATCH] fix remove job id concurrent issue --- SessionMessagingKit/Jobs/JobQueue.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/SessionMessagingKit/Jobs/JobQueue.swift b/SessionMessagingKit/Jobs/JobQueue.swift index e661189e5..159641522 100644 --- a/SessionMessagingKit/Jobs/JobQueue.swift +++ b/SessionMessagingKit/Jobs/JobQueue.swift @@ -7,6 +7,8 @@ public final class JobQueue : NSObject, JobDelegate { internal static var currentlyExecutingJobs: Set = [] + 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...