Skip to content

Commit

Permalink
fix: handle continuing writing to batch from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
nickybondarenko committed Apr 9, 2024
1 parent 510aa7a commit 5efd4ed
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,43 @@ internal protocol EventStorage {
}

internal class EventStorageImpl: EventStorage {
static let DIRECTORY = "events"
static let READYTOSENDEXTENSION = ".ready"
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let folderURL: URL
var fileURL: URL
var currentBatch: [Event] = []
private static let DIRECTORY = "events"
private static let READYTOSENDEXTENSION = ".ready"
private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
private let currentFolderURL: URL
private var currentFileURL: URL
private var currentBatch: [Event] = []

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

func startNewBatch() {
let urlString = "\(fileURL)"+"\(EventStorageImpl.READYTOSENDEXTENSION)"
let latestWriteFile = latestWriteFile()
if latestWriteFile != nil {
currentFileURL = latestWriteFile!

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
currentBatch = eventsFrom(id: latestWriteFile!.absoluteString)

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
return
}
let urlString = "\(currentFileURL)"+"\(EventStorageImpl.READYTOSENDEXTENSION)"

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used (operator_usage_whitespace)
let newPath = URL(fileURLWithPath: urlString)
do {
try FileManager.default.moveItem(at: fileURL, to: newPath)
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)")
}
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)")
currentFileURL = currentFolderURL.appendingPathComponent("events-\(Date().currentTime)")
currentBatch = []
}

Check warning on line 44 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) {
currentBatch.append(event)
do {
let data = try encoder.encode(currentBatch)
try data.write(to: fileURL, options: .atomic)
try data.write(to: currentFileURL, options: .atomic)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to write to disk: \(error)")
Expand All @@ -51,7 +57,7 @@ internal class EventStorageImpl: EventStorage {
var readyFilesList: [String] = []
var directoryContents: [String] = []
do {
directoryContents = try FileManager.default.contentsOfDirectory(atPath: folderURL.absoluteString)
directoryContents = try FileManager.default.contentsOfDirectory(atPath: currentFolderURL.absoluteString)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to read contents of directory on disk: \(error)")
Expand Down Expand Up @@ -100,6 +106,22 @@ internal class EventStorageImpl: EventStorage {
}
return nestedFolderURL
}

private func latestWriteFile() -> URL? {
var directoryContents: [String] = []
do {
directoryContents = try FileManager.default.contentsOfDirectory(atPath: EventStorageImpl.getFolderURL())
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to read contents of directory on disk: \(error)")
}
for file in directoryContents {
if !file.hasSuffix(EventStorageImpl.READYTOSENDEXTENSION) {

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

View workflow job for this annotation

GitHub Actions / SwiftLint

Prefer For-Where Violation: `where` clauses are preferred over a single `if` inside a `for` (for_where)
return URL(string: file)
}
}
return nil
}
}

struct Event: Codable {
Expand Down

0 comments on commit 5efd4ed

Please sign in to comment.