-
Notifications
You must be signed in to change notification settings - Fork 6
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf5c2c4
Concurrent uploads
hansallis 0aaedc2
Improved updating of the Upload button
hansallis 018fb88
Uploads are retried 2 times. No distinction between whether upload or…
hansallis 6a3fb4c
Merge branch 'protect-earth:main' into main
hansallis 7b7dc83
Changed .filter(..).first to .first(where:)
hansallis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,17 @@ final class UploadViewModel: CollectionViewModel { | |
var rightNavigationButtonsPublisher: Published<[NavigationBarButtonModel]>.Publisher { $rightNavigationButtons } | ||
var dataPublisher: Published<[ListSection<CollectionListItem>]>.Publisher { $data } | ||
|
||
let maxConcurrentUploads = 3 | ||
let maxUploadAttempts = 3 | ||
private var api: Api | ||
private var database: Database | ||
private var screenLockManager: ScreenLockManaging | ||
private var logger: Logging | ||
private var sites: [Site] = [] | ||
private var species: [Species] = [] | ||
private var supervisors: [Supervisor] = [] | ||
private var currentUpload: Cancellable? | ||
private var currentUploads: [String: Cancellable?] = [:] | ||
private var failedUploadCount: [String: Int] = [:] | ||
private weak var navigation: UploadNavigating? | ||
|
||
init(api: Api = CurrentEnvironment.api, database: Database = CurrentEnvironment.database, screenLockManager: ScreenLockManaging = CurrentEnvironment.screenLockManager, logger: Logging = CurrentEnvironment.logger, navigation: UploadNavigating) { | ||
|
@@ -107,6 +110,8 @@ final class UploadViewModel: CollectionViewModel { | |
presentUploadButton(isUploading: true) | ||
presentNavigationButtons(isUploading: true) | ||
screenLockManager.disableLocking() | ||
self.currentUploads = [:] | ||
self.failedUploadCount = [:] | ||
uploadLocalTreesRecursively() | ||
} | ||
|
||
|
@@ -125,44 +130,58 @@ final class UploadViewModel: CollectionViewModel { | |
|
||
private func cancelUploading() { | ||
logger.log(.upload, "Uploading cancelled.") | ||
currentUpload?.cancel() | ||
for ((_, upload)) in currentUploads { | ||
upload?.cancel(); | ||
} | ||
stopUploading() | ||
} | ||
|
||
private func uploadLocalTreesRecursively() { | ||
logger.log(.upload, "Uploading images...") | ||
database.fetchLocalTrees { [weak self] trees in | ||
self?.logger.log(.upload, "Trees to upload: \(trees.count)") | ||
|
||
guard let tree = trees.sorted(by: \.createDate, order: .descending).first else { | ||
self?.logger.log(.upload, "No more items to upload - bailing.") | ||
self?.stopUploading() | ||
return | ||
} | ||
|
||
self?.logger.log(.upload, "Now uploading tree: \(tree)") | ||
self?.currentUpload = self?.api.upload( | ||
tree: tree, | ||
progress: { progress in | ||
self?.logger.log(.upload, "Progress: \(progress)") | ||
self?.update(uploadProgress: progress, for: tree) | ||
}, | ||
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?.presentTreesFromDatabase() | ||
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 { | ||
self?.logger.log(.upload, "No more items to upload - bailing.") | ||
self?.stopUploading() | ||
return | ||
} | ||
|
||
self?.logger.log(.upload, "Now uploading tree: \(tree)") | ||
self?.currentUploads[tree.phImageId] = (self?.api.upload( | ||
tree: tree, | ||
progress: { progress in | ||
self?.logger.log(.upload, "Progress: \(progress)") | ||
self?.update(uploadProgress: progress, for: tree) | ||
}, | ||
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) | ||
} | ||
Comment on lines
+159
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
case let .failure(error): | ||
self?.update(uploadProgress: 0.0, for: tree) | ||
self?.presentUploadButton(isUploading: false) | ||
self?.logger.log(.upload, "Error when uploading a local tree: \(error)") | ||
} | ||
} | ||
) | ||
)) | ||
} | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌🏼