Skip to content

Commit

Permalink
Fix crash on sync request failures
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 29, 2024
1 parent 4075919 commit aedc877
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 57 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ dependencies {
// Modern HTTP client
// https://github.com/square/okhttp/blob/master/CHANGELOG.md
val okhttpVer = "5.0.0-alpha.14"
implementation("com.squareup.okhttp3:okhttp-coroutines:$okhttpVer")
implementation("com.squareup.okhttp3:okhttp-brotli:$okhttpVer")
testImplementation("com.squareup.okhttp3:mockwebserver:$okhttpVer")

Expand Down
12 changes: 6 additions & 6 deletions app/src/main/kotlin/api/ApiImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import element.ElementJson
import element.toElementsJson
import event.EventJson
import event.toEventsJson
import http.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.coroutines.executeAsync
import reports.ReportJson
import reports.toReportsJson
import user.UserJson
Expand All @@ -37,7 +37,7 @@ class ApiImpl(
}.build()

val request = httpClient.newCall(Request.Builder().url(url).build())
val response = request.await()
val response = request.executeAsync()

if (!response.isSuccessful) {
throw Exception("Unexpected HTTP response code: ${response.code}")
Expand All @@ -61,7 +61,7 @@ class ApiImpl(
}.build()

val request = httpClient.newCall(Request.Builder().url(url).build())
val response = request.await()
val response = request.executeAsync()

if (!response.isSuccessful) {
throw Exception("Unexpected HTTP response code: ${response.code}")
Expand Down Expand Up @@ -89,7 +89,7 @@ class ApiImpl(
}.build()

val request = httpClient.newCall(Request.Builder().url(url).build())
val response = request.await()
val response = request.executeAsync()

if (!response.isSuccessful) {
throw Exception("Unexpected HTTP response code: ${response.code}")
Expand All @@ -113,7 +113,7 @@ class ApiImpl(
}.build()

val request = httpClient.newCall(Request.Builder().url(url).build())
val response = request.await()
val response = request.executeAsync()

if (!response.isSuccessful) {
throw Exception("Unexpected HTTP response code: ${response.code}")
Expand Down Expand Up @@ -141,7 +141,7 @@ class ApiImpl(
}.build()

val request = httpClient.newCall(Request.Builder().url(url).build())
val response = request.await()
val response = request.executeAsync()

if (!response.isSuccessful) {
throw Exception("Unexpected HTTP response code: ${response.code}")
Expand Down
23 changes: 0 additions & 23 deletions app/src/main/kotlin/http/CallExtensions.kt

This file was deleted.

59 changes: 31 additions & 28 deletions app/src/main/kotlin/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import event.EventsRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
Expand All @@ -35,39 +36,41 @@ class Sync(

mutex.withLock {
runCatching {
val startedAt = now()
coroutineScope {
val startedAt = now()

if (elementsRepo.selectCount() == 0L && elementsRepo.hasBundledElements()) {
elementsRepo.fetchBundledElements().getOrThrow()
}
if (elementsRepo.selectCount() == 0L && elementsRepo.hasBundledElements()) {
elementsRepo.fetchBundledElements().getOrThrow()
}

val elementsReport = async { elementsRepo.sync().getOrThrow() }
val reportsReport = async { reportsRepo.sync().getOrThrow() }
val areasReport = async { areasRepo.sync().getOrThrow() }
val usersReport = async { usersRepo.sync().getOrThrow() }
val eventsReport = async { eventsRepo.sync().getOrThrow() }
val elementsReport = async { elementsRepo.sync().getOrThrow() }
val reportsReport = async { reportsRepo.sync().getOrThrow() }
val areasReport = async { areasRepo.sync().getOrThrow() }
val usersReport = async { usersRepo.sync().getOrThrow() }
val eventsReport = async { eventsRepo.sync().getOrThrow() }

listOf(
elementsReport,
reportsReport,
areasReport,
usersReport,
eventsReport,
).awaitAll()
listOf(
elementsReport,
reportsReport,
areasReport,
usersReport,
eventsReport,
).awaitAll()

val fullReport = SyncReport(
startedAt = startedAt,
finishedAt = ZonedDateTime.now(ZoneOffset.UTC),
newElements = elementsReport.await().newElements,
updatedElements = elementsReport.await().updatedElements,
newEvents = eventsReport.await().newEvents,
updatedEvents = eventsReport.await().updatedEvents,
)
val fullReport = SyncReport(
startedAt = startedAt,
finishedAt = ZonedDateTime.now(ZoneOffset.UTC),
newElements = elementsReport.await().newElements,
updatedElements = elementsReport.await().updatedElements,
newEvents = eventsReport.await().newEvents,
updatedEvents = eventsReport.await().updatedEvents,
)

syncNotificationController.showPostSyncNotifications(
report = fullReport,
conf = conf.current,
)
syncNotificationController.showPostSyncNotifications(
report = fullReport,
conf = conf.current,
)
}
}.onSuccess {
conf.update { it.copy(lastSyncDate = now()) }
}.onFailure {
Expand Down

0 comments on commit aedc877

Please sign in to comment.