Skip to content

Commit

Permalink
fix: handle continuing writing to batch from disk, remove force unwra…
Browse files Browse the repository at this point in the history
…pping
  • Loading branch information
nickybondarenko committed Apr 9, 2024
1 parent 5efd4ed commit 0c0a666
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,25 @@ internal class EventStorageImpl: EventStorage {

init() throws {
currentFolderURL = URL(fileURLWithPath: try EventStorageImpl.getFolderURL())
currentFileURL = currentFolderURL.appendingPathComponent("events-\(Date().currentTime)")
currentFileURL = EventStorageImpl.latestWriteFile() ?? currentFolderURL.appendingPathComponent("events-\(Date().currentTime)")

Check warning on line 23 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Line Length Violation: Line should be 120 characters or less; currently it has 134 characters (line_length)
}

func startNewBatch() {
let latestWriteFile = latestWriteFile()
if latestWriteFile != nil {
currentFileURL = latestWriteFile!
currentBatch = eventsFrom(id: latestWriteFile!.absoluteString)
guard let latestWriteFile = EventStorageImpl.latestWriteFile() else {
let urlString = "\(currentFileURL)\(EventStorageImpl.READYTOSENDEXTENSION)"
let newPath = URL(fileURLWithPath: urlString)
do {
try FileManager.default.moveItem(at: currentFileURL, to: newPath)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to start a new batch: \(error)")
}
currentFileURL = currentFolderURL.appendingPathComponent("events-\(Date().currentTime)")
currentBatch = []
return
}
let urlString = "\(currentFileURL)"+"\(EventStorageImpl.READYTOSENDEXTENSION)"
let newPath = URL(fileURLWithPath: urlString)
do {
try FileManager.default.moveItem(at: currentFileURL, to: newPath)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to start a new batch: \(error)")
}
currentFileURL = currentFolderURL.appendingPathComponent("events-\(Date().currentTime)")
currentBatch = []
currentFileURL = latestWriteFile
currentBatch = eventsFrom(id: latestWriteFile.absoluteString)
}

Check warning on line 43 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
func writeEvent(event: Event) {
Expand Down Expand Up @@ -107,7 +106,7 @@ internal class EventStorageImpl: EventStorage {
return nestedFolderURL
}

private func latestWriteFile() -> URL? {
private static func latestWriteFile() -> URL? {
var directoryContents: [String] = []
do {
directoryContents = try FileManager.default.contentsOfDirectory(atPath: EventStorageImpl.getFolderURL())
Expand Down

0 comments on commit 0c0a666

Please sign in to comment.