Fix job retrying
This commit is contained in:
parent
77c1f721b9
commit
2fa3a7edb7
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue