-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored code and added retry mechanism
- Loading branch information
1 parent
8e45e0a
commit 24629a1
Showing
3 changed files
with
89 additions
and
52 deletions.
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Configuration | ||
|
||
// Start a configuration manager, load configuration from an adjacent | ||
// `config.json` file, cast config values to appropriate types, and | ||
// fail if required config values are not present | ||
struct Config { | ||
var url: String | ||
var writeKey: String | ||
init() { | ||
let manager = ConfigurationManager() | ||
manager.load(file: "config.json") | ||
url = manager["url"] as? String ?? "" | ||
writeKey = manager["writeKey"] as? String ?? "" | ||
|
||
if url == "" { | ||
fatalError("The config parameter 'url' is required. Set it in 'config.json' and please try again.") | ||
} | ||
|
||
if writeKey == "" { | ||
fatalError("The config parameter 'writeKey' is required. Set it in 'config.json' and please try again.") | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Cocoa | ||
import Just | ||
|
||
private func getISOTimestamp() -> String { | ||
if #available(macOS 10.12, *) { | ||
let date = Date() | ||
let dateFormatter = ISO8601DateFormatter() | ||
return dateFormatter.string(from: date) | ||
} else { | ||
fatalError("This process only runs on macOS 10.12+.") | ||
} | ||
} | ||
|
||
class Sync { | ||
private static let config = Config() | ||
|
||
enum SyncStatus { | ||
case pending(nextState: State) | ||
case success | ||
case failure(retryState: State) | ||
} | ||
|
||
var syncStatus: SyncStatus = .success | ||
private var task: DispatchWorkItem? | ||
|
||
func initializeSync(state: State) { | ||
syncStatus = .pending(nextState: state) | ||
handleSync() | ||
} | ||
|
||
private func handleSync() { | ||
if task != nil { task!.cancel() } | ||
|
||
switch syncStatus { | ||
case let .pending(nextState): | ||
sendRequest(state: nextState) | ||
case .success: | ||
break | ||
case let .failure(retryState): | ||
syncStatus = .pending(nextState: retryState) | ||
sendRequest(state: retryState) | ||
} | ||
} | ||
|
||
private func sendRequest(state: State) { | ||
let r = Just.post( | ||
Sync.config.url, | ||
json: ["state": state.rawValue, "updatedAt": getISOTimestamp()], | ||
auth: (Sync.config.writeKey, "") | ||
) | ||
if r.ok { | ||
NSLog("Network: request succeeded") | ||
syncStatus = .success | ||
} else { | ||
NSLog("Network: request failed") | ||
syncStatus = .failure(retryState: state) | ||
task = DispatchWorkItem { self.handleSync() } | ||
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: task!) | ||
} | ||
} | ||
} |