Fix job retrying

pull/313/head
nielsandriesse 4 years ago
parent 77c1f721b9
commit 2fa3a7edb7

@ -57,6 +57,7 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N
// MARK: Running // MARK: Running
public func execute() { public func execute() {
SNLog("Attachment upload failure count: \(failureCount).")
guard let stream = TSAttachmentStream.fetch(uniqueId: attachmentID) else { guard let stream = TSAttachmentStream.fetch(uniqueId: attachmentID) else {
return handleFailure(error: Error.noAttachment) return handleFailure(error: Error.noAttachment)
} }
@ -78,16 +79,19 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N
} }
private func handleSuccess() { private func handleSuccess() {
SNLog("Attachment uploaded successfully.")
delegate?.handleJobSucceeded(self) delegate?.handleJobSucceeded(self)
Configuration.shared.storage.resumeMessageSendJobIfNeeded(messageSendJobID) Configuration.shared.storage.resumeMessageSendJobIfNeeded(messageSendJobID)
} }
private func handlePermanentFailure(error: Swift.Error) { private func handlePermanentFailure(error: Swift.Error) {
SNLog("Attachment upload failed permanently due to error: \(error).")
delegate?.handleJobFailedPermanently(self, with: error) delegate?.handleJobFailedPermanently(self, with: error)
failAssociatedMessageSendJob(with: error) failAssociatedMessageSendJob(with: error)
} }
private func handleFailure(error: Swift.Error) { private func handleFailure(error: Swift.Error) {
SNLog("Attachment upload failed due to error: \(error).")
delegate?.handleJobFailed(self, with: error) delegate?.handleJobFailed(self, with: error)
if failureCount + 1 == AttachmentUploadJob.maxFailureCount { if failureCount + 1 == AttachmentUploadJob.maxFailureCount {
failAssociatedMessageSendJob(with: error) 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 ] let allJobTypes: [Job.Type] = [ AttachmentDownloadJob.self, AttachmentUploadJob.self, MessageReceiveJob.self, MessageSendJob.self, NotifyPNServerJob.self ]
allJobTypes.forEach { type in allJobTypes.forEach { type in
let allPendingJobs = Configuration.shared.storage.getAllPendingJobs(of: type) 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 { } else {
let retryInterval = self.getRetryInterval(for: job) 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) 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))) return 0.1 * min(maxBackoff, pow(backoffFactor, Double(job.failureCount)))
} }
@objc private func retry(_ job: Any) { @objc private func retry(_ timer: Timer) {
guard let job = job as? Job else { return } SNLog("Retrying job.")
guard let job = timer.userInfo as? Job else { return }
job.execute() job.execute()
} }
} }

Loading…
Cancel
Save