Skip to content
Draft
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
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/repositories/CurrencyRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import to.bitkit.App
import to.bitkit.async.appScope
import to.bitkit.data.CacheStore
import to.bitkit.data.SettingsStore
Expand Down Expand Up @@ -84,6 +85,8 @@ class CurrencyRepo @Inject constructor(
private fun startPolling() {
repoScope.launch {
fxRatePollingFlow.collect {
// Android blocks a backgrounded app's network, so a refresh there only fails; ON_START refreshes
if (App.currentActivity?.hasStartedActivity == false) return@collect
refresh()
}
}
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@ class LightningRepo @Inject constructor(
delay(SYNC_RETRY_DELAY_MS)
if (restartStoppedNode()) return@launch
Logger.info("Retrying sync after failure", context = TAG)
sync().onSuccess {
Logger.info("Sync retry succeeded", context = TAG)
runSync().onSuccess {
when (it) {
SyncRun.Synced -> Logger.info("Sync retry succeeded", context = TAG)
// The sync in progress runs once more for it, and its outcome decides the next check
SyncRun.Deferred -> Logger.info("Sync retry deferred to the sync in progress", context = TAG)
}
}.onFailure {
Logger.warn("Sync retry failed, will retry in ${SYNC_RETRY_DELAY_MS / 1000}s", it, context = TAG)
}
Expand Down Expand Up @@ -946,12 +950,17 @@ class LightningRepo @Inject constructor(
}
}

suspend fun sync(): Result<Unit> = executeWhenNodeRunning("sync") {
suspend fun sync(): Result<Unit> = runSync().map { }

/** Whether a sync ran, or was left to the one in progress, which runs once more after it ends. */
private enum class SyncRun { Synced, Deferred }

private suspend fun runSync(): Result<SyncRun> = executeWhenNodeRunning("sync") {
// If sync is in progress, mark pending and skip
if (!syncMutex.tryLock()) {
syncPending.set(true)
Logger.verbose("Sync in progress, pending sync marked", context = TAG)
return@executeWhenNodeRunning Result.success(Unit)
return@executeWhenNodeRunning Result.success(SyncRun.Deferred)
}

runCatching {
Expand All @@ -969,6 +978,7 @@ class LightningRepo @Inject constructor(
}
if (syncPending.get()) delay(MS_SYNC_LOOP_DEBOUNCE)
} while (syncPending.getAndSet(false))
SyncRun.Synced
}.also {
_lightningState.update { state -> state.copy(isSyncingWallet = false) }
syncMutex.unlock()
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/to/bitkit/repositories/SparkRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import kotlinx.serialization.Serializable
import org.lightningdevkit.ldknode.Event
import org.lightningdevkit.ldknode.PaymentFailureReason
import org.lightningdevkit.ldknode.PaymentId
import to.bitkit.App
import to.bitkit.async.appScope
import to.bitkit.data.CacheStore
import to.bitkit.data.SettingsStore
Expand Down Expand Up @@ -1024,7 +1025,15 @@ class SparkRepo @Inject constructor(
val info = runSuspendCatching { sparkService.getInfo() }
.onFailure { Logger.warn("Failed to read Spark's sync state", it, context = TAG) }
.getOrNull() ?: return false
info.lastSyncError?.let { Logger.warn("Spark's last sync failed: '$it'", context = TAG) }
info.lastSyncError?.let {
// Spark keeps syncing in the background, where Android blocks the app's network, so a failure there is
// expected and not a fault to warn about
if (App.currentActivity?.hasStartedActivity == false) {
Logger.debug("Spark's last sync failed in the background: '$it'", context = TAG)
} else {
Logger.warn("Spark's last sync failed: '$it'", context = TAG)
}
}
val syncedAt = info.lastSyncedAt
return info.lastSyncError == null && syncedAt != null && (seconds == null || syncedAt > seconds)
}
Expand Down