Skip to content

Commit

Permalink
Retry for certain error types, eg. time-outs
Browse files Browse the repository at this point in the history
Still not an ideal system, we shouldn’t be trying to upload if we are currently
downloading for example.
  • Loading branch information
mxcl committed Feb 24, 2019
1 parent 2f7b08e commit 41d513f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Sources/Model/Item/Dotfiles/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public class Sync {
private func announce(_ promise: Promise<Void>) {
delegate?.dotfilesSyncItemsUpdated()

promise.done { [weak self] in
self?.delegate?.dotfilesSyncItemsUpdated()
}.catch { [weak self] error in
self?.delegate?.dotfilesSyncError(error)
promise.catch {
self.delegate?.dotfilesSyncError($0)
}.finally {
self.delegate?.dotfilesSyncItemsUpdated()
}
}

Expand Down
43 changes: 39 additions & 4 deletions Sources/Model/Item/Item+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Dispatch
import Bakeware
import Path

import AppKit

public extension Item {
/// returns a new promise that is intended for user-communication only
private func reflect() -> Promise<Void> {
Expand Down Expand Up @@ -43,17 +45,39 @@ public extension Item {

func upload() -> Promise<Void>? {
dispatchPrecondition(condition: .onQueue(.main))


func save() -> Promise<Void> {
let p = db.save(self.record)
#if DEBUG
return p.done {
assert($0 === self.record)
}
#else
return p.asVoid()
#endif
}

func go() -> Promise<Void> {
return DispatchQueue.global().async(.promise) {
{ ($0, $0.md5) }(try Data(contentsOf: self.path))
}.get { data, md5 in
if (self.record[.checksum] as? String) == md5 {
let alert = NSAlert()
alert.informativeText = "NO DIFF"
alert.addButton(withTitle: "OK")
alert.runModal()
}
}.done { data, md5 in
self.record[.data] = data as CKRecordValue
self.record[.checksum] = md5 as CKRecordValue
}.then {
db.save(self.record).done {
assert($0 === self.record)
}
save()
}.recover { error -> Promise<Void> in

// for CloudKit time-outs or no Internet, keep trying

guard error.shouldRetry else { throw error }
return after(.seconds(2)).then(go)
}
}

Expand Down Expand Up @@ -130,3 +154,14 @@ public extension Item {
}
}
}

private extension Error {
var shouldRetry: Bool {
switch self {
case CKError.networkUnavailable, CKError.networkFailure:
return true
default:
return false
}
}
}
3 changes: 3 additions & 0 deletions Sources/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class RootViewController: NSViewController {

override func viewDidLoad() {
versionLabel.stringValue = "Workbench \(Bundle.main.version)"
#if DEBUG
versionLabel.stringValue += "-debug"
#endif
}

@IBAction func onMiscClicked(_ sender: Any) {
Expand Down

0 comments on commit 41d513f

Please sign in to comment.