Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ abstract class BatchProcessor<Input, Output>(
open suspend fun run(items: List<Input>): Metrics = withContext(Dispatchers.IO) {
val processedCount = AtomicInteger(0)
val startTime = System.currentTimeMillis()
var totalSuccess = 0

try {
if (items.isEmpty()) {
Log.w(TAG, "No items to process.")
return@withContext Metrics.Success()
val metrics = Metrics.Success()
listener?.onComplete(application, metrics)
return@withContext metrics
}

val memoryUtils = MemoryUtils(application, options.memory)
Expand All @@ -44,24 +47,26 @@ abstract class BatchProcessor<Input, Output>(
semaphore.withPermit {
try {
val output = onProcess(application, item)
val current = processedCount.incrementAndGet()
val progress = current.toFloat() / items.size
listener?.onProgress(application, progress)
output
} catch (e: Exception) {
listener?.onError(application, e, item)
null
}finally {
val current = processedCount.incrementAndGet()
val progress = current.toFloat() / items.size
listener?.onProgress(application, progress)
}
}
}
}

val outputBatch = deferredResults.mapNotNull { it.await() }
totalSuccess += outputBatch.size
onBatchComplete(application, outputBatch)
}

val endTime = System.currentTimeMillis()
val metrics = Metrics.Success(processedCount.get(), timeElapsed = endTime - startTime)
val metrics = Metrics.Success(totalSuccess, timeElapsed = endTime - startTime)

listener?.onComplete(application, metrics)
metrics
Expand All @@ -71,7 +76,7 @@ abstract class BatchProcessor<Input, Output>(
}
catch (e: Exception) {
val metrics = Metrics.Failure(
processedBeforeFailure = processedCount.get(),
processedBeforeFailure = totalSuccess,
timeElapsed = System.currentTimeMillis() - startTime,
error = e
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BatchProcessorTest {
assertEquals(0, metrics.totalProcessed)

coVerify(exactly = 0) { mockListener.onProgress(any(), any()) }
coVerify ( exactly = 0){mockListener.onComplete(mockApp, any()) } // shouldnt call because early return
coVerify ( exactly = 1){mockListener.onComplete(mockApp, any()) }
}

@Test
Expand Down