session-ios/SessionMessagingKit/Jobs/JobQueue.swift

104 lines
4.0 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-08 03:12:38 +01:00
2020-11-10 05:48:47 +01:00
@objc(SNJobQueue)
public final class JobQueue : NSObject, JobDelegate {
2020-11-24 10:09:23 +01:00
private var hasResumedPendingJobs = false // Just for debugging
2020-11-08 03:12:38 +01:00
2020-11-10 05:48:47 +01:00
@objc public static let shared = JobQueue()
2020-11-08 03:12:38 +01:00
2020-11-10 05:48:47 +01:00
@objc public func add(_ job: Job, using transaction: Any) {
2020-11-24 05:36:03 +01:00
let transaction = transaction as! YapDatabaseReadWriteTransaction
2020-11-20 04:04:56 +01:00
addWithoutExecuting(job, using: transaction)
2020-11-26 23:07:24 +01:00
transaction.addCompletionQueue(Threading.jobQueue) {
2020-11-24 05:36:03 +01:00
job.execute()
}
2020-11-20 04:04:56 +01:00
}
@objc public func addWithoutExecuting(_ job: Job, using transaction: Any) {
job.id = String(NSDate.millisecondTimestamp())
2020-12-02 06:25:16 +01:00
SNMessagingKitConfiguration.shared.storage.persist(job, using: transaction)
2020-11-08 03:12:38 +01:00
job.delegate = self
}
@objc public func resumePendingJobs() {
2020-11-24 10:09:23 +01:00
if hasResumedPendingJobs {
#if DEBUG
preconditionFailure("resumePendingJobs() should only be called once.")
#endif
}
hasResumedPendingJobs = true
let allJobTypes: [Job.Type] = [ AttachmentDownloadJob.self, AttachmentUploadJob.self, MessageReceiveJob.self, MessageSendJob.self, NotifyPNServerJob.self ]
allJobTypes.forEach { type in
2020-12-02 06:25:16 +01:00
let allPendingJobs = SNMessagingKitConfiguration.shared.storage.getAllPendingJobs(of: type)
2020-11-26 23:27:20 +01:00
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()
}
}
}
2020-11-08 03:12:38 +01:00
public func handleJobSucceeded(_ job: Job) {
2020-12-02 06:25:16 +01:00
SNMessagingKitConfiguration.shared.storage.withAsync({ transaction in
SNMessagingKitConfiguration.shared.storage.markJobAsSucceeded(job, using: transaction)
2020-11-08 03:54:40 +01:00
}, completion: {
// Do nothing
})
2020-11-08 03:12:38 +01:00
}
public func handleJobFailed(_ job: Job, with error: Error) {
2020-11-08 03:54:40 +01:00
job.failureCount += 1
2020-12-02 06:25:16 +01:00
let storage = SNMessagingKitConfiguration.shared.storage
guard !storage.isJobCanceled(job) else { return SNLog("\(type(of: job)) canceled.") }
2020-11-08 03:54:40 +01:00
storage.withAsync({ transaction in
storage.persist(job, using: transaction)
}, completion: { // Intentionally capture self
if job.failureCount == type(of: job).maxFailureCount {
storage.withAsync({ transaction in
storage.markJobAsFailed(job, using: transaction)
}, completion: {
// Do nothing
})
} else {
let retryInterval = self.getRetryInterval(for: job)
SNLog("\(type(of: job)) failed; scheduling retry (failure count is \(job.failureCount)).")
2020-11-26 23:07:24 +01:00
Timer.scheduledTimer(timeInterval: retryInterval, target: self, selector: #selector(self.retry(_:)), userInfo: job, repeats: false)
2020-11-08 03:54:40 +01:00
}
})
}
2020-11-18 05:36:51 +01:00
public func handleJobFailedPermanently(_ job: Job, with error: Error) {
job.failureCount += 1
2020-12-02 06:25:16 +01:00
let storage = SNMessagingKitConfiguration.shared.storage
2020-11-18 05:36:51 +01:00
storage.withAsync({ transaction in
storage.persist(job, using: transaction)
}, completion: { // Intentionally capture self
storage.withAsync({ transaction in
storage.markJobAsFailed(job, using: transaction)
}, completion: {
// Do nothing
})
})
}
2020-11-08 03:54:40 +01:00
private func getRetryInterval(for job: Job) -> TimeInterval {
// Arbitrary backoff factor...
// try 1 delay: 0.00s
// try 2 delay: 0.19s
// ...
// try 5 delay: 1.30s
// ...
// try 11 delay: 61.31s
let backoffFactor = 1.9
let maxBackoff: Double = 60 * 60 * 1000
return 0.1 * min(maxBackoff, pow(backoffFactor, Double(job.failureCount)))
}
2020-11-26 23:27:20 +01:00
@objc private func retry(_ timer: Timer) {
guard let job = timer.userInfo as? Job else { return }
SNLog("Retrying \(type(of: job)).")
2020-12-01 01:40:37 +01:00
job.delegate = self
2020-11-08 03:54:40 +01:00
job.execute()
2020-11-08 03:12:38 +01:00
}
}