Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload parallelism and retries #46

Closed
wants to merge 5 commits into from
Closed

Conversation

hansallis
Copy link

This PR is an attempt at making photo uploads parallel and adding a retry mechanism as well.
3 attempts are made for each image. Currently, if the registration on AirTable fails, it will also re-upload the entire image which is not ideal.

Relates to issues #35 and #37.

Comment on lines +159 to +180
completion: { result in
switch result {
case let .success(airtableTree):
self?.logger.log(.upload, "Successfully uploaded tree.")
self?.database.save([airtableTree], sentFromThisDevice: true)
self?.database.remove(tree: tree) {
self?.currentUploads[tree.phImageId] = nil;
if (self != nil && self!.currentUploads.count == 0) {
self?.presentUploadButton(isUploading: false)
}
self?.presentTreesFromDatabase()
self?.uploadLocalTreesRecursively()
}
case let .failure(error):
self?.failedUploadCount[tree.phImageId] = (self?.failedUploadCount[tree.phImageId] ?? 0) + 1
self?.currentUploads[tree.phImageId] = nil;
self?.update(uploadProgress: 0.0, for: tree)
self?.logger.log(.upload, "Error when uploading a local tree: \(error)")
self?.uploadLocalTreesRecursively()
if (self != nil && self!.currentUploads.count == 0) {
self?.presentUploadButton(isUploading: false)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uploadLocalTreesRecursively is getting a little long. The completion callback seems like a good candidate for extraction. It seems to only use self and tree.

let sortedTrees = trees.sorted(by: \.createDate, order: .descending)
while (self != nil && self!.currentUploads.count < self!.maxConcurrentUploads && self!.currentUploads.count < trees.count) {

guard let tree = sortedTrees.first(where: { treeEntry in self?.currentUploads[treeEntry.phImageId] == nil && (self?.failedUploadCount[treeEntry.phImageId] ?? 0) < self!.maxUploadAttempts}) else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌🏼

@philsturgeon
Copy link
Collaborator

Thanks for this folks, we used it as inspiration for #47 🙌🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants