From b9823012fbd8883b9bd6115b9065fb9ef8331ef9 Mon Sep 17 00:00:00 2001 From: Ignacio Tischelman Date: Fri, 1 Nov 2024 15:02:12 -0300 Subject: [PATCH] Fixing log batches logic --- .../Logs/Exporter/DefaultLogBatcher.swift | 29 +++++++++++-------- .../Exporter/StorageEmbraceLogExporter.swift | 2 +- .../StorageEmbraceLogExporterTests.swift | 4 +++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Sources/EmbraceCore/Internal/Logs/Exporter/DefaultLogBatcher.swift b/Sources/EmbraceCore/Internal/Logs/Exporter/DefaultLogBatcher.swift index b9c70992..adaa7316 100644 --- a/Sources/EmbraceCore/Internal/Logs/Exporter/DefaultLogBatcher.swift +++ b/Sources/EmbraceCore/Internal/Logs/Exporter/DefaultLogBatcher.swift @@ -14,6 +14,7 @@ protocol LogBatcherDelegate: AnyObject { protocol LogBatcher { func addLogRecord(logRecord: LogRecord) func renewBatch(withLogs logRecords: [LogRecord]) + func forceEndCurrentBatch() } class DefaultLogBatcher: LogBatcher { @@ -52,20 +53,24 @@ class DefaultLogBatcher: LogBatcher { } internal extension DefaultLogBatcher { - func renewBatch(withLogs logRecords: [LogRecord] = []) { + func forceEndCurrentBatch() { processorQueue.async { - guard let batch = self.batch else { - return - } - self.cancelBatchDeadline() - self.delegate?.batchFinished(withLogs: batch.logs) - // TODO: Add cleanup step: - // --> delete reported logs - self.batch = .init(limits: self.logLimits, logs: logRecords) + self.renewBatch() + } + } - if logRecords.count > 0 { - self.renewBatchDeadline(with: self.logLimits) - } + func renewBatch(withLogs logRecords: [LogRecord] = []) { + guard let batch = self.batch else { + return + } + self.cancelBatchDeadline() + self.delegate?.batchFinished(withLogs: batch.logs) + // TODO: Add cleanup step: + // --> delete reported logs + self.batch = .init(limits: self.logLimits, logs: logRecords) + + if logRecords.count > 0 { + self.renewBatchDeadline(with: self.logLimits) } } diff --git a/Sources/EmbraceCore/Internal/Logs/Exporter/StorageEmbraceLogExporter.swift b/Sources/EmbraceCore/Internal/Logs/Exporter/StorageEmbraceLogExporter.swift index 6ceb4436..d9366437 100644 --- a/Sources/EmbraceCore/Internal/Logs/Exporter/StorageEmbraceLogExporter.swift +++ b/Sources/EmbraceCore/Internal/Logs/Exporter/StorageEmbraceLogExporter.swift @@ -41,7 +41,7 @@ class StorageEmbraceLogExporter: LogRecordExporter { @objc func onSessionEnd(noticication: Notification) { // forcefully start a new batch of logs when a session ends - logBatcher.renewBatch(withLogs: []) + logBatcher.forceEndCurrentBatch() } func export(logRecords: [ReadableLogRecord], explicitTimeout: TimeInterval?) -> ExportResult { diff --git a/Tests/EmbraceCoreTests/Internal/Logs/Exporter/StorageEmbraceLogExporterTests.swift b/Tests/EmbraceCoreTests/Internal/Logs/Exporter/StorageEmbraceLogExporterTests.swift index fca9193a..539295ec 100644 --- a/Tests/EmbraceCoreTests/Internal/Logs/Exporter/StorageEmbraceLogExporterTests.swift +++ b/Tests/EmbraceCoreTests/Internal/Logs/Exporter/StorageEmbraceLogExporterTests.swift @@ -197,6 +197,10 @@ class SpyLogBatcher: LogBatcher { logRecords.append(logRecord) } + func forceEndCurrentBatch() { + self.renewBatch(withLogs: []) + } + private(set) var didCallRenewBatch: Bool = false private(set) var renewBatchInvocationCount: Int = 0 func renewBatch(withLogs logRecords: [LogRecord]) {