Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #219 from Sage-Bionetworks/syoung/upload-restart
Browse files Browse the repository at this point in the history
If an upload request is more than a day old then cancel it and retry
  • Loading branch information
syoung-smallwisdom authored Jun 27, 2023
2 parents f8f9039 + a41ebb8 commit 31d140f
Showing 1 changed file with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import JsonModel
import UIKit
#endif

protocol BridgeFileUploadMetadataBlob {}
protocol BridgeFileUploadMetadataBlob {
var userInfo: [String : Any]? { get }
}
protocol BridgeUploadTrackingData : Codable {
var userInfo: [String : Any]? { get }
}
Expand Down Expand Up @@ -411,7 +413,7 @@ extension BridgeFileUploadAPITyped {
// throw this over to the main queue so we can access Kotlin stuff
OperationQueue.main.addOperation {
guard let sessionToken = uploadManager.appManager.sessionToken else {
Logger.log(severity: .warn, tag: .upload, message: "Unable to request an upload URL from Bridge--not logged in to an account.")
Logger.log(tag: .upload, error: BridgeAuthError(), metadata: uploadMetadata.userInfo)
return
}

Expand Down Expand Up @@ -665,11 +667,13 @@ public class BridgeFileUploadManager: NSObject, URLSessionBackgroundDelegate {
do {
mapping = try DictionaryDecoder().decode(type, from: mappingPlist)
} catch let error {
Logger.log(severity: .error, tag: .upload, message: "Error attempting to decode plist object to \(T.self):\n\(mappingPlist)\n\(error)")
Logger.log(tag: .upload, error: error, message: "Error attempting to decode plist object to \(T.self):\n\(mappingPlist)")
}
}
else {
Logger.log(tag: .upload, error: BridgeUnexpectedNullError(category: .missingMapping, message: "in retrieveMapping: Missing mapping for \(key)"))
}
}

return mapping
}

Expand Down Expand Up @@ -1018,10 +1022,18 @@ public class BridgeFileUploadManager: NSObject, URLSessionBackgroundDelegate {
for invariantFilePath in uploadURLsRequested.keys {
guard let api = self.apiFromXAttrs(for: invariantFilePath) else { continue }
guard let metadataBlob = api.retrieveMetadata(from: invariantFilePath, mappings: uploadURLsRequested) else { continue }
// Assume files with an active urlsession task are not (yet) orphaned, no matter how old.
guard !filesInFlight.contains(invariantFilePath) else { continue }

var fileUrl: URL!
guard self.fileIsADayOld(invariantFilePath: invariantFilePath, fileUrl: &fileUrl) else { continue }
guard self.fileIsADayOld(invariantFilePath: invariantFilePath, fileUrl: &fileUrl)
else {
Logger.log(severity: .info, message: "checkForOrphanedUploads: Exiting upload request resend - file is less than a day old.")
continue
}

// If there is a file in-flight then something went wrong. Cancel before resending
if let inflight = tasks.first(where: { $0.description == invariantFilePath }) {
inflight.cancel()
}

// If we get all the way here, touch the file to reset the orphanage clock
// and then send the upload request again.
Expand Down Expand Up @@ -1563,6 +1575,22 @@ extension URL {
}
}

struct BridgeAuthError : Error, CustomNSError {
static var errorDomain: String { "BridgeClientUI.AuthError" }

let errorCode: Int
let message: String

init(errorCode: Int = -1, message: String = "Not logged in to an account.") {
self.errorCode = errorCode
self.message = message
}

var errorUserInfo: [String : Any] {
[NSLocalizedFailureReasonErrorKey: message]
}
}

struct BridgeHttpError : Error, CustomNSError {
static var errorDomain: String { "BridgeClientUI.HttpError" }

Expand Down Expand Up @@ -1597,5 +1625,6 @@ struct BridgeUnexpectedNullError : Error, CustomNSError {
case invalidURL = -105
case corruptData = -106
case missingMetadata = -107
case missingMapping = -108
}
}

0 comments on commit 31d140f

Please sign in to comment.