From 2fa3a7edb79a47e7ea24176465a68392a4b3e589 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 27 Nov 2020 09:27:20 +1100 Subject: [PATCH] Fix job retrying --- SessionMessagingKit/Jobs/AttachmentUploadJob.swift | 4 ++++ SessionMessagingKit/Jobs/JobQueue.swift | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift index 45c92a490..c42e2ba8e 100644 --- a/SessionMessagingKit/Jobs/AttachmentUploadJob.swift +++ b/SessionMessagingKit/Jobs/AttachmentUploadJob.swift @@ -57,6 +57,7 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N // MARK: Running public func execute() { + SNLog("Attachment upload failure count: \(failureCount).") guard let stream = TSAttachmentStream.fetch(uniqueId: attachmentID) else { return handleFailure(error: Error.noAttachment) } @@ -78,16 +79,19 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N } private func handleSuccess() { + SNLog("Attachment uploaded successfully.") delegate?.handleJobSucceeded(self) Configuration.shared.storage.resumeMessageSendJobIfNeeded(messageSendJobID) } private func handlePermanentFailure(error: Swift.Error) { + SNLog("Attachment upload failed permanently due to error: \(error).") delegate?.handleJobFailedPermanently(self, with: error) failAssociatedMessageSendJob(with: error) } private func handleFailure(error: Swift.Error) { + SNLog("Attachment upload failed due to error: \(error).") delegate?.handleJobFailed(self, with: error) if failureCount + 1 == AttachmentUploadJob.maxFailureCount { failAssociatedMessageSendJob(with: error) diff --git a/SessionMessagingKit/Jobs/JobQueue.swift b/SessionMessagingKit/Jobs/JobQueue.swift index 1a7eb2e3f..e73e12530 100644 --- a/SessionMessagingKit/Jobs/JobQueue.swift +++ b/SessionMessagingKit/Jobs/JobQueue.swift @@ -32,7 +32,11 @@ public final class JobQueue : NSObject, JobDelegate { let allJobTypes: [Job.Type] = [ AttachmentDownloadJob.self, AttachmentUploadJob.self, MessageReceiveJob.self, MessageSendJob.self, NotifyPNServerJob.self ] allJobTypes.forEach { type in let allPendingJobs = Configuration.shared.storage.getAllPendingJobs(of: type) - allPendingJobs.sorted(by: { $0.id! < $1.id! }).forEach { $0.execute() } // Retry the oldest jobs first + allPendingJobs.sorted(by: { $0.id! < $1.id! }).forEach { job in // Retry the oldest jobs first + SNLog("Resuming pending job of type: \(type).") + job.delegate = self + job.execute() + } } } @@ -58,6 +62,7 @@ public final class JobQueue : NSObject, JobDelegate { }) } else { let retryInterval = self.getRetryInterval(for: job) + SNLog("Job failed; scheduling retry.") Timer.scheduledTimer(timeInterval: retryInterval, target: self, selector: #selector(self.retry(_:)), userInfo: job, repeats: false) } }) @@ -90,8 +95,9 @@ public final class JobQueue : NSObject, JobDelegate { return 0.1 * min(maxBackoff, pow(backoffFactor, Double(job.failureCount))) } - @objc private func retry(_ job: Any) { - guard let job = job as? Job else { return } + @objc private func retry(_ timer: Timer) { + SNLog("Retrying job.") + guard let job = timer.userInfo as? Job else { return } job.execute() } }