Skip to content

Colota - Android GPS Location tracker

fix: prevent locations from being dropped when retry-forever is enabled

Max Dietrichffb6fb722142
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
index 383b4789..08260052 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
@@ -187,6 +187,12 @@ class SyncManager(
}
private suspend fun syncQueue(onProgress: ((sent: Int, failed: Int) -> Unit)? = null) = coroutineScope {
+ // Snapshot volatile config so it stays consistent for the entire sync pass
+ val currentEndpoint = endpoint
+ val currentAuthHeaders = authHeaders
+ val currentHttpMethod = httpMethod
+ val currentMaxRetries = maxRetries // 0 = retry forever, >0 = give up after N attempts
+
var totalProcessed = 0
var totalSucceeded = 0
var totalFailed = 0
@@ -206,24 +212,30 @@ class SyncManager(
}
// Chunks of 10 concurrent HTTP requests to avoid flooding the server
+ val processedBefore = totalProcessed
+
for (chunk in queued.chunked(10)) {
val successfulIds = mutableListOf<Long>()
val permanentlyFailedIds = mutableListOf<Long>()
- val (retriable, exceeded) = chunk.partition { it.retryCount < maxRetries }
+ val (retriable, exceeded) = if (currentMaxRetries <= 0) {
+ chunk to emptyList()
+ } else {
+ chunk.partition { it.retryCount < currentMaxRetries }
+ }
permanentlyFailedIds.addAll(exceeded.map { it.queueId })
if (BuildConfig.DEBUG && exceeded.isNotEmpty()) {
- Log.w(TAG, "Removing ${exceeded.size} items that exceeded $maxRetries retries")
+ Log.w(TAG, "Removing ${exceeded.size} items that exceeded $currentMaxRetries retries")
}
val results = retriable.map { item ->
async {
val success = networkManager.sendToEndpoint(
JSONObject(item.payload),
- endpoint,
- authHeaders,
- httpMethod
+ currentEndpoint,
+ currentAuthHeaders,
+ currentHttpMethod
)
item.queueId to success
}
@@ -236,7 +248,7 @@ class SyncManager(
val item = retriable.first { it.queueId == queueId }
dbHelper.incrementRetryCount(queueId, "Send failed")
- if (item.retryCount + 1 >= maxRetries) {
+ if (currentMaxRetries > 0 && item.retryCount + 1 >= currentMaxRetries) {
permanentlyFailedIds.add(queueId)
if (BuildConfig.DEBUG) {
Log.w(TAG, "Item $queueId reached max retries")
@@ -250,10 +262,12 @@ class SyncManager(
dbHelper.removeBatchFromQueue(toRemove)
// Delete locations of permanently failed items (never sent)
- val failedLocationIds = chunk
- .filter { it.queueId in permanentlyFailedIds }
- .map { it.locationId }
- dbHelper.deleteLocations(failedLocationIds)
+ if (permanentlyFailedIds.isNotEmpty()) {
+ val failedLocationIds = chunk
+ .filter { it.queueId in permanentlyFailedIds }
+ .map { it.locationId }
+ dbHelper.deleteLocations(failedLocationIds)
+ }
totalProcessed += toRemove.size
totalSucceeded += successfulIds.size
@@ -264,6 +278,14 @@ class SyncManager(
yield()
}
+ // No items removed from queue. Stop re-fetching the same failing items
+ if (totalProcessed == processedBefore) {
+ if (BuildConfig.DEBUG) {
+ Log.d(TAG, "No progress in batch $batchNumber, stopping sync pass")
+ }
+ break
+ }
+
batchNumber++
}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
index 358a254b..50b6cba0 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
@@ -354,13 +354,111 @@ class SyncManagerTest {
verify { dbHelper.removeBatchFromQueue(match { it.containsAll(listOf(1L, 2L)) }) }
}
- // --- stopPeriodicSync ---
+ // --- syncQueue: maxRetries=0 (retry forever) ---
@Test
- fun `stopPeriodicSync cancels job`() {
- syncManager.startPeriodicSync()
- syncManager.stopPeriodicSync()
- // No exception means the job was cancelled cleanly
+ fun `syncQueue retries all items when maxRetries is 0`() = scope.runTest {
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 0,
+ retryIntervalSeconds = 30,
+ maxRetries = 0,
+ isOfflineMode = false,
+ isWifiOnlySync = false,
+ authHeaders = emptyMap()
+ )
+
+ // Item with high retry count should still be sent, not treated as exceeded
+ val item = QueuedLocation(1L, 100L, """{"lat":52.0}""", 99)
+ every { dbHelper.getQueuedLocations(50) } returnsMany listOf(listOf(item), emptyList())
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns true
+
+ syncManager.manualFlush()
+
+ coVerify(exactly = 1) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
+ verify { dbHelper.removeBatchFromQueue(listOf(1L)) }
+ // Location should NOT be deleted (it was successfully sent)
+ verify(exactly = 0) { dbHelper.deleteLocations(any()) }
+ }
+
+ @Test
+ fun `syncQueue does not permanently fail items when maxRetries is 0`() = scope.runTest {
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 0,
+ retryIntervalSeconds = 30,
+ maxRetries = 0,
+ isOfflineMode = false,
+ isWifiOnlySync = false,
+ authHeaders = emptyMap()
+ )
+
+ val item = QueuedLocation(1L, 100L, """{"lat":52.0}""", 50)
+ every { dbHelper.getQueuedLocations(50) } returnsMany listOf(listOf(item), emptyList())
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns false
+
+ syncManager.manualFlush()
+
+ // Should increment retry count but NOT remove from queue or delete location
+ verify { dbHelper.incrementRetryCount(1L, "Send failed") }
+ verify(exactly = 0) { dbHelper.removeBatchFromQueue(any()) }
+ verify(exactly = 0) { dbHelper.deleteLocations(any()) }
+ }
+
+ @Test
+ fun `syncQueue stops fetching batches when all sends fail`() = scope.runTest {
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 0,
+ retryIntervalSeconds = 30,
+ maxRetries = 5,
+ isOfflineMode = false,
+ isWifiOnlySync = false,
+ authHeaders = emptyMap()
+ )
+
+ val item = QueuedLocation(1L, 100L, """{"lat":52.0}""", 0)
+ // Always return the same item (it stays in queue after failure)
+ every { dbHelper.getQueuedLocations(50) } returns listOf(item)
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns false
+
+ syncManager.manualFlush()
+
+ // Should only fetch ONE batch, not loop 10 times re-fetching the same failing item
+ verify(exactly = 1) { dbHelper.getQueuedLocations(50) }
+ coVerify(exactly = 1) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
+ }
+
+ @Test
+ fun `syncQueue continues to next batch after cleaning up exceeded items`() = scope.runTest {
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 0,
+ retryIntervalSeconds = 30,
+ maxRetries = 3,
+ isOfflineMode = false,
+ isWifiOnlySync = false,
+ authHeaders = emptyMap()
+ )
+
+ // Batch 1: only exceeded items (cleaned up, no sends)
+ // Batch 2: fresh items that should be sent
+ val exceeded = QueuedLocation(1L, 100L, """{"lat":52.0}""", 3)
+ val fresh = QueuedLocation(2L, 101L, """{"lat":53.0}""", 0)
+ every { dbHelper.getQueuedLocations(50) } returnsMany listOf(
+ listOf(exceeded),
+ listOf(fresh),
+ emptyList()
+ )
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns true
+
+ syncManager.manualFlush()
+
+ // Should process BOTH batches: clean up exceeded AND send fresh
+ verify(exactly = 3) { dbHelper.getQueuedLocations(50) }
+ coVerify(exactly = 1) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
+ verify { dbHelper.removeBatchFromQueue(listOf(1L)) }
+ verify { dbHelper.removeBatchFromQueue(listOf(2L)) }
}
// --- getCachedQueuedCount ---
@@ -436,7 +534,7 @@ class SyncManagerTest {
authHeaders = emptyMap()
)
- // Always return items — loop should still stop at batch 10
+ // Always return items - loop should still stop at batch 10
val item = QueuedLocation(1L, 100L, """{"lat":52.0}""", 0)
every { dbHelper.getQueuedLocations(50) } returns listOf(item)
coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns true
@@ -648,11 +746,11 @@ class SyncManagerTest {
syncManager.startPeriodicSync()
- // Before interval elapses — no sync yet
+ // Before interval elapses - no sync yet
advanceTimeBy(30_000)
coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
- // After interval elapses — sync happens
+ // After interval elapses - sync happens
advanceTimeBy(31_000)
coVerify(atLeast = 1) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
syncManager.stopPeriodicSync()